You are on page 1of 14

Group 5

Leader: Magaling, Paul Vincent A.


Members: Casas, Wiljoe
Caalem, John Kyle
Pagsuguiron, Vince Kyle

Source Code

StudentRec Class

package FinalReq;

/* StudentRec class illustrate adding, viewing, deleting of records to and from


a text needed for StudentRecordSystem */
public class StudentRec{
private String studentID;
private String name;
private int age;
private String address;
private double mathG;
private double englishG;
private double sciG;
private double avG;

/* Assigning the value of the paramaters of StudentRecordSystem to the


instance variable of StudentRec */
public StudentRec(String id, String studentName, int studentAge, String
address, double math, double english, double science, double ave){
this.studentID = id;
this.name = studentName;
this.age = studentAge;
this.address = address;
this.mathG = math;
this.englishG = english;
this.sciG = science;
this.avG = ave;
}

//initializing the values to the getters


public String getName(){
return name;
}

public String getID(){


return studentID;
}

public int getAge(){


return age;
}

public String getAddress(){


return address;
}
public double getMath(){
return mathG;
}

public double getEng(){


return englishG;
}

public double getSci(){


return sciG;
}

public double getAve(){


return avG;
}

// Assigning the paramaters to be used by the setters


public void setID(String id){
this.studentID = id;
}

public void setName(String name){


this.name = name;
}

public void setAge(int sAge){


this.age = sAge;
}

public void setAddress(String add){


this.address = add;
}

public void setMath(double math){


this.mathG = math;
}

public void setEng(double eng){


this.englishG = eng;
}

public void setSci(double sci){


this.sciG = sci;
}

public void setAvg(double avrg){


this.avG = avrg;
}
}// end of class
Main class StudentRecordSystem
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;

public class StudentRecordSystem {


// declaring student as the ArrayList
private static List<StudentRec>students = new ArrayList<StudentRec>();
// method which gets data from students and displays the content
public static void RecordList(){
System.out.println("Student ID" + "\t\t" + "Name"+ "\t\t" + "Age" +
"\t\t" + "Address" + "\t\t" + "Math" + "\t\t" + "English" + "\t\t" + "Science" +
"\t\t" + "Average\n");
// 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.getID() + "\t\t\t");
System.out.print(s.getName() + "\t\t");
System.out.print(s.getAge() + "\t\t");
System.out.print(s.getAddress() + "\t\t");
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() + "\n");
}
}//end of method

// 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

students.clear(); //clear all elements in the students arraylist


try{
// instantiates br as FileReader with studentRecords paramaeter
bread = new BufferedReader(new FileReader(filename));
try{
String name, add, id;
int age;
double math, eng, sci, ave;
/* reads each line through bread identifier, and stores it on
temporary identifiers */
// loop continues util null is encountered
while ((id = bread.readLine()) != null){
name = bread.readLine();
age = Integer.parseInt(bread.readLine());
add = bread.readLine();
math = Double.parseDouble(bread.readLine());
eng = Double.parseDouble(bread.readLine());
sci = Double.parseDouble(bread.readLine());
ave = Double.parseDouble(bread.readLine());

// adds the data to students arraylist


students.add(new StudentRec(id, name, age, add, math, eng,
sci, ave));
}
} finally{
bread.close(); //closes BufferedReader
}
} catch (IOException e){
e.printStackTrace();
}
}//end of method

// method which writes data into parameter 'filename'


// uses PrintWriter and FileWriter
public static boolean writeFile(String filename){
boolean saved = false;
PrintWriter printW = null; //printW is a PrintWriter identifier

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

private static final DecimalFormat df = new DecimalFormat("0.00");//sets


decimal places to two

// Student Record menu methods

public static void addStudent(List<StudentRec>students){


String sID = " ";
String sName = " ";
String sAdd = " ";
String choice = " ";
int age = 0;
double math = 0;
double eng = 0;
double sci = 0;
double avrg = 0;

Scanner sc = new Scanner(System.in);


BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));

//accepts data input


do{

System.out.print("\nEnter Student No./ID: ");


try{
sID = input.readLine();
}catch(IOException e){
System.out.println("ERROR!");
}
// calls readFile method using "studentRecords.txt" as file paramater
// StudentRec.txt should be existing.
readFile("studentRecords.txt");
boolean exist = false;
for (StudentRec s:students){
if(sID.equalsIgnoreCase(s.getID())){
System.out.println("Student ID entered already exist!");
exist = true;
break;
}
}
if(!exist){
try{
System.out.print("Enter Student's Name: ");
sName = input.readLine();
System.out.print("Enter Age: ");
age = Integer.parseInt(input.readLine());
System.out.print("Enter Address: ");
sAdd = input.readLine();
System.out.print("Enter Student's Grade in Math: ");
math = Double.parseDouble(input.readLine());
System.out.print("Enter Student's Grade in English: ");
eng = Double.parseDouble(input.readLine());
System.out.print("Enter Student's Grade in Science: ");
sci = Double.parseDouble(input.readLine());
}catch(IOException e){
System.out.println("ERROR!");
}
avrg = (math + eng + sci) / 3;
System.out.println("Average : " + avrg);
System.out.println("\nAdding Record...");

// writes the new added data to students ArrayList


students.add(new StudentRec(sID, sName, age, sAdd, math, eng,
sci, avrg));

// calls RecordList to display the list with the added record(s)


RecordList();

/* calls method writeFile with the file paramater


"studentRecords.txt" to save
data on text file */
if(writeFile("studentRecords.txt")){
System.out.println("\nStudent's data succesfully added!");
}
else{
System.out.println("\nProblem(s) encountered while adding
record.");
}
}
// ask the user if they want to repeat the process or terminate the
program if no.
System.out.print("Add Again [Y/N]? ");
choice = sc.nextLine();

}while(choice.equalsIgnoreCase("Y"));//end of loop
}//end of method

public static void searchStudent(List<StudentRec>students){


String sID = " ";
String choice = " ";

Scanner sc = new Scanner(System.in);


BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));

// accepts data input


do{
System.out.print("Enter Student No./ID : ");
try{
sID = input.readLine();
}catch (IOException e){
System.out.println("ERROR!");
}

// calls readFromFile method using "StudentRec.txt" as file parameter


// StudentRec.txt should be existing.
readFile("studentRecords.txt");

System.out.println("Student ID" + "\t\t" + "Name"+ "\t\t\t" + "Age" +


"\t" + "Address");

boolean isFound = false;//determines if the record is in the list or


not
for(StudentRec s : students){
if(sID.equalsIgnoreCase(s.getID())){
System.out.print(s.getID() + "\t\t\t");
System.out.print(s.getName() + "\t\t");
System.out.print(s.getAge() + "\t");
System.out.print(s.getAddress() + "\n");
isFound = true;
break;
}
}//end of loop

if(!isFound){
System.out.println("\nStudent ID does not exist");
}

System.out.print("View Another Record [Y/N]? ");


choice = sc.nextLine();
}while(choice.equalsIgnoreCase("Y"));
}

public static void editStudent(List<StudentRec>students){


String sID = " ";
String sName = " ";
String sAdd = " ";
String choice = " ";
int age = 0;
int pos = -1;//holds the index of the record found
double math = 0;
double eng = 0;
double sci = 0;
double avrg = 0;

Scanner sc = new Scanner(System.in);


BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
// accepts data input
do{
System.out.print("Enter Student's No./ID : ");
try{
sID = input.readLine();
}catch(IOException e){
System.out.println("ERROR!");
}
// calls readFromFile method using "StudentRec.txt" as file parameter
// StudentRec.txt should be existing.
readFile("studentRecords.txt");

boolean isFound = false;// determines if the record is in the list or


not
System.out.println("Student ID" + "\t\t" + "Name"+ "\t\t" + "Age" +
"\t\t" + "Address" + "\t\t" + "Math" + "\t\t" + "English" + "\t\t" + "Science" +
"\t\t" + "Average\n");
for(StudentRec s : students){
pos++;
if(sID.equalsIgnoreCase(s.getID())){
System.out.print(s.getID() + "\t\t\t");
System.out.print(s.getName() + "\t\t");
System.out.print(s.getAge() + "\t\t");
System.out.print(s.getAddress() + "\t\t");
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() + "\n");
isFound = true;
break;
}
}//end of loop

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!");
}

avrg = (math + eng + sci) / 3;


System.out.println("\nAverage : " + avrg);
System.out.println("\nAdding Record...\n");

// changes the content of the records in the position


// with the inputted data
students.set(pos,new StudentRec(sID, sName, age, sAdd, math, eng,
sci, avrg));

// displays the updated list


System.out.println("\t\t\t\tUpdated List of Students : ");
RecordList();

// writes the updated list to the text file


if (writeFile("studentRecords.txt")){
System.out.println("File Successfully Updated.");
}else{
System.out.println("Problems encountered while updating
file.");
}
}

// 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

public static void deleteStudent(List<StudentRec>students){


String sID = " ";
String choice = " ";
int pos = -1; //holds the index of the record found

Scanner sc = new Scanner(System.in);


BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));

// accepts data input


do{
System.out.print("Enter Student's No./ID : ");
try{
sID = input.readLine();
}catch(IOException e){
System.out.println("ERROR!");
}
// calls readFromFile method using "StudentRec.txt" as file parameter
// StudentRec.txt should be existing.
readFile("studentRecords.txt");
boolean isFound = false;//determines if the record is in the list or
not
// searches record from the array list
System.out.println("Student ID" + "\t\t" + "Name"+ "\t\t" + "Age" +
"\t\t" + "Address" + "\t\t" + "Math" + "\t\t" + "English" + "\t\t" + "Science" +
"\t\t" + "Average\n");
for(StudentRec s : students){
pos++;
if(sID.equalsIgnoreCase(s.getID())){
System.out.print(s.getID() + "\t\t\t");
System.out.print(s.getName() + "\t\t");
System.out.print(s.getAge() + "\t\t");
System.out.print(s.getAddress() + "\t\t");
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() + "\n");

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);
}
}

// displays the updated list


System.out.println("\t\t\tUpdated List of Students");
RecordList();
if(writeFile("studentRecords.txt")){
System.out.println("File Succesfully Updated.");
}else{
System.err.println("Problem Updating File.");
}
// asks the user if they want to repeat the process or terminate the
program if no
System.out.print("Delete another record [Y/N]? ");
choice = sc.nextLine();
}while(choice.equalsIgnoreCase("Y"));//end of loop
}//end of method

public static void viewGrades(List<StudentRec>students){


String sID = " ";
String choice = " ";

Scanner sc = new Scanner(System.in);


BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));

// acepts data input


do{
System.out.print("Enter Student's No./ID : ");
try{
sID = input.readLine();
}catch(IOException e){
System.out.println("ERROR!");
}

// calls readFromFile method using "StudentRec.txt" as file parameter


// StudentRec.txt should be existing.
readFile("studentRecords.txt");

boolean isFound = false;//determines if the record is in the list or


not

// searches the student id from the array list


System.out.println("\t\t\t\t\tSummary of Grades");
System.out.println("Student ID" + "\t\t" + "Name" + "\t\t" +"Math" +
"\t\t" + "English" + "\t\t" + "Science" + "\t\t" + "Average");
for (StudentRec s:students){
System.out.print(s.getID() + "\t\t");
System.out.print(s.getName() + "\t\t");
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() + "\n");
isFound = true;
break;
}
if (!isFound){
System.out.println("\nStudent ID does not exist");
}
System.out.print("View Another Summary of Grades [Y/N]? ");
choice = sc.nextLine();
}while(choice.equalsIgnoreCase("Y"));//end of loop
}//end of method

public static void main(String []args) throws IOException{


BufferedReader input = new BufferedReader(new
InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
int n;
System.out.println("\t\t\tMENU");
System.out.println("[1] Add Student Record");
System.out.println("[2] View Student Record");
System.out.println("[3] Edit Student Record");
System.out.println("[4] Delete Student Record");
System.out.println("[5] View Summary of Grades");
System.out.println("[6] Terminate Program");

System.out.print("\t\n Enter Choice: ");


n = sc.nextInt();
switch(n){

// 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

Add Student Record

View Student Record


Edit Student Record

Delete Student Record


View Summary of Grades

Terminate Program

Incorrect Input

You might also like