Source of ShowGrades.java


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

  6: public class ShowGrades {

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

 10:         // create variables
 11:         Scanner kbd = new Scanner(System.in);
 12:         Student[] myClass = readStudentInfo();
 13:         int[] grades;
 14:         String aNumber;

 16:         // class summary
 17:         printClass(myClass);

 19:         // read and report on assignment grades
 20:         System.out.print("Enter an A-Number (or nothing to quit): ");
 21:         aNumber = kbd.nextLine();
 22:         while (!"".equals(aNumber)) {
 23:             // find this Student's grades and write them to the Web
 24:             grades = getGrades(aNumber, myClass);
 25:             writeGrades(aNumber, grades);

 27:             // get next A-Number
 28:             System.out.print("Enter an A-Number (or nothing to quit): ");
 29:             aNumber = kbd.nextLine();
 30:         }
 31:     }

 33:     /**
 34:      * Print out the Students in the given array.
 35:      *
 36:      * @param myClass the array of Students to print out
 37:      */
 38:     private static void printClass(Student[] myClass) {
 39:         System.out.println("\nYour class:");
 40:         for (int s = 0; s < myClass.length; ++s) {
 41:             System.out.println("\t" + myClass[s]);
 42:         }
 43:         System.out.println();
 44:     }

 46:     /**
 47:      * Read Student names and grades from a data file.
 48:      * NOTE: CSCI1226 students do not need to understand how this method works!
 49:      *
 50:      * @return an array containing Student objects for each student's info
 51:      *         found in the file.
 52:      */
 53:     private static Student[] readStudentInfo() {
 54:         // create variables
 55:         Student[] result;
 56:         int numStu;
 57:         String name;
 58:         int grade;

 60:         // try to get all the student information
 61:         try(Scanner in = new Scanner(new File("StudentInfo.txt"))) {
 62:             // file starts with # of student records in file
 63:             numStu = in.nextInt();
 64:             in.nextLine();

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

 69:             // read in the student records
 70:             for (int i = 0; i < numStu; ++i) {
 71:                 // record starts with name on its own line
 72:                 name = in.nextLine();
 73:                 result[i] = new Student(name);

 75:                 // next line has grades for every assignment
 76:                 for (int a = 1; a <= Student.NUM_ASGN; ++a) {
 77:                     grade = in.nextInt();
 78:                     result[i].setAsgnGrade(a, grade);
 79:                 }
 80:                 in.nextLine();
 81:             }

 83:             // all info saved, so return it
 84:             return result;
 85:         } 
 86:         // deal with possible weird stuff by printing an error message
 87:         // and quitting the program(!)
 88:         catch (FileNotFoundException fnf) {
 89:             System.out.println("Missing data file: StudentInfo.txt");
 90:             System.exit(0);
 91:         } catch (NoSuchElementException nse) {
 92:             System.out.println("Corrupt data file: StudentInfo.txt");
 93:             System.exit(0);
 94:         }

 96:         // should never get here, but Java requires a return here
 97:         return null;
 98:     }

100:     /**
101:      * Find the assignments grades for the student with the given ANumber.
102:      *
103:      * @param aNumber the A-Number of the Student to find
104:      * @param myClass the array to find the student in
105:      * @return an array containing the assignment grades of the given student
106:      *         OR null if there is no such Student
107:      */
108:     private static int[] getGrades(String aNumber, Student[] myClass) {
109:         for (int s = 0; s < myClass.length; ++s) {
110:             if (aNumber.equals(myClass[s].getANumber())) {
111:                 return myClass[s].getAsgnGrades();
112:             }
113:         }
114:         return null;
115:     }

117:     /**
118:      * Write a Student's grades into a Web file.
119:      * The Web file is named with the student's A-Number
120:      * and the extension "html".
121:      * NOTE: CSCI1226 students don't need to understand how this method works!
122:      *
123:      * @param aNumber the A-Number of the Student
124:      * @param grades an array containing that Student's grades
125:      */
126:     private static void writeGrades(String aNumber, int[] grades) {
127:         if (grades == null) {
128:             System.out.println("No such Student: " + aNumber);
129:         } else {
130:             try(PrintWriter out = new PrintWriter(new File(aNumber+".html"))) {
131:                 // create head matter for the file
132:                 out.println("<HTML><HEAD><TITLE>" + aNumber 
133:                         + "'s Grades</TITLE></HEAD><BODY>");

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

138:                 // create table of grades
139:                 out.println("<TABLE BORDER>");
140:                 for (int a = 0; a < grades.length; ++a) {
141:                     out.printf(" <TR><TH>A%02d</TH><TD>%d</TD></TR>%n",
142:                         (a+1), grades[a]);
143:                 }

145:                 // close off table, body and file
146:                 out.println("</TABLE></BODY></HTML>");
147:                 out.close();

149:                 // report success to user
150:                 System.out.println("Grade report created in file " 
151:                         + aNumber + ".html");
152:             } catch (FileNotFoundException fnf) {
153:                 System.out.println("Could not create output file!");
154:             }
155:         }
156:     }
157: }