Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA PLEASE A .txt file basically need to be created and then saved too. Provide options to save and load the file that stores two

JAVA PLEASE

A .txt file basically need to be created and then saved too.

Provide options to save and load the file that stores two Map objects in the problem

//Here is the Main:

import java.util.*;

public class Main

{

public static void main(String[] args)

{

Scanner input = new Scanner(System.in);

int choice = 0;

Map student = new TreeMap();

Map grade = new TreeMap();

Set sHash = new HashSet();

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 StudentMap, MapGradeMap)

{

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 StudentMap, MapGradeMap)

{

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(MapStudentMap, MapGradeMap)

{

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(MapGradeMap)

{

Set studentList = GradeMap.keySet();

for(Student s: studentList)

{

System.out.println(s.toString()+ " " + GradeMap.get(s));

}

}

}

//Here is the Student.java Class

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)

{

if (this.lname.compareTo(s.lname) == 0) {// if last names are the same

if (this.fname.compareTo(s.fname) == 0) {//if first names are the same

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Principles Of Multimedia Database Systems

Authors: V.S. Subrahmanian

1st Edition

1558604669, 978-1558604667

More Books

Students also viewed these Databases questions

Question

7. How will you encourage her to report back on the findings?

Answered: 1 week ago

Question

Were the decisions based on appropriate facts?

Answered: 1 week ago

Question

Were the right people involved in the decision-making process?

Answered: 1 week ago