There are multiple issues with my code, I know. I am definitely a beginner. One issue is that my object keeps being written over. I tried incrementing within the switch case as you can see. That doesn't seem to work. I am not sure how else to do it. Also, when my program sees that a user tries to reuse an employee id it gives an error, but doesn't break out of the switch case. Any help on how to break out once the error is printed would be a big help! Thank you!
import java.util.*;
public class HR{
public static void main (String arg [ ]) throws Exception {
Employee[] employees = new Employee[10];
int size = 0;
int index = 0;
int option = 1;
int enumb = 0;
int delEnumb = 0;
double salary;
double moreThanSalary;
String name;
while (option != 0) {
try {
// Printing statements displaying menu on console
System.out.println("\n**********MENU**********");
System.out.println("1: Add Employee");
System.out.println("2: Delete Employee");
System.out.println("3: Display Employee(s) Earning Salary higher that you specify");
System.out.println("4: Print all employees info");
System.out.println("0: Exit program");
Scanner input = new Scanner(System.in);
System.out.print("\nEnter your selection : ");
option = input.nextInt();
switch (option) {
case 1:
Scanner scanint = new Scanner(System.in);
Scanner scanstr = new Scanner(System.in);
System.out.println("\nYou have selected to add a new employee");
System.out.println("\nEnter ID: ");
enumb = scanint.nextInt();
for (index = 0; index < employees.length; index++){
if (employees[index] != null){
if (employees[index].getEnumb() == enumb) {
System.out.println("\nEmployee ID already exists");
}
}
}
System.out.println("Enter salary: ");
salary = scanint.nextDouble();
System.out.println("Enter name: ");
name = scanstr.nextLine();
employees[size]= new Employee(enumb, salary, name);
size++; // this doesn't seem to be incrementing so it is just writing over
// the same object again and again.
break;
case 2:
System.out.println("\nYou have selected to remove an employee");
Scanner scanId = new Scanner(System.in);
System.out.println("Enter Employee number: ");
delEnumb = scanId.nextInt();
if (delEnumb >= 10001 && delEnumb <= 99999){
for (index = 0; index < employees.length; index++){
if (employees[index] != null){
if (employees[index].getEnumb() == delEnumb) {
employees[index] = null;
System.out.println("Employee entry has been deleted");
}
}
}
}
break;
case 3:
System.out.println("\nYou have selected to search through and find employees "
+"\nwhose salary is more than an amount you provide.");
System.out.println("Enter Salary amount: ");
Scanner userInput = new Scanner(System.in);
moreThanSalary = input.nextDouble();
for (index = 0; index < employees.length; index++) {
if (employees[index] != null){
if (employees[index].getSalary() > moreThanSalary ) {
System.out.println(employees[index]);
}
}
}// closes for loop
break;
case 4:
System.out.println("============================");
for (index=0; index < employees.length; index++){
if (employees[index] != null){
System.out.println(employees[index]);
System.out.println("============================");
}
}
break;
case 0:
System.out.print("\nYou have selected to exit the program. Thank you!");
break;
// If none of the above cases execute (less than 0 or more than 4)
default:
System.out.println("\nSorry, you have entered a number other than 1, 2, 3, 4 or zero. \nTry Again.\n");
break;
} // closes switch
} // closes try
catch (InputMismatchException ime) {
System.out.print("\n\t\t\t\t ***Error*** \nThat entry was not an integer! Try again :)\n");
continue;
} // closes catch
catch (EmployeeException ee) {
System.out.print(ee.getMessage());
} // closes catch
} // closes while
} // closes main
} // closes class '''