Lab 7 1. Develop a simple student management program for a class that can help t
ID: 3606317 • Letter: L
Question
Lab 7 1. Develop a simple student management program for a class that can help the instructor manage the information of all students. You need to design a Class representing a student that includes relevant data fields including as first name, last name, SIU ID, an Your program should allow the instructor to do the following operations: d score. Suppose the class has at most 30 students. a) Whenever your program starts, it first reads a file created by you, named "students.txt", which contains the information of students in the class. contains the information of one student. Suppose each row of the file b) List the information of all students one row by one row c) List the information of a student. The program will ask the instructor to input the first and last name of the student. If such a student exists, wl list the information of the student. Otherwise, a prompt will be printed to the instructor saying 'not exist'. d) Modify the information of a student. The program will ask the instructor to input the first and last name of the student. If such a student exists, it will further ask the instructor to further input other information of the student. Otherwise, a prompt will be printed to the instructor saying not exist. e) Write updated student information to the "students.txt" file. Each row of the file should contain the information of one student. The above process repeats until the user selects exit. Before exiting the program, the function for requirement d) will be called to write updated student information to the “students.txt., fileExplanation / Answer
import java.io.*;
import java.util.*;
import java.lang.*;
//Student Class
class Student {
String firstName;
String lastName;
int SIU_ID;
int Score;
//Constructor to define Student Information
public Student(String firstName,String lastName,int SIU_ID,int Score){
this.firstName = firstName;
this.lastName = lastName;
this.SIU_ID = SIU_ID;
this.Score = Score;
}
//Method to print Student Information
public void printStudent(){
System.out.println("Student Details");
System.out.println("FirstName: "+this.firstName);
System.out.println("LastName: "+this.lastName);
System.out.println("SIU_ID: "+this.SIU_ID);
System.out.println("Score: "+this.Score);
}
//Method to modify Student Information
public void modify(int SIU_ID,int Score){
this.SIU_ID = SIU_ID;
this.Score = Score;
}
}
public class Main {
//Scanner class calling to take user input
static Scanner scan = new Scanner(System.in);
//Method to check whether student is present or not with given firstname and lastname
public static int checkStudent(String firstname, String lastname, Student[] students){
for(int i=0;i<30;i++){
try {
//Checks student firstname and lastname from all students Information
if(students[i].firstName.equals(firstname)&&students[i].lastName.equals(lastname)){
return i;
}
//We need handle NullPointerException because we may not define whole 30 student details.
} catch (NullPointerException e) {
break;
}
}
return -1;
}
//Method to seach Student details
public static void searchStudent(Student[] students){
System.out.print("Enter firstname of the student: ");
String fN = scan.next();
System.out.print("Enter lastname of the student: ");
String lN = scan.next();
int i = checkStudent(fN,lN,students);
if(i!=-1){
students[i].printStudent();
}
else {
System.out.println("not exist");
}
}
//Method to modify Student Information
public static void modifyStudent(Student[] students){
System.out.print("Enter firstname of the student: ");
String fN = scan.next();
System.out.print("Enter lastname of the student: ");
String lN = scan.next();
//First check whether student present or not
int i = checkStudent(fN,lN,students);
if(i!=-1){
System.out.print("Enter SIU_ID: ");
int SIU_ID = scan.nextInt();
System.out.print("Enter Score: ");
int Score = scan.nextInt();
students[i].modify(SIU_ID,Score);
System.out.println("Student Information updated");
}
else {
System.out.println("not exist");
}
}
public static void main(String[] args) {
Scanner scanner;
//Array of Student objects for local storage
Student students[] = new Student[30];
int student_number = 0;
try {
//Opening students.txt file and reading data
scanner = new Scanner( new File("students.txt") );
while(scanner.hasNextLine()){
String details[] = scanner.nextLine().split(" ");
//Storing file data to students objects
students[student_number] = new Student(details[0],details[1],Integer.parseInt(details[2]),Integer.parseInt(details[3]));
student_number++;
}
//Giving Menu for User
System.out.println("Menu:");
System.out.print("1.Search Student 2.Modify Student Information 3.Exit Option is :");
int option = scan.nextInt();
while(option!=3) {
if(option==1){
searchStudent(students);
}
else if(option==2){
modifyStudent(students);
}
else if(option==3){
break;
}
else {
System.out.println("Enter Correct Option");
}
System.out.println("Menu:");
System.out.print("1.Search Student 2.Modify Student Information 3.Exit Option is :");
option = scan.nextInt();
}
scanner.close();
try {
//Opening again students.txt to write local updated data to file
BufferedWriter out = new BufferedWriter(new FileWriter("students.txt"));
for(int i=0;i<30;i++){
try {
//Writing student updated Information to file
out.write(students[i].firstName+" "+students[i].lastName+" "+students[i].SIU_ID+" "+students[i].Score+" ");
//We need handle NullPointerException because we may not define whole 30 student details.
} catch (NullPointerException e) {
break;
}
}
out.close();
//We need to handle FileNotFoundException while opening file
} catch (FileNotFoundException e) {
System.out.println("File not available to open");
//We need to hand IOException while opening file for writing mode
} catch (IOException e){
System.out.println(e);
}
//We need to handle FileNotFoundException while opening file
} catch (FileNotFoundException e) {
System.out.println("File not available to open");
}
}
}
/* sample students.txt file data
~~~~~~~~~~~~~~~~~
prakash k 2 432
ranjith a 32 300
~~~~~~~~~~~~~~~~~~
*/
/* Sample Output Simulation
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.