0

I am completing a final project for a Java course I am taking. The goal of this project is to create an Asset Management tool that someone could use to enter employees and their employee ID's, electronic devices assigned to them and the serial numbers, and then look up which devices an employee has by searching for their name or employee ID.

I have a Switch statement that checks to see if the user has typed 'add new employee record', 'search employee records', 'end program' or anything else (default). I need to create a second Switch that will operate when the user types 'search employee records'. I was researching this and found that it is not considered best practice to nest Switch statements inside of other Switch statements, and it would be better to write a method that you can call inside of the specific Switch case that will run another Switch statement. I have tried this, but the code I have written in this method is not able to see my HashMap that was created at the beginning of my main method. All of the errors I receive are related to this.

Question: Where do I need to move my HashMap so that both the first Switch (located in the main Class, main method) and the second Switch (located in the Employee class, searchForEmployee method) can see and gather keys/values from it?

Here's the code I have in the main Class:


import java.util.*;

class Main {
  public static void main(String[] args) {

    //Creating a Scanner to take input from user
    Scanner input = new Scanner(System.in);

    //Creating a HashMap to take in employee names and ID numbers
    Map<String, Integer> employeeIndex = new HashMap<>();

    //Creating an ArrayList to keep track of employees and allow for easy user searching
    ArrayList<Employee> employees = new ArrayList<Employee>();

    System.out.println("This is an Asset Management Tracking program. This database allows you to add and manage employee-assigned equipment. \n");

    boolean loop = true;
    while (loop) {
    
      System.out.println("\nWould you like to add an employee to the system, or search for an employee record? Please enter your selection below: \n" + "\nAdd New Employee Record \n" + "\nSearch Employee Records \n" + "\nEnd Program \n");

      String answer = input.nextLine().toLowerCase();
    
      switch (answer) {
          
        case "add new employee record": System.out.println("\nPlease enter the employee's name (Last name, First name): \n");
          String employeeName = input.nextLine();
          System.out.println("\nPlease enter their 6-digit employee ID: \n");
          int employeeID = input.nextInt();
          input.nextLine();

          Employee employee1 = new Employee(employeeName, employeeID);
          employeeIndex.put(employeeName, employeeID);
          System.out.println("\nOkay. You have entered:\n\n" + "Name: " + employeeName + "\nID: " + employeeID);
          employees.add(employee1);
          System.out.println(employeeIndex.entrySet());
          break;

        case "search employee records":  System.out.println("\nHow would you like to search? \n" + "\nEmployee Name\n" + "\nEmployee ID\n");
          Employee.searchForEmployee();
          break;

        case "end program":
          System.out.println("\nThank you for using this Asset Management Program!");
          input.close();
          loop = false;
          break;

        default: 
          System.out.println("\nSorry, that is an incorrect response (perhaps you typed it wrong?). Please choose one of the available options. \n");
          break;
      }
    }
  }
}

And the searchForEmployee method in the Employee Class:


static void searchForEmployee() {

    Scanner input2 = new Scanner(System.in);
    String answer2 = input2.next().toLowerCase();
    
    switch (answer2) {
          
      case "employee name": 
        System.out.println("\nPlease enter the employee's name (Last name, First name): \n");
        String searchEmployeeName = input2.nextLine();
        if (employeeIndex.containsKey(searchEmployeeName) = true) {
          System.out.println(employeeIndex.get(searchEmployeeName));  
        } else {
            System.out.println("\nI'm sorry, that employee does not appear to be in this database yet. Here are the employee records contained so far: \n");
            System.out.println(employeeIndex.keySet());
          }
        break;

      case "employee id":  
        System.out.println("\nPlease enter the employee's ID#: \n");
        int searchEmployeeID = input2.nextInt();
        if (employeeIndex.containsValue(searchEmployeeID) = true) {
          System.out.println(employeeIndex.get());
        } else {
            System.out.println("\nI'm sorry, that employee ID does not appear to be in this database yet. Here are the employee ID numbers contained so far: \n");
            System.out.println(employeeIndex.values());
          }
        break;

      default: 
        System.out.println("\nSorry, that is an incorrect response (perhaps you typed it wrong?). Please choose one of the available options. \n");
        break;
      }
    }

These are the errors I receive when I run the program:


Employee.java:26: error: cannot find symbol
        if (employeeIndex.containsKey(searchEmployeeName) = true) {
            ^
  symbol:   variable employeeIndex
  location: class Employee

Employee.java:27: error: cannot find symbol
          System.out.println(employeeIndex.get(searchEmployeeName));  
                             ^
  symbol:   variable employeeIndex
  location: class Employee

Employee.java:30: error: cannot find symbol
            System.out.println(employeeIndex.keySet());
                               ^
  symbol:   variable employeeIndex
  location: class Employee

Employee.java:37: error: cannot find symbol
        if (employeeIndex.containsValue(searchEmployeeID) = true) {
            ^
  symbol:   variable employeeIndex
  location: class Employee

Employee.java:38: error: cannot find symbol
          System.out.println(employeeIndex.get());
                             ^
  symbol:   variable employeeIndex
  location: class Employee

Employee.java:41: error: cannot find symbol
            System.out.println(employeeIndex.values());
                               ^
  symbol:   variable employeeIndex
  location: class Employee

6 errors
exit status 1

Thanks again for your help. I am still very much in the beginner stage and this community has been so helpful to me in the learning process.

MiakaSHW
  • 1
  • 1
  • Variable scopes is one of the first things you should have learned in this course if it's a beginning programming course.. – Paul Samsotha Sep 10 '22 at 02:48
  • It is a course that focuses specifically on Java, not on programming as a whole. I believe that scopes were very lightly touched upon in one of the modules, I will go back and review to see if it answers this particular question. Thank you for your answer. – MiakaSHW Sep 11 '22 at 21:33

0 Answers0