In: Computer Science
Code an application program that keeps tracks of
srudent information at your college:names, identification numbers,
and grade point averages in a fully encapsulated (homogenous)
sorted array-based data structure. When launched, the user will be
asked to input the maximum size of data set, the initial number of
students, and the initial data set. once this is complete, the user
will be presented with the following menu:
Enter:1 to insert a new student's info
2 to fetch and out put a student's info
3 to delete student info
4 to update student info
5 to output all the student info in sorted order
6 to exit program
note: please use Java language to create this program and please
write comments for each statement that what is happening. i know
there are same questions solution posted in chegg, but i want some
unique from others
I have created two file as below and mentioned inline comments as well. Do let me know in case of any doubt.
Sorting of array is done in switch-case statements whenever there is update/delete/insertion in studentList array.
Student.java
public class Student{
Integer id;
String name;
double grade;
public Student(Integer id, String name, double grade) {
this.id = id;
this.name = name;
this.grade = grade;
}
public Student() {
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", grade=" + grade +
'}';
}
}
Screenshot:
StudentDB.java
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class StudentDB {
static Student[] studentList;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter max size of data set: ");
int maxSize = sc.nextInt();
studentList = new Student[maxSize];
System.out.print("Enter no of students: ");
int students = sc.nextInt();
System.out.print("Enter space separated student details: ");
sc.nextLine();// to consume enter character which is left in input stream by nextInt()
int i = 0;
while (i < students) {
String line[] = sc.nextLine().split(" ");
studentList[i] = new Student(Integer.parseInt(line[0]), line[1], Double.parseDouble(line[2]));
i++;
}
Arrays.sort(studentList, 0, i, Comparator.comparing(obj -> obj.id));
System.out.println("Menu:");
System.out.println("Enter:1 to insert a new student's info\n" +
"2 to fetch and out put a student's info\n" +
"3 to delete student info\n" +
"4 to update student info\n" +
"5 to output all the student info in sorted order\n" +
"6 to exit program");
int choice = sc.nextInt();
while (choice >= 1 && choice <= 6) {
switch (choice) {
case 1:
if (i < maxSize) {
System.out.print("Enter space separated student details: ");
sc.nextLine(); // to consume enter character
String line[] = sc.nextLine().split(" ");
studentList[i] = new Student(Integer.parseInt(line[0]), line[1], Double.parseDouble(line[2]));
i++;
Arrays.sort(studentList, 0, i, Comparator.comparing(obj -> obj.id));
} else System.out.println("Student list is full. New students can't be stored.");
break;
case 2:
System.out.print("Enter id to search");
int searchId = sc.nextInt();
for (int l = 0; l < i; l++) {
if (studentList[l].id.equals(searchId))
System.out.println(studentList[l]); // toString() will be used to print details
}
break;
case 3:
System.out.print("Enter id to be deleted: ");
int delId = sc.nextInt();
int j = 0, index = -1;
while (j < i) {
if (studentList[j].id.equals(delId)) {
index = j; // store the index where student is found
break;
}
j++;
}
if (index == -1) System.out.println("Student id not found");
else {
j = index;
//move all student obj one index back from index where the student to be deleted is found
while (j < i - 1) {
studentList[j] = studentList[j + 1];
j++;
}
i--; // decrement i as one student is deleted
System.out.println("Student deleted.");
}
Arrays.sort(studentList, 0, i, Comparator.comparing(obj -> obj.id));
break;
case 4:
System.out.print("Enter student details to be updated with id: ");
sc.nextLine(); // to consume enter character
String line[] = sc.nextLine().split(" ");
Student updstudent = new Student(Integer.parseInt(line[0]), line[1], Double.parseDouble(line[2]));
int updId = Integer.parseInt(line[0]);
// loop over students and update when id matches
for (int k = 0; k < i; k++) {
if (studentList[k].id.equals(updId))
studentList[k] = updstudent;
}
Arrays.sort(studentList, 0, i, Comparator.comparing(obj -> obj.id));
break;
case 5:
//print all the student records
for (int m=0;m<i;m++) {
System.out.println(studentList[m]); // toString() will be used to print details
}
break;
case 6:
System.exit(0);
default:
System.err.println("Wrong choice.");
}
System.out.print("Enter choice: ");
choice = sc.nextInt();
}
}
}
Screenshot:
Output: