Write a java program to store employee information in a linked list, the program
ID: 658684 • Letter: W
Question
Write a java program to store employee information in a linked list, the program should implement the following functions:
to add an new employee to the linked list and store employee's information ( ID, name, address, department,salary),
Modify an existing employee's information (change address and salary only). As we should ask for employee ID to update his/her information,to remove the employee using employee ID from the linked list. The program should display the list of functions, and ask the user to enter the number of function that he/she wants to do, then perform the function as it is required in the previous table.Finally we display all current employees in your linked list, by creating a function
called display.
Explanation / Answer
/*
* The Employee class that contains id, name, address,,dept and salary
* as member variables. The class also contains the default constructor
* and parameterized constructor to set and get methods for id, name,
* address ,dept and salary
* */
//Employee.java
public class Employee
{
private int id;
private String name;
private String address;
private String dept;
private int salary;
//Default constrctor
public Employee()
{
this.id=0;
this.name="";
this.address="";
this.dept="";
this.salary=0;
}
//Parameterized constrctor
public Employee(int id, String name,String address,String dept, int salary)
{
this.id=id;
this.name=name;
this.address=address;
this.dept=dept;
this.salary=salary;
}
//Set methods
public void setID(int id)
{
this.id=id;
}
public void setName(String name)
{
this.name=name;
}
public void setAddress(String adddress)
{
this.address=adddress;
}
public void setDept(String dept)
{
this.dept=dept;
}
public void setSalary(int salary)
{
this.salary=salary;
}
//Get methods
public int getID()
{
return id;
}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
public String getDept()
{
return dept;
}
public int setSalary()
{
return salary;
}
//Override the toString that returns the string represetnation of Employee class object
@Override
public String toString()
{
return "ID : "+id+" Name : "+name+" Address : "+address+" Dept : "+dept+" Salary : "+salary;
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------
/*Java menu program that display a menu of choice to add an employee, to modify the employee or to delete
an employee and display the list of employees stored in alinked list */
//EmployeeDriver.java
import java.util.LinkedList;
import java.util.Scanner;
public class EmployeeDriver
{
public static void main(String[] args)
{
//Scanner object to read input from user
Scanner scanner=new Scanner(System.in);
int id;//To read id of employee
//Create an instance of LinkedList class that takes Employee as its argumet type
LinkedList<Employee>empList=
new LinkedList<Employee>();
//Repeat while loop as long as user enter exit optiopn
while(true)
{
int userchoice=menu();
switch(userchoice)
{
case 1:
//call readEmployee method that read id,name,address, dept and salary
//and add emp object to linkelist empList
Employee emp=readEmployee();
//call add method on employee
empList.add(emp);
break;
case 2:
//prompt for id of employee
System.out.println("Enter employee id");
id=scanner.nextInt();
//call the method modifyEmployee with id and empList to modify
//employee of given id
modifyEmployee(empList,id);
break;
case 3:
//prompt for id of employee
System.out.println("Enter employee id");
id=scanner.nextInt();
//call the method removeEmployee with id and empList to remove
//employee of given id
removeEmployee(empList,id);
break;
case 4:
//call the method display to print employees in linkedlist
display(empList);
break;
case 5:
//exit from the program
System.exit(1);
break;
}
}
}
/*The method display that takes the LinkedList,empList and
prints the employees in the list */
private static void display(LinkedList<Employee> empList)
{
//index of employee
int index=0;
while (index<empList.size())
{
System.out.println(empList.get(index).toString());
index++;
}
}
/*The method removeEmployee that takes the LinkedList,empList and id
* as input arguments and checks if the employee exists then remove
* the employee from the empList */
private static void removeEmployee(LinkedList<Employee> empList, int id)
{
int index=0;
boolean found=false;
while (index<empList.size() && !found)
{
if(empList.get(index).getID()==id)
{
empList.remove(index);
found=true;
}
index++;
}
if(found)
System.out.println("Employee removed from Linked List");
else
System.out.println("No employee found with id "+id);
}
/*The method modifyEmployee that takes the LinkedList,empList and id
* as input arguments and checks if the employee exists then prompts
* for new address and new salary and set using set methods of employee
* class update the employee in the empList*/
private static void modifyEmployee(LinkedList<Employee> empList, int id)
{
Scanner scanner=new Scanner(System.in);
int index=0;
boolean found=false;
String newAddress;
int newSalary;
while (index<empList.size() && !found)
{
if(empList.get(index).getID()==id)
{
found=true;
System.out.println("Enter Address ");
newAddress=scanner.nextLine();
System.out.println("Enter Salary ");
newSalary=scanner.nextInt();
empList.get(index).setAddress(newAddress);
empList.get(index).setSalary(newSalary);
}
index++;
}
if(found)
System.out.println("Employee Modified");
else
System.out.println("No employee found with id "+id);
}
/*The method readEmployee prompts user to enter id, name, address, dept and salary
and returns a employee object as its return type */
private static Employee readEmployee()
{
Scanner scanner=new Scanner(System.in);
System.out.println("Enter ID ");
int id=Integer.parseInt(scanner.nextLine());
System.out.println("Enter Name ");
String name=scanner.nextLine();
System.out.println("Enter Address ");
String address=scanner.nextLine();
System.out.println("Enter Dept ");
String dept=scanner.nextLine();
System.out.println("Enter Salary ");
int salary=scanner.nextInt();
return new Employee(id, name, address, dept, salary);
}
/*The menu class that displays the menu of choices to select from
* and returns user choice as return type .*/
private static int menu()
{
Scanner scanner=new Scanner(System.in);
int ch;
System.out.println("1.Add employee to Linked List");
System.out.println("2.Modify employee in Linked List");
System.out.println("3.Remove employee From Linked List");
System.out.println("4.Display employees in Linked List");
System.out.println("5.Exit ");
//repeats the loop if user enter invalid choice.
do
{
System.out.println("Enter your choice ");
ch=scanner.nextInt();
if(ch<1 ||ch>5)
System.out.println("Invalid Choice.Enter valid choice");
}while(ch<1 ||ch>5);
//return user choice.
return ch;
}
}//end of class
------------------------------------------------------------------------------------------------------------------------------------
sample output:
1.Add employee to Linked List
2.Modify employee in Linked List
3.Remove employee From Linked List
4.Display employees in Linked List
5.Exit;
Enter your choice
1
Enter ID
1000
Enter Name
Mahesh
Enter Address
NY
Enter Dept
sales
Enter Salary
20000
1.Add employee to Linked List
2.Modify employee in Linked List
3.Remove employee From Linked List
4.Display employees in Linked List
5.Exit;
Enter your choice
4
ID : 1000 Name : Mahesh Address : NY Dept : sales Salary : 20000
1.Add employee to Linked List
2.Modify employee in Linked List
3.Remove employee From Linked List
4.Display employees in Linked List
5.Exit;
Enter your choice
1
Enter ID
1001
Enter Name
Kunal
Enter Address
NY
Enter Dept
Marketing
Enter Salary
25000
1.Add employee to Linked List
2.Modify employee in Linked List
3.Remove employee From Linked List
4.Display employees in Linked List
5.Exit;
Enter your choice
4
ID : 1000 Name : Mahesh Address : NY Dept : sales Salary : 20000
ID : 1001 Name : Kunal Address : NY Dept : Marketing Salary : 25000
1.Add employee to Linked List
2.Modify employee in Linked List
3.Remove employee From Linked List
4.Display employees in Linked List
5.Exit;
Enter your choice
2
Enter employee id
1000
Enter Address
Texas
Enter Salary
35000
Employee Modified
1.Add employee to Linked List
2.Modify employee in Linked List
3.Remove employee From Linked List
4.Display employees in Linked List
5.Exit;
Enter your choice
4
ID : 1000 Name : Mahesh Address : Texas Dept : sales Salary : 35000
ID : 1001 Name : Kunal Address : NY Dept : Marketing Salary : 25000
1.Add employee to Linked List
2.Modify employee in Linked List
3.Remove employee From Linked List
4.Display employees in Linked List
5.Exit;
Enter your choice
3
Enter employee id
1001
Employee removed from Linked List
1.Add employee to Linked List
2.Modify employee in Linked List
3.Remove employee From Linked List
4.Display employees in Linked List
5.Exit;
Enter your choice
4
ID : 1000 Name : Mahesh Address : Texas Dept : sales Salary : 35000
1.Add employee to Linked List
2.Modify employee in Linked List
3.Remove employee From Linked List
4.Display employees in Linked List
5.Exit;
Enter your choice
Hope this helps you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.