Question
In java , Provide options to save and load the file that stores two Map objects in the code below: import java.util.*; public class Test
In java , Provide options to save and load the file that stores two Map objects in the code below:
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int choice = 0;
Map
Map
Set
while(choice!= 5)
{
printMenuAndGetChoice();
choice = input.nextInt();
if(choice==1)
{
add(student,grade);
}
if(choice==2)
{
remove(student,grade);
}
if(choice==3)
{
modify(student,grade);
}
if(choice==4)
{
print(grade);
}
if(choice==5)
{
System.out.println("Have a nice day!");
}
}
}
public static void add(Map
{
Scanner input = new Scanner(System.in);
System.out.println("You will now be enrolling a student!");
System.out.println("Please enter first name of student: ");
String firstName = input.next();
System.out.println("Please enter last name of student: ");
String lastName = input.next();
System.out.println("Please enter ID number of student: " );
int idNum = input.nextInt();
System.out.println("Enter the student's grade(A-F): ");
String grade = input.next();
System.out.println("Student " + firstName+ " " + lastName+ " " + idNum + " has been added.");
while(StudentMap.containsKey(idNum))
{
System.out.println("Error Student with ID already exists");
System.out.println("Please try again!");
idNum=input.nextInt();
}
Student s = new Student(firstName,lastName,idNum);
StudentMap.put(s.getID(),s);
GradeMap.put(s, grade);
}
/**
* prints the menu and choices
*/
public static void printMenuAndGetChoice()
{
System.out.println("1. Enroll a Student");
System.out.println("2. Expell a Student");
System.out.println("3. Change grade");
System.out.println("4. Display Student Body");
System.out.println("5. Exit");
}
public static void remove(Map
{
Scanner input = new Scanner(System.in);
System.out.println("Which student would you like to expell?(Please enter ID Number)");
int idNum = input.nextInt();
GradeMap.remove(StudentMap.get(idNum));
StudentMap.remove(idNum);
}
public static void modify(Map
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter the ID number of the student you will be modifying: ");
int idNum = input.nextInt();
while (!StudentMap.containsKey(idNum))
{
System.out.println("Student doesn't exist!");
System.out.println("Please enter ID Num");
idNum=input.nextInt();
}
System.out.println("What is the new grade?");
String nGrade = input.next();
GradeMap.put(StudentMap.get(idNum),nGrade);
}
public static void print(Map
{
Set
for(Student s: studentList)
{
System.out.println(s.toString()+ " " + GradeMap.get(s));
}
}
}
------------------------------------------------------------------------------------------------------
public class Student implements Comparable
{
String fname;
String lname;
int id;
public Student(String f,String l,int i)
{
fname = f;
lname = l;
id = i;
}
public String getFname()
{
return fname;
}
public String getLname()
{
return lname;
}
public int getID()
{
return id;
}
public void setFname(String n)
{
fname = n;
}
public void setLname(String l)
{
lname = l;
}
public void setID(int i)
{
id = i;
}
public String toString()
{
return fname + " " + lname+ " " + id;
}
@Override
public int compareTo(Student s) {
// TODO Auto-generated method stub
if (this.lname.compareTo(s.lname) == 0) {
if (this.fname.compareTo(s.fname) == 0) {
return this.id-s.id;
}
return this.fname.compareTo(s.fname);
}
return this.lname.compareTo(s.lname);
}
public boolean equals(Student s)
{
if(fname.equals(s.fname) && lname.equals(s.lname) && id==s.id)
{
return true;
}
return false;
}
public int hashCode() {
final int HASH_MULTIPLIER = 29;
int h = HASH_MULTIPLIER * fname.hashCode() + lname.hashCode();
h = HASH_MULTIPLIER * h + ((Integer) id).hashCode();
return h;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started