In: Computer Science
Code an application program that keeps track of student information at your college: names, identification numbers, and grade point averages in a fully encapsulated (homogeneous) Sorted array-based data structure. When launched, the user will be asked to input the maximum size of the 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 information. 2 to fetch and output a student’s information. 3 to delete a student’s information. 4 to update a student’s information. 5 to output all the student information in sorted order, and 6 to exit the program. The program should perform an unlimited number of operations until the user enters a 6 to exit the program.
Just need some help
Data Structures and Algorithms Using Java. McAllister.
// Student.java
public class Student {
private int id;
private String name;
private char gender;
public Student(int myid, String myName, char myGender){
this.id = myid;
this.name = myName;
this.gender = myGender;
}
public void setId(int myId){
this.id = myId;
}
public void setName(String myName){
this.name = myName;
}
public void setGender(char myGender){
this.gender = myGender;
}
public int getId(){
return this.id;
}
public String getName(){
return this.name;
}
public char getGender(){
return this.gender;
}
@Override
public String toString() {
return this.id + "\t" + this.name + "\t\t" + this.gender;
}
}
// StudentOperation.java
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class StudentOperation implements
Comparator<Student> {
String name;
int noOfStudents;
int maxStudent = 5;
Student student[];
public StudentOperation(int noOfStudents){
this.name = name;
this.noOfStudents = noOfStudents;
this.student = new Student[noOfStudents];
}
public void add(){
int id;
String name;
char gender;
Scanner input = new Scanner(System.in);
int i = 0;
while(true){
System.out.println("Enter an student id or -1 to stop:");
id = input.nextInt();
if(id == -1){
break;
}
System.out.println("Enter student name:");
name = input.next();
System.out.println("Enter student gender:(M/F)");
gender = input.next().charAt(0);
if(i < this.noOfStudents){
this.student[i] = new Student(id, name, gender);
}else{
System.out.println("Sorry, the data set reached it's capacity of
students.");
}
i++;
}
}
public void print() {
System.out.println("Students details: ");
Arrays.sort(this.student, new
StudentOperation(this.noOfStudents));
if(this.student.length > 0){
System.out.println("id\tname\t\ttgender");
for(int i = 0; i < this.student.length; i++){
if(this.student[i] != null){
System.out.println(this.student[i].toString());
}
}
}else{
System.out.println("No student found");
}
}
public void remove() {
Scanner input = new Scanner(System.in);
System.out.println("Enter the id of the student you want to
remove");
int id = input.nextInt();
String name = null;
Student[] arrayData = new Student[this.noOfStudents];
for(int i = 0, k = 0; i < this.student.length; i++){
if(this.student[i] != null){
if(this.student[i].getId() == id){
name = this.student[i].getName();
continue;
}
arrayData[k++] = this.student[i];
}
}
this.student = arrayData;
if(name != null)
System.out.println("Student " + name + " has been removed.");
else
System.out.println("Sorry the student with id "+ id +" does not
exist and cannot be removed.");
}
public void update() {
Scanner input = new Scanner(System.in);
System.out.println("Enter the id of the student you want to
update");
int id = input.nextInt();
String name = null;
char gender;
Student[] arrayData = new Student[this.noOfStudents];
for(int i = 0, k = 0; i < this.student.length; i++){
if(this.student[i] != null){
if(this.student[i].getId() == id){
System.out.println("Enter the updated name: ");
name = input.next();
this.student[i].setName(name);
System.out.println("Enter the updated gender: ");
gender = input.next().charAt(0);
this.student[i].setGender(gender);
break;
}
}
}
if(name != null)
System.out.println("Student with id "+ id +" has been
update.");
else
System.out.println("Sorry the student with id "+ id +" does not
exist and cannot be removed.");
}
public void search() {
Scanner input = new Scanner(System.in);
System.out.println("Enter the id of the student you want to
search");
int id = input.nextInt();
System.out.println("id\tname\tgender");
for(int i = 0; i < this.student.length; i++){
if(this.student[i] != null && this.student[i].getId() ==
id){
System.out.println(this.student[i].toString());
}
}
}
@Override
public int compare(Student a, Student b) {
return a.getId() - b.getId();
}
}
// StudentTest.java
import java.util.Scanner;
public class StudentTest {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int noOfStudents = 5;
System.out.println("Input the maximum size of the data
set:");
noOfStudents = input.nextInt();
int option;
StudentOperation s = new StudentOperation(noOfStudents);
do{
System.out.println("Choose one of the following:");
System.out.println("----------------------------");
System.out.println("1) Insert a new student's information");
System.out.println("2) Fetch and output a student's
information");
System.out.println("3) Delete a student's information");
System.out.println("4) Update a student's information");
System.out.println("5) Output all the student information in sorted
order");
System.out.println("6) Exit");
option = input.nextInt();
switch(option){
case 1:
s.add();
break;
case 2:
s.search();
break;
case 3:
s.remove();
break;
case 4:
s.update();
break;
case 5:
s.print();
break;
}
}while(option < 6);
System.out.println("Thank you for using the program");
}
}
Output:
PS D:\xampp\htdocs\example\Student> java StudentTest
Input the maximum size of the data set:
5
Choose one of the following:
----------------------------
1) Insert a new student's information
2) Fetch and output a student's information
3) Delete a student's information
4) Update a student's information
5) Output all the student information in sorted order
6) Exit
1
Enter an student id or -1 to stop:
3
Enter student name:
student 3
Enter student gender:
Enter an student id or -1 to stop:
-1
Choose one of the following:
----------------------------
1) Insert a new student's information
2) Fetch and output a student's information
3) Delete a student's information
4) Update a student's information
5) Output all the student information in sorted order
6) Exit
3
Enter the id of the student you want to remove
3
Student student has been removed.
Choose one of the following:
----------------------------
1) Insert a new student's information
2) Fetch and output a student's information
3) Delete a student's information
4) Update a student's information
5) Output all the student information in sorted order
6) Exit
1
Enter an student id or -1 to stop:
3
Enter student name:
student3
Enter student gender:
F
Enter an student id or -1 to stop:
5
Enter student name:
asdad
Enter student gender:
M
Enter an student id or -1 to stop:
8
Enter student name:
sewr
Enter student gender:
M
Enter an student id or -1 to stop:
4
Enter student name:
fsdf
Enter student gender:
M
Enter an student id or -1 to stop:
9
Enter student name:
sdfsf
Enter student gender:
F
Enter an student id or -1 to stop:
7
Enter student name:
rwwe
Enter student gender:
F
Sorry, the data set reached it's capacity of students.
Enter an student id or -1 to stop:
-1
Choose one of the following:
----------------------------
1) Insert a new student's information
2) Fetch and output a student's information
3) Delete a student's information
4) Update a student's information
5) Output all the student information in sorted order
6) Exit
5
Students details:
id name gender
3 student3 F
4 fsdf M
5 asdad M
8 sewr M
9 sdfsf F
Choose one of the following:
----------------------------
1) Insert a new student's information
2) Fetch and output a student's information
3) Delete a student's information
4) Update a student's information
5) Output all the student information in sorted order
6) Exit
4
Enter the id of the student you want to update
9
Enter the updated name:
test
Enter the updated gender:
F
Student with id 9 has been update.
Choose one of the following:
9 test F
Choose one of the following:
----------------------------
1) Insert a new student's information
2) Fetch and output a student's information
3) Delete a student's information
4) Update a student's information
5) Output all the student information in sorted order
6) Exit
6
Thank you for using the program
Note: Instruction to be followed
Firstly compile all the files like below:
javac Student.java
javac StudentOperation.java
javac StudentTest.java
Run program using
java StudentTest
If you need anything more on this please add comment below. I'll reply ASAP.