Code:
import java.util.*;
import java.io.*;
class Project10Driver
{
static int age, id, graduationYear, credLeft, apCred, SAT, examOne, examTwo, finalExam, gre;
static double gpa;
static String schoolName, name, degree;
static Scanner scan = new Scanner(System.in);
static int userChoice = 1;
static int counter = 0;
static Student[] ListOfStudents = new Student[50];
final static int LIMIT = 14;
static int ap, pTime;
static String major, minor;
static boolean time;
public static void main(String[]args) throws IOException
{
// the following 2 lines make it work with spaces in strings.
//String blah = System.getProperty("line.separator");
//scan.useDelimiter(blah);
while (userChoice != 0)
{
menu();
userChoice = scan.nextInt();
menuSwitch(userChoice);
}
}
public static void getInputFromFile()
{
String line = "";
System.out.print("Please enter the name of the file: ");
String fileName = scan.next();
try{
Scanner scan3 = new Scanner (new File(fileName));
while(scan3.hasNext()) {
line = scan3.nextLine();
if (line.equals("MIDDLE"))
{
name = scan3.nextLine();
schoolName = scan3.nextLine();
age = Integer.parseInt(scan3.nextLine());
id = Integer.parseInt(scan3.nextLine());
gpa = Integer.parseInt(scan3.nextLine());
graduationYear = Integer.parseInt(scan3.nextLine());
ListOfStudents[counter] = new MiddleSchool(name, age, schoolName, id, gpa, graduationYear);
counter++;
}
else if (line.equals("HIGH"))
{
name = scan3.nextLine();
schoolName = scan3.nextLine();
age = Integer.parseInt(scan3.nextLine());
id = Integer.parseInt(scan3.nextLine());
gpa = Integer.parseInt(scan3.nextLine());
graduationYear = Integer.parseInt(scan3.nextLine());
credLeft = Integer.parseInt(scan3.nextLine());
SAT = Integer.parseInt(scan3.nextLine());
apCred = Integer.parseInt(scan3.nextLine());
ListOfStudents[counter] = new HighSchool(name, age, schoolName, id, gpa, graduationYear, credLeft, SAT, apCred);
counter++;
}
else if (line.equals("UNDERGRAD"))
{
name = scan3.nextLine();
schoolName = scan3.nextLine();
age = Integer.parseInt(scan3.nextLine());
id = Integer.parseInt(scan3.nextLine());
gpa = Integer.parseInt(scan3.nextLine());
apCred = Integer.parseInt(scan3.nextLine());
major = scan3.nextLine();
minor = scan3.nextLine();
time = false;
examOne = Integer.parseInt(scan3.nextLine());
examTwo = Integer.parseInt(scan3.nextLine());
finalExam = Integer.parseInt(scan3.nextLine());
double[] activity = new double[LIMIT];
double[] exercise = new double[LIMIT];
double[] project = new double[LIMIT];
Scanner scan2 = new Scanner(scan3.nextLine());
for(int x = 0; x < LIMIT; x++)
activity[x] = Double.parseDouble(scan2.nextLine());
scan2 = new Scanner(scan3.nextLine());
for (int i = 0; i < LIMIT; i++)
exercise[i] = Double.parseDouble(scan2.next());
scan2 = new Scanner(scan3.nextLine());
for(int j = 0; j < LIMIT; j++)
project[j] = Double.parseDouble(scan2.next());
ListOfStudents[counter] = new Undergrad(name, age, schoolName, id, gpa, apCred, major, minor, time, examOne, examTwo, activity, exercise, project, finalExam);
counter++;
}
else if (line.equals("GRAD"))
{
name = scan3.nextLine();
schoolName = scan3.nextLine();
age = Integer.parseInt(scan3.next());
id = Integer.parseInt(scan3.next());
gpa = Integer.parseInt(scan3.next());
major = scan3.next();
minor = scan3.next();
time = false;
degree = scan3.next();
gre = Integer.parseInt(scan3.next());
ListOfStudents[counter] = new Graduate(name, age, schoolName, id, gpa, major, minor, time, gre, degree);
counter++;
}
else
System.out.println("Bad File");
//add to the array
}
}
catch(Exception e1)
{
System.err.println("File input error");
}
}
public static void menuSwitch(int choice)
{
int num;
switch(choice)
{
case 1:
MiddleSchool ms= new MiddleSchool();
ms.getUserInput();
ListOfStudents[counter] = ms;
counter++;
break;
case 2:
HighSchool hs = new HighSchool();
hs.getUserInput();
ListOfStudents[counter] = hs;
counter++;
break;
case 3:
Undergrad uGrad = new Undergrad();
uGrad.getUserInput();
ListOfStudents[counter] = uGrad;
counter++;
break;
case 4:
Graduate grad = new Graduate();
grad.getUserInput();
ListOfStudents[counter] = grad;
counter++;
break;
case 5:
System.out.print("Please enter index of student: ");
int index = scan.nextInt();
System.out.print("Please enter desired grade: ");
double desiredGrade = scan.nextDouble();
Undergrad ug = (Undergrad)ListOfStudents[index];
double finalGrade = ug.computeFinalExamGrade(desiredGrade);
System.out.println("To get a "+desiredGrade+" you need to score a "+finalGrade+" on the final exam");
break;
case 6:
for(int i = 0; i < ListOfStudents.length; ++i)
if(ListOfStudents[i] != null)
{
System.out.println();
System.out.println("Student #"+(i+1));
System.out.println(ListOfStudents[i]);
}
System.out.println();
break;
case 7:
getInputFromFile();
break;
default:
for(int i = 0; i < ListOfStudents.length; ++i)
if(ListOfStudents[i] != null){
System.out.println();
System.out.println("Student #"+(i+1));
System.out.println(ListOfStudents[i]);
}
System.out.println();
System.out.println("Goodbye");
break;
}
}
public static void menu()
{
System.out.println("0: Quit");
System.out.println("1: Middle School");
System.out.println("2: High School");
System.out.println("3: College - Undergraduate");
System.out.println("4: College - Graduate");
System.out.println("5: Compute final exam grade needed for a desired grade (undergraduate only) ");
System.out.println("6: Print");
System.out.println("7: Retrieve information for a file.");
System.out.print("\nEnter your choice: ");
}
}
and here is the file it should be retrieving
Bookmarks