Professional Documents
Culture Documents
Source Code
StudentRec Class
package FinalReq;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.text.DecimalFormat;
// method that gets student's summary of grades from the file then display
the contents
public static void StudentGrades(){
System.out.println("Math" + "\t\t\t" + "English" + "\t\t\t" + "Science" +
"\t\t\t" + "Average");
// for each loop. each data from records is stored on identifier record.
// Both are of type StudentRec
for (StudentRec s:students){
System.out.print(s.getMath() + "\t\t");
System.out.print(s.getEng() + "\t\t");
System.out.print(s.getSci() + "\t\t");
System.out.print(s.getAve() + "\t\t");
}
}//end of method
/* method which reads data from the opened text file - filename paramater
uses BufferedReader */
public static void readFile(String filename){
BufferedReader bread = null;//initializes bread identifier as
BufferedReader
try{
// instantiates printW as PrintWriter and FileWriter
printW = new PrintWriter(new FileWriter(filename));
try{
/* for each loop, each data from records is written
to paramater studentRecords */
for (StudentRec s : students){
printW.println(s.getID());
printW.println(s.getName());
printW.println(s.getAge());
printW.println(s.getAddress());
printW.println(s.getMath());
printW.println(s.getEng());
printW.println(s.getSci());
printW.println(s.getAve());
}
saved = true;
} finally{
printW.close();
}
} catch (IOException e){
e.printStackTrace();
}
return saved;
}//end of method
}while(choice.equalsIgnoreCase("Y"));//end of loop
}//end of method
if(!isFound){
System.out.println("\nStudent ID does not exist");
}
if (!isFound){
System.out.println("\nStudent ID does not exist");
}
else{
// accepts data input for editing
try{
System.out.print("Enter New Student No./ID : ");
sID = input.readLine();
System.out.print("Enter New Student Name : ");
sName = input.readLine();
System.out.print("Enter New Age : ");
age = Integer.parseInt(input.readLine());
System.out.print("Enter New Address : ");
sAdd = input.readLine();
System.out.print("Enter New Math Grade : ");
math = Double.parseDouble(input.readLine());
System.out.print("Enter New English Grade : ");
eng = Double.parseDouble(input.readLine());
System.out.print("Enter New Science Grade : ");
sci = Double.parseDouble(input.readLine());
}catch (IOException e){
System.out.print("ERROR!");
}
// will ask the user if they want to repeat the process or terminate
if no
System.out.print("Edit Another [Y/N]? ");
choice = sc.nextLine();
}while(choice.equalsIgnoreCase("Y"));//end of loop
}//end of method
isFound = true;
break;
}
}//end of loop
if (!isFound){
System.out.println("\nStudent ID does not exist!");
}else{
// prompts user if he wants to delete the record
String delete = " ";
System.out.print("Are you sure you want to delete this record
[Y/N]? ");
try{
delete = input.readLine();
}catch (IOException e){
System.out.println("ERROR!");
}
if(delete.equalsIgnoreCase("Y")){
students.remove(pos);
}
}
// calls the methods chosen by the user then uses the ArrayList
students
// as the paramater
case 1:
addStudent(students);
break;
case 2:
searchStudent(students);
break;
case 3:
editStudent(students);
break;
case 4:
deleteStudent(students);
break;
case 5:
viewGrades(students);
break;
case 6:
// terminates loop
System.out.println("Thank you for using!");
System.exit(0);
default:
System.out.println("Not Available!");
break;
}
}// end of main
}// end of class
Output
Terminate Program
Incorrect Input