Source of ShowGrades.java


  2: import java.util.Scanner;
  3: import java.util.NoSuchElementException;
  4: import java.io.*;

  6: public class ShowGrades {
  7:     
  8:     public static final Scanner KBD = new Scanner(System.in);

 10:     public static void main(String[] args) {

 12:         // create variables
 13:         Student[] myClass = readStudentInfo();
 14:         int[] grades;
 15:         String aNumber;

 17:         // class summary
 18:         printClass(myClass);
 19:         
 20:         // get and save grades
 21:         while (!(aNumber = getANumber()).isEmpty()) {
 22:             // find this Student's grades and write them to the Web
 23:             grades = getGrades(aNumber, myClass);
 24:             writeGrades(findStudent(aNumber, myClass));
 25:         }
 26:     }

 28:     /**
 29:      * Get and return an A-number from the user. The methods prompts for the 
 30:      * A-number, and reminds the user that an empty string will end input, but
 31:      * does not check whether the A-number entered is valid.
 32:      * 
 33:      * @return the A-number entered by the user
 34:      */
 35:     public static String getANumber() {
 36:         String aNumber;
 37:         
 38:         System.out.print("Enter an A-Number (or nothing to quit): ");
 39:         aNumber = KBD.nextLine();
 40:         
 41:         return aNumber;
 42:     }

 44:     /**
 45:      * Print out the Students in the given array.
 46:      *
 47:      * @param myClass the array of Students to print out
 48:      */
 49:     private static void printClass(Student[] myClass) {
 50:         System.out.println("\nYour class:");
 51:         for (int s = 0; s < myClass.length; ++s) {
 52:             System.out.println("\t" + myClass[s]);
 53:         }
 54:         System.out.println();
 55:     }

 57:     /**
 58:      * Read Student names and grades from a data file.
 59:      * NOTE: CSCI1226 students do not need to understand how this method works!
 60:      *
 61:      * @return an array containing Student objects for each student's info
 62:      *         found in the file.
 63:      */
 64:     private static Student[] readStudentInfo() {
 65:         // create variables
 66:         Student[] result;
 67:         int numStu;
 68:         String name;
 69:         int grade;

 71:         // try to get all the student information
 72:         try(Scanner in = new Scanner(new File("StudentInfo.txt"))) {
 73:             // file starts with # of student records in file
 74:             numStu = in.nextInt();
 75:             in.nextLine();

 77:             // create an array big enuf to hold them all
 78:             result = new Student[numStu];

 80:             // read in the student records
 81:             for (int i = 0; i < numStu; ++i) {
 82:                 // record starts with name on its own line
 83:                 name = in.nextLine();
 84:                 result[i] = new Student(name);

 86:                 // next line has grades for every assignment
 87:                 for (int a = 1; a <= Student.NUM_ASGN; ++a) {
 88:                     grade = in.nextInt();
 89:                     result[i].setAsgnGrade(a, grade);
 90:                 }
 91:                 in.nextLine();
 92:             }

 94:             // all info saved, so return it
 95:             return result;
 96:         } 
 97:         // deal with possible weird stuff by printing an error message
 98:         // and quitting the program(!)
 99:         catch (FileNotFoundException fnf) {
100:             System.out.println("Missing data file: StudentInfo.txt");
101:             System.exit(0);
102:         } catch (NoSuchElementException nse) {
103:             System.out.println("Corrupt data file: StudentInfo.txt");
104:             System.exit(0);
105:         }

107:         // should never get here, but Java requires a return here
108:         return null;
109:     }

111:     /**
112:      * Find the assignments grades for the student with the given ANumber.
113:      *
114:      * @param aNumber the A-Number of the Student to find
115:      * @param myClass the array to find the student in
116:      * @return an array containing the assignment grades of the given student
117:      *         OR null if there is no such Student
118:      */
119:     private static int[] getGrades(String aNumber, Student[] myClass) {
120:         for (int s = 0; s < myClass.length; ++s) {
121:             if (aNumber.equals(myClass[s].getANumber())) {
122:                 return myClass[s].getAsgnGrades();
123:             }
124:         }
125:         return null;
126:     }

128:     /**
129:      * Write a Student's grades into a Web file.
130:      * The Web file is named with the student's A-Number
131:      * and the extension "html".
132:      * NOTE: CSCI1226 students don't need to understand how this method works!
133:      *
134:      * @param aNumber the A-Number of the Student
135:      * @param grades an array containing that Student's grades
136:      */
137:     private static void writeGrades(Student stu) {
138:         if (stu == null) {
139:             System.out.println("No such Student!");
140:         } else {
141:             String aNumber = stu.A_NUMBER;
142:             int[] grades = stu.getAsgnGrades();
143:             
144:             try(PrintWriter out = new PrintWriter(new File(aNumber+".html"))) {
145:                 // create head matter for the file
146:                 out.println("<HTML><HEAD><TITLE>" + aNumber 
147:                         + "'s Grades</TITLE></HEAD><BODY>");

149:                 // create in-page title
150:                 out.println("<H1>Assignment Grades for " + aNumber + "</H1>");

152:                 // create table of grades
153:                 out.println("<TABLE BORDER>");
154:                 for (int a = 0; a < grades.length; ++a) {
155:                     out.printf(" <TR>"
156:                             + "<TH>A%02d</TH>"
157:                             + "<TD align='right'>%d</TD>"
158:                             + "</TR>%n",
159:                         (a+1), grades[a]);
160:                 }

162:                 // close off table, body and file
163:                 out.println("</TABLE>");
164:                 out.println("<p>Final Assignment Grade: " 
165:                         + stu.getAsgnsGrade() + "%");
166:                 out.println("</BODY></HTML>");
167:                 out.close();

169:                 // report success to user
170:                 System.out.println("Grade report created in file " 
171:                         + aNumber + ".html");
172:             } catch (FileNotFoundException fnf) {
173:                 System.out.println("Could not create output file!");
174:             }
175:         }
176:     }

178:     private static Student findStudent(String aNumber, Student[] myClass) {
179:         for (Student s : myClass) {
180:             if (aNumber.equals(s.A_NUMBER)) {
181:                 return s;
182:             }
183:         }
184:         return null;
185:     }
186:     
187: }