Answered! You will create a class to keep student's information: name, student ID, and grade. The program will have the following functionality:…

You will create a class to keep student’s information: name, student ID, and grade. The program will have the following functionality:
– The record is persistent, that is, the whole registry should be saved on file upon exiting the program, and after any major change.
– The program should provide the option to create a new entry with a student’s name, ID, and grade.
– There should be an option to lookup a student from his student ID (this will be the key in the hash table).
– There should be an option to remove a particular entry by providing the student ID.
– There should be an option to display the whole registry, sorted by student ID.
– Remember that the registry must be persistent, so you will have to save all this information to the file system. You may use any of the versions of hash tables implemented in the previous exercise.

Here is my code for it so far, it might not be correct.

public class Student<String, V> extends HashTable<String, V> {
private V name;
private String id;
private int grade;

public V lookup() {
HashTable<String, V> registry = new HashTable<String, V>();
Scanner input = new Scanner(System.in);
System.out.print(“Please Enter the Student ID: “);
id = (String) input.next();
registry.lookup(id);
java.lang.String out = “The Student’s Name is ” + name;
return (V) out;
}

public V remove() {
HashTable<String, V> registry = new HashTable<String, V>();
Scanner input = new Scanner(System.in);
System.out.print(“Please Enter the Student ID: “);
id = (String) input.next();
super.remove(id);
java.lang.String out = “The Student That Has Been Removed is ” + name;
return (V) out;
}

public java.lang.String display() {
HashTable<String, V> registry = new HashTable<String, V>();
super.getSortedList(null);
System.out.println(something);
}

public static void main(java.lang.String[] args) {
HashTable<String, V> registry = new HashTable<String, V>();
Scanner input = new Scanner(System.in);
java.lang.String choice;
System.out.print(“nWhat Would You Like to Do? Enter a Number:”
+ “n1. Lookup Student by ID”
+ “n2. Remove an entry with Student ID”
+ “n3. Display Registryn”);
switch (choice) {
case “1”:
registry.lookupName();
case “2”:
registry.removeEntry();
case “3”:
registry.display();
}

}
}

The ‘key’ is the student id, and the ‘value’ is to call the object Student to get the name and letter grade.

I don’t understand how to make the registry “persistent’.

Expert Answer

//Student.java
import java.util.*;
import java.io.*;
public class Student {
private String name,grade;
int id;
//constructer

public Student(String name,int id,String grade){
this.name=name;
this.id=id;
this.grade=grade;
}

public int getId(){
return id;
}
//getting student
public String toString()
{
return this.id + ” “+ this.name+” “+this.grade;
}
//main method
public static void main(String args[]){
Hashtable<Integer,Student> register=new Hashtable<Integer,Student>(); //hashtable
Scanner input = new Scanner(System.in); //input
int option;
int id;
while(true){
System.out.println();
//menu
System.out.print(“nWhat Would You Like to Do? Enter a Number:”
+”n1. Inserting new Student”
+ “n2. Lookup Student by ID”
+ “n3. Remove an entry with Student ID”
+ “n4. Display Registry”
+”n5.exitn”);
option=input.nextInt();
if(option==5){
break;
}
switch(option){
case 1:
Student newStudent=createStudent();
register.put(newStudent.getId(),newStudent);
try{
writeToFIle(register); //writing to file
}
catch(Exception e){
}
break;
case 2:
System.out.print(“Please Enter the Student ID: “);
id =input.nextInt();
//found
if(register.get(id)!=null){
System.out.println(register.get(id));
}
else{
System.out.println(“Student Not found”);
}
break;
case 3:
System.out.print(“Please Enter the Student ID: “);
id = input.nextInt();
if(register.get(id)!=null){
register.remove(id);//removing student
try{
writeToFIle(register); //writing to file
}
catch(Exception e){
}
}
else{
System.out.println(“Student Not found”);
}

break;
case 4:
//displaying register
List<Integer> v = new ArrayList<Integer>(register.keySet());
Collections.sort(v);

for (Integer str : v) {
System.out.println(str + ” ” + register.get(str));
}
break;
}
}
try{
writeToFIle(register);
}
catch(Exception e){
}
}
public static Student createStudent(){
String name,grade;
int id;
Scanner sc=new Scanner(System.in);
System.out.printf(“Enter id::”);
id=sc.nextInt();
System.out.printf(“Enter Name::”);
name=sc.next();
System.out.printf(“Enter Grade::”);
grade=sc.next();

return new Student(name,id,grade);
}
public static void writeToFIle(Hashtable<Integer,Student> h) throws IOException{
List<Integer> v = new ArrayList<Integer>(h.keySet());
Collections.sort(v);
String result=””;
for (Integer str : v) {
result+=str + ” ” + h.get(str)+”n”;
}

File file = new File(“register.txt”);

// creates the file
file.createNewFile();

// creates a FileWriter Object
FileWriter writer = new FileWriter(file);

// Writes the content to the file
writer.write(result);
writer.flush();
writer.close();
System.out.println(“Record written to FIle”);
}
}

Still stressed from student homework?
Get quality assistance from academic writers!