Now that you\'ve implemented the hash tables you will use to implement a ver sim
ID: 3845929 • Letter: N
Question
Now that you've implemented the hash tables you will use to implement a ver simple registry for students' records. 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.
This is for java
Explanation / Answer
//test driver class
package sample1;
import java.util.Random;
import java.util.Scanner;
import java.util.Date;
public class test
{
public static void main(String args[]){
Registry re=new Registry();
re.addStudent();
re.addStudent();
re.addStudent();
re.addStudent();
re.removeStudent();
re.showRegistry();
re.persistRegistry();
}
}
//Student class
package sample1;
public class Student {
private int ID;
private String name;
private String grade;
public Student(){
}
public Student(int iD, String name, String grade) {
super();
ID = iD;
this.name = name;
this.grade = grade;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
//Student Registry class
package sample1;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Scanner;
public class Registry {
private Hashtable<Integer,Student> stuTable=new Hashtable<Integer,Student>();
private Scanner s=new Scanner(System.in);
private Scanner s1=new Scanner(System.in);
public void addStudent(){
System.out.println(" Enter the student name : ");
String name=s.next();
System.out.println(" Enter the student ID : ");
int ID=s.nextInt();
System.out.println(" Enter the student grade : ");
String grade=s1.nextLine();
Student st=new Student(ID,name,grade);
if(!stuTable.containsKey(new Integer(ID))){
stuTable.put(new Integer(ID),st);
System.out.println("Added Successfully");
else
System.out.println(" Student with this ID already present..");
}
public void removeStudent(){
System.out.println(" Enter the student ID to remove : ");
int ID=s.nextInt();
try{
stuTable.remove(new Integer(ID));
System.out.println("Removed Successfully");
}
catch(Exception e){
System.out.println(" Student not found");
}
}
public void showRegistry(){
System.out.println("| Name | ID | Grade |");
for(Integer id:stuTable.keySet()){
System.out.println(stuTable.get(id).getName()+" | "+stuTable.get(id).getID()+" | "+stuTable.get(id).getGrade());
}
}
public void persistRegistry(){
System.out.println(" Enter file system path to persit file : ");
String filePath=s.nextLine();
String fileContent="";
for(Integer id:stuTable.keySet())
fileContent+=stuTable.get(id).getName()+" | "+stuTable.get(id).getID()+" | "+stuTable.get(id).getGrade()+" ";
try {
File file=new File(filePath);
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(fileContent);
fileWriter.flush();
fileWriter.close();
System.out.println(" Registry written to file successfully");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.