Do the following program in Java: Write a Java program to implement simple index
ID: 3735340 • Letter: D
Question
Do the following program in Java:
Write a Java program to implement simple index on primary key for a file of employee records:
You should use the switch case statement for the following:
Case 1: Implement add() to ask users to enter information about employee records, including their EID, Name, Age, and Rating, store these information to a fixed-length record file. Create an index file at the same time by using EID as primary key and RRN as reference.
Case 2: Implement search() by EID using the index file, and display the record on screen if record found, otherwise, display "Record not found!"
Case 3: Implement delete() using the index file.
Case 4: quit the program
Explanation / Answer
java code:
package employeerecord;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
class Employee
{
//record variables
String EID;
String Name;
int Age;
int Rating;
//constructor
Employee()
{
this.EID="";
this.Name="";
this.Age=0;
this.Rating=0;
}
//constructor with parameter
Employee(String id, String name, int age, int rate) {
this.EID=id;
this.Name=name;
this.Age=age;
this.Rating=rate; }
//set record function
void setEmployee( String EID, String Name, int Age, int Rating)
{
this.EID=EID;
this.Name=Name;
this.Age=Age;
this.Rating=Rating;
}
//display the record
void display()
{
System.out.println(" EID: "+EID+" NAME: "+Name+" AGE: "+Age+" RATING: "+Rating+" ");
}
//write to file function
void writeToFile() throws IOException
{
BufferedWriter bw=new BufferedWriter(new FileWriter("Employee.txt"));
bw.write(" EID: "+EID+" NAME: "+Name+" AGE: "+Age+" RATING: "+Rating);
}
}
//driver class
public class EmployeeRecord {
public static void main(String[] args)throws IOException {
Employee[] e=new Employee[10]; //employee record for 10 set
int n=0,c;
Scanner sc=new Scanner(System.in);
//get user options
do
{
System.out.println("1.Add Record 2.Search 3.delete 4.Quit"+
" Enter your choice: ");
c=sc.nextInt();
//call the appropirate function
switch(c){
case 1:
add(e,n,sc); //call add()
n++;
break;
case 2:
search(e,n,sc); //callsearch()
break;
case 3:
if(delete(e,n,sc)==1) //call delete()
n--;
break;
default:
System.exit(1); //quit
}
}while(true);
}
//add function
private static void add(Employee[] e, int n, Scanner sc)throws IOException {
String id,name;
int age,rate;
System.out.println("Enter id:");
id=sc.next();
System.out.println("Enter Name:");
name=sc.next();
System.out.println("Enter Age:");
age=sc.nextInt();
System.out.println("Enter Rating:");
rate=sc.nextInt();
e[n]=new Employee(id, name, age, rate);
e[n].writeToFile();
System.out.println("Record added successfully ");
}
//search function
private static void search(Employee[] e, int n, Scanner sc) {
String id;
System.out.println("Enter id to search:");
id=sc.next();
for(int i=0;i<n;i++)
{
if(e[i].EID.equals(id)){
e[i].display();
return;
}
}
System.out.println("Record not found ");
}
//delete function
private static int delete(Employee[] e, int n, Scanner sc) {
String id;
System.out.println("Enter id to delete:");
id=sc.next();
for(int i=0;i<n;i++)
{
if(e[i].EID.equals(id)){
for(int j=i;j<n-1;j++)
{
e[j]=e[j+1] ;
}
return 1;
}
}
System.out.println("Record not found ");
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.