Source of TestStudent.java


  2: import java.util.Scanner;

  4: /**
  5:  * Check the Student class by creating some objects and manipulating them.
  6:  * This version of the program is for version 2.0 (or later) of the Student 
  7:  * class.
  8:  *
  9:  * @author Mark Young (A00000000)
 10:  * @version 2.1 (2017-03-13)
 11:  */
 12: public class TestStudent {

 14:     public static void main(String[] args) {
 15:         // create a couple of students
 16:         Scanner kbd = new Scanner(System.in);
 17:         Student s1 = new Student("A00000001", "Dent, Stu.", 75);
 18:         Student s2 = new Student("A00000002", "Tudiaunt, A.", 60);
 19:         Student stu = null;     // so no complaint about "not initialized"
 20:         String aNumber, name;
 21:         int pct;

 23:         // show info for first two students
 24:         System.out.println("\nFirst Student:");
 25:         printStudentRecord(s1);
 26:         System.out.println("\nSecond Student:");
 27:         printStudentRecord(s2);
 28:         pause(kbd);

 30:         // Change some info for s1
 31:         s1.setName("Dent, Stuart");
 32:         s2.setPctGrade(90);

 34:         // show revised information
 35:         System.out.println("\nRevised information for the first student:");
 36:         s1.printRecord();
 37:         System.out.println("\nRevised information for the second student:");
 38:         s2.printRecord();
 39:         pause(kbd);

 41:         // read student numbers until user signals to quit
 42:         System.out.print("Enter an A-Number or leave blank to quit: ");
 43:         aNumber = kbd.nextLine().toUpperCase();
 44:         while (aNumber.startsWith("A")) {
 45:             // read student info
 46:             System.out.println();
 47:             System.out.print("Enter student name: ");
 48:             name = kbd.nextLine();
 49:             System.out.print("Enter percent grade: ");
 50:             pct = kbd.nextInt();
 51:             kbd.nextLine();

 53:             // check for invalid grades!
 54:             while (!Student.isValidGrade(pct)) {
 55:                 System.out.println("That's not a valid grade!");
 56:                 System.out.println("Valid grades are between "
 57:                         + Student.MIN_GRADE + " and " + Student.MAX_GRADE
 58:                         + " (inclusive).");
 59:                 System.out.print("Enter percent grade: ");
 60:                 pct = kbd.nextInt();
 61:                 kbd.nextLine();
 62:             }

 64:             // create and print student information
 65:             stu = new Student(aNumber, name, pct);
 66:             System.out.println("\nHere is your student: ");
 67:             stu.printRecord();

 69:             // get next student’s number
 70:             System.out.println();
 71:             System.out.print("Enter an A-Number or leave blank to quit: ");
 72:             aNumber = kbd.nextLine().toUpperCase();
 73:         }
 74:         pause(kbd);

 76:         // report last student read (if any)
 77:         if (stu != null) {
 78:             // can only print the Student's record if there's a Student there!
 79:             System.out.println("The last student you entered was " + stu);
 80:             stu.printRecord();
 81:             pause(kbd);
 82:         }
 83:     }

 85:     /**
 86:      * Print a report on a given student.  Can be replaced by a call to the
 87:      * Student method printRecord.
 88:      *
 89:      * @param s the Student to print the record of.
 90:      */
 91:     public static void printStudentRecord(Student s) {
 92:         System.out.println(""
 93:                 + "\n\tNumber: " + s.getANumber()
 94:                 + "\n\tName:   " + s.getName()
 95:                 + "\n\tGrade:  " + s.getPctGrade()
 96:                 + "\n\tLetter: " + s.getLetterGrade());
 97:     }

 99:     /**
100:      * Prompt the user and wait for them to press the enter key.
101:      * The input stream must be empty.
102:      *
103:      * @param kbd the stream to read the enter key from.
104:      */
105:     public static void pause(Scanner kbd) {
106:         System.out.print("\nPress Enter...");
107:         kbd.nextLine();
108:         System.out.println();
109:     }

111: }