27. Code Java application program that keeps track of student information at you
ID: 3722056 • Letter: 2
Question
27. Code Java application program that keeps track of student information at your college (see Exercise 25). Include their names, identification numbers, and grade point averages in a fully encapsulated, homogeneous singly linked list. When launched, the user will be asked to input 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, 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. If the user requests an operation on a node not in the structure, the program output should be “node not in structure.” Otherwise, the message “operation complete” should be output.
Here is the info from exercise 25:
package Listing;
import java.util.Scanner;
public class Listing
{
private String name;
private String number;
private String gpa;
public Listing()
{
this.name = "";
this.number = "";
this.gpa = "";
}
public Listing(String name, String number, String gpa)
{
this.name = name;
this.number = number;
this.gpa = gpa;
}
public String toString()
{
return ("Name is " + name + " Number is " + number + " GPA is " + gpa);
}
public Listing deepCopy()
{
Listing clone = new Listing(name, number, gpa);
return clone;
}
public int compareTo(String targetKey)
{
return(name.compareTo(targetKey));
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getNumber()
{
return number;
}
public void setNumber(String number)
{
this.number = number;
}
public String getGPA()
{
return gpa;
}
public void setGPA(String gpa)
{
this.gpa = gpa;
}
public void input()
{
Scanner userInput = new Scanner(System.in);
System.out.println("Enter the name: ");
name = userInput.nextLine();
System.out.println("Enter the number: ");
number = userInput.nextLine();
System.out.println("Enter a GPA:");
gpa = userInput.nextLine();
}
public static void main(String[] args)
{
Listing List = new Listing();
List.input();
System.out.println(List.toString());
List.deepCopy();
System.out.println("Info has been added");
}
}
Explanation / Answer
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Listing {
private String name;
private String number;
private String gpa;
public Listing() {
this.name = "";
this.number = "";
this.gpa = "";
}
public Listing(String name, String number, String gpa) {
this.name = name;
this.number = number;
this.gpa = gpa;
}
public String toString() {
return ("Name is " + name + " Number is " + number + " GPA is " + gpa);
}
public Listing deepCopy() {
Listing clone = new Listing(name, number, gpa);
return clone;
}
public int compareTo(String targetKey) {
return (name.compareTo(targetKey));
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getGPA() {
return gpa;
}
public void setGPA(String gpa) {
this.gpa = gpa;
}
public void input() {
Listing listing = new Listing();
Scanner userInput = new Scanner(System.in);
System.out.println("Enter the name: ");
name = userInput.nextLine();
System.out.println("Enter the number: ");
number = userInput.nextLine();
System.out.println("Enter a GPA:");
gpa = userInput.nextLine();
}
public static void main(String[] args) {
List<Listing> studentList = new ArrayList();
System.out.println("Enter the initial number of students:");
Scanner sc = new Scanner(System.in);
int numberOfStudents = sc.nextInt();
for (int i = 0; i < numberOfStudents; i++) {
Listing listing = new Listing();
System.out.println("Enter the name: ");
listing.name = sc.next();
System.out.println("Enter the number: ");
listing.number = sc.next();
System.out.println("Enter a GPA:");
listing.gpa = sc.next();
studentList.add(listing);
}
int choice;
do {
System.out.println("Enter: 1 to insert a new student's information");
System.out.println("2 to fetch and output a student's information,");
System.out.println("3 to delete a student's information,");
System.out.println("4 to update a student's information,");
System.out.println("5 to output all the student information, and");
System.out.println("6 to exit the program.");
System.out.println("Enter your choice");
choice = sc.nextInt();
switch (choice) {
case 1:
Listing listing = new Listing();
System.out.println("Enter the name: ");
listing.name = sc.next();
System.out.println("Enter the number: ");
listing.number = sc.next();
System.out.println("Enter a GPA:");
listing.gpa = sc.next();
studentList.add(listing);
System.out.println("operation complete");
break;
case 2:
System.out.println("Enter the number to fetch the student info");
String number = sc.nextLine();
boolean found = false;
for (Listing student : studentList) {
if (student.getNumber() == number) {
found = true;
System.out.println(student);
System.out.println("operation complete");
}
}
if (found == false) {
System.out.println("node not in structure");
}
break;
case 3:
System.out.println("Enter the number to delete the student info");
String num = sc.nextLine();
boolean exist = false;
for (Listing student : studentList) {
if (student.getNumber() == num) {
exist = true;
studentList.remove(student);
System.out.println("operation complete");
}
}
if (exist == false) {
System.out.println("node not in structure");
}
break;
case 4:
System.out.println("Enter the number to update the student info");
String studentNumber = sc.nextLine();
boolean isThere = false;
for (Listing student : studentList) {
if (student.getNumber() == studentNumber) {
isThere = true;
System.out.println("Enter the name: ");
student.name = sc.next();
System.out.println("Enter the number: ");
student.number = sc.next();
System.out.println("Enter a GPA:");
student.gpa = sc.next();
System.out.println("operation complete");
}
}
if (isThere == false) {
System.out.println("node not in structure");
}
break;
case 5:
System.out.println("Student Details are:");
for (Listing student : studentList) {
System.out.println(student);
}
System.out.println("operation completed");
break;
case 6:
System.exit(0);
}
} while (choice != 6);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.