Menu Driven java program to take input and display details of the employee. When the user wants to add an employee he chooses option 1 whereas he chooses option 5 to display the details he entered.
I tried the list.contains(Object o) method but it keeps adding the repeated object into the list. Right now I have removed that part of the code.
class Employee {
Scanner sc = new Scanner(System.in);
int empid, num;
String empname, empdesignation, empdept, empproject;
//static ArrayList al = new ArrayList();
public void setEmpNo(int n) {
this.num = n;
}
public int getEmpNo() {
return num;
}
public int getID() {
return empid;
}
public void setID(int id) {
this.empid = id;
}
public String getName() {
return empname;
}
public void setName(String name) {
this.empname = name;
}
public String getDept() {
return empdept;
}
public void setDept(String dept) {
this.empdept = dept;
}
public String getDesig() {
return empdesignation;
}
public void setDesig(String desig) {
this.empdesignation = desig;
}
public String getProject() {
return empproject;
}
public void setProject(String proj) {
this.empproject = proj;
}
public void displayemp(int id, String name, String dept, String designation, String project) {
System.out.print("\n============================================================\n");
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Department: " + dept);
System.out.println("Designation: " + designation);
System.out.println("Project: " + project);
System.out.print("\n============================================================\n");
}
}
public class trying {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int ch = 0, id, n, empID, pid, projectID;
// final int maxEmp=1000;
String name, designation, dept, pname, mgrname, project;
Employee emp10 = new Employee();
Project proj10 = new Project();
List<Employee> list = new ArrayList<Employee>();
List<Project> list1 = new ArrayList<Project>();
// String[] p2=new String[100];
do {
System.out.println("MENU");
System.out.println(
" 1.Add Employee\n 2.Add Project \n 3.Edit Employee\n 4.Edit Project\n 5.Show Employee Deatils \n 6.Show Project Details \n 7.Exit");
// System.out.println("Enter the maximum number of employees: ");
// Employee[] emp=new Employee[maxEmp];
System.out.print("Enter choice: ");
try {
ch = sc.nextInt();
sc.nextLine();
} catch (Exception e) {
System.out.println("Enter an Integer value");
}
switch (ch) {
case 1:
// Employee emp6 = new Employee(1,"qwe","asd","zxc","qazwsxedc");
// Employee emp7 = new Employee(2,"q","d","z","q");
System.out.println("Enter the number of employees to be entered: ");
try {
n = sc.nextInt();
emp10.setEmpNo(n);
if (n < 0) {
System.out.println("Only Positive Numbers & no Letters Please!");
continue;
}
for (int i = 0; i < n; i++) {
Employee emp5 = new Employee();
System.out.print("\n============================================================\n");
System.out.print("Enter ID: ");
id = sc.nextInt();
try {
if (id < 0) {
System.out.println("Enter an Integer value");
continue;
}
emp5.setID(id);
} catch (InputMismatchException e) {
System.out.println("Enter an Integer value");
sc.next();
System.exit(0);
}
sc.nextLine();
System.out.print("Enter Name: ");
name = sc.nextLine();
emp5.setName(name);
System.out.print("Enter Department: ");
dept = sc.nextLine();
emp5.setDept(dept);
System.out.print("Enter Designation: ");
designation = sc.nextLine();
emp5.setDesig(designation);
System.out.print("Enter Project: ");
project = sc.nextLine();
emp5.setProject(project);
System.out.print("\n============================================================\n");
// emp5.displayemp(id, name, dept, designation, project);
list.add(emp5);
// list.add(emp7);
}
}
catch (InputMismatchException e) {
System.out.println("Enter an Integer value");
sc.next(); // this consumes the invalid token
}
case 5:
System.out.println("Showing Employee Details:");
for (Employee e : list) {
System.out.print("\n============================================================\n");
System.out.println("Id: " + e.empid + "\nName: " + e.empname + "\nDept: " + e.empdept + "\nDesig: "
+ e.empdesignation + "\nProject: " + e.empproject);
System.out.print("\n============================================================\n");
}
break;
I want this code to remove the same entries made by the user ie., no redundant entries.
Current output
Id: 1 Name: q Dept: a Desig: z Project: qaz
============================================================
============================================================
Id: 1 Name: q Dept: a Desig: z Project: qaz
============================================================
Expected output
Id: 1 Name: q Dept: a Desig: z Project: qaz
============================================================