import java.awt.*;
import java.io.*;
import java.util.*;
import java.io.FileWriter;
public class Homework2{
public static final int ROWS = 10; //you may change this value
public static final int COLS = 7; //you may change this value
public static final int SIZE = 85; //you may change this value
public static final int WAIT = 100; //you may change this value.
public static final Color IDENTCOLOR = Color.WHITE;
public static final String[] identifiers = {"charname", "V_VS_DV", "SHOES","INVIS_FLY", "LIFE_FREE",
"COLSANDERS","VEY_APOLLO","LAK_CELTIC","CS_MAJOR","SMILE","LOVEFIRST"};
public static void main(String[] args)throws FileNotFoundException {
File inFile = new File("C:/Users/Irena/Desktop/Homework2/sampleinput2.txt");
Scanner scanner;
try {
scanner = new Scanner(inFile);
} catch (Exception e){
System.err.println("File not found: " + e.toString());
return;
}
Mosaic.open(ROWS,COLS,SIZE,SIZE);//Opens Canvas
labelQuestions();//Labels Questions
getAnswers(scanner);//Reads Text Files For Information
exportCSVFile(scanner);//Exports Information As A CSV File
}//end of main
//Method: labelQuestions
//Input: N/A
//Called by: main
//Purpose: This sends the question identifiers to the Mosaic canvas,
// labeling them in their appropriate spots.
public static void labelQuestions(){
for(int i = 1; i <= ROWS; i++){
Mosaic.drawLabel(i-1, 0, identifiers[i], IDENTCOLOR);
}
}//End of labelQuestions
//Method: getAnswers
//Input: Scanner scanner
//Called by: main
//Purpose: This methods reads through the inputed textfile for lines that
// contain colons. It then splits the Strings along the colon character.
// It then organizes this information into array lists so that each array
// lists the character and the numerical value of each vote cast by that character.
public static void getAnswers(Scanner scanner){
ArrayList<String[]> userSurveyData = new ArrayList<String[]>();
String[] userTempSurveyData = new String[11];
while(scanner.hasNextLine()){
String[] line = scanner.nextLine().split(": ");
for(int i = 0; i < identifiers.length; i++){
if(line[0].equals(identifiers[i]) && !line[1].equals("")){
userTempSurveyData[i] = line[1];
colorAnswers(userTempSurveyData[0],userTempSurveyData[i],i-1);
}
}
if (line[0].equals("request") && line[1].equals("Submit the survey")) {
userSurveyData.add(userTempSurveyData);
System.out.println();
for (int j=0; j < 10; j++)
userTempSurveyData[j] = "";
}
}
}//End of getAnswers
//Method: colorAnswers
//Input: String Name, String Answer, int i
//Called by: getAnswers
//Purpose: This methods gets the name of the respondant, the numberical
// answer and the row number and then sends all of that information
// to the appropriate method that will send the data to the
// Mosaic canvas to visually represent the data.
public static void colorAnswers(String Name, String Answer, int i){
if(Answer.equals("5")){
colorIntensity(i,1,Name);
}
if(Answer.equals("4")){
colorIntensity(i,2,Name);
}
if(Answer.equals("3")){
colorIntensity(i,3,Name);
}
if(Answer.equals("2")){
colorIntensity(i,4,Name);
}
if(Answer.equals("1")){
colorIntensity(i,5,Name);
}
if(Answer.equals("0")){
colorIntensity(i,6,Name);
}
Mosaic.delay(WAIT);
}//End of colorAnswers
//Method: colorIntensity
//Input: int i, int j, String Name
//Called by: colorAnswers
//Purpose: This method gets data from colorAnswers and sends that information
// to the proper location on the Mosaic. This method is also in change
// for increasing the opacity of an answer as more and more people vote
// for that option.
public static void colorIntensity(int i, int j, String Name){
Mosaic.getBlue(i,j);
Mosaic.setColor(i, j, 0, 0, Mosaic.getBlue(i,j)+2);
Mosaic.drawLabel(i, j, Name, IDENTCOLOR);
}//End of colorIntensity
//Method: exportCSVFile
//Input: Scanner scanner
//Called by: main
//Purpose: This method writes pertinent data to a csv file, naming the
// file according to what the user inputs when prompted.
public static void exportCSVFile(Scanner scanner){
Scanner scan = new Scanner(System.in);
String[] results = {"","", "", "", "", "", "", "", "", "", ""};
System.out.print("Please enter the output file name:\t");
String message = scan.nextLine();
while(scanner.hasNextLine()){
String[] line = scanner.nextLine().split(": ");
for(int i = 0; i < identifiers.length; i++){
if(line[0].equals(identifiers[i])){
if(!(results[i].equals("")))
results[i] += ", ";
results[i] += line[1];
}
}
}
try{
FileWriter writer = new FileWriter(message);
writer.append("question identifier,strongly agree,agree,no opinion,disagree,strongly disagree,N/A or decline,");
for(int i = 1; i < identifiers.length; i++){
writer.append(identifiers[i]+","+results[i]+",");
}
writer.flush();
writer.close();
}
catch(IOException e){
e.printStackTrace();
}
}//End of exportCSVFile
}//End of Homework2