I have an assignment to create an ArrayList of employees, to provide a menu to add, find, and delete employee records. I successfully managed to implement all the functions on my own, but there is a small problem. When I use the find or delete option, the correct record is found or deleted properly but the code goes through the array list of elements and prints out employee not found till the correct record is found, this is unnecessary as it should only print the found record. I have limited experience with coding and I am writing my own code from scratch, please help me with this. enter image description here
I'VE ATTACHED MY CODE AND THE OUTPUT!
else if (choice.equals("4")) {
System.out.println("Enter Name: ");
String fName = myScanner.nextLine();
System.out.println("Enter Job Name: ");
String fJob = myScanner.nextLine();
for (int i = 0; i < myEList.size(); i++) {
if (myEList.get(i).getName().equals(fName) && myEList.get(i).getJob().equals(fJob)) {
System.out.println("Employee found!");
System.out.println(myEList.get(i).toString());
} else {
System.out.print("Employee not found!");
}
}
} else if (choice.equals("5")) {
System.out.println("Enter Name: ");
String dName = myScanner.nextLine();
System.out.println("Enter Job Name: ");
String dJob = myScanner.nextLine();
for (int i = 0; i < myEList.size(); i++) {
if (myEList.get(i).getName().equals(dName) && myEList.get(i).getJob().equals(dJob)) {
System.out.println("Employee record removed succesfully!");
myEList.remove(i);
} else {
System.out.print("Employee not found!");
}
}
}
This is the Output Enter Option: 4 Enter Name: arjun Enter Job Name: tester Searching... Employee not found! Employee not found! Employee found! Name: arjun Job Name: tester Weekly Pay: 1200.0