2013-10-28 73 views
0

刚刚了解了最近的哈希映射。到目前为止,我的代码可以添加,删除,打印性能比例,并排序员工的最后/第一个姓名。但是,更新/修改没有做任何事情。我的尝试是,首先搜索我想更新的员工ID。输入他们的ID后,我会提示用户修改性能比例,但之后如果用户打印出所有性能比例,它仍然不会改变任何内容。哈希映射,无法修改/更新

因此,有关如何更新的任何建议/提示/链接都很有帮助。这是我的代码的一部分,涵盖所有情况。 (情况3是更新/修改,显示了我对它的尝试工作)。

switch (choice) { 
     case 1: 
      addEmployee(firstAndLast, idNumber, performanceScale); 
      break; 
     case 2: 
      removeEmployee(firstAndLast, idNumber, performanceScale); 
      break; 

     case 3: 
      modifyPerformanceScale(idNumber); 
      break; 
     case 4: 
      printAllperfScale(performanceScale); 
      break; 
     case 5: 
      printLastNameAscending(firstAndLast); 
      break; 
     case 6: 
      exit = true; 
      System.out.println("Exiting program..."); 
      break; 
     default: 
      System.out 
        .println("Please choose a number from 1 - 5 from the menu."); 
     } 
    } 

} 

public static void addEmployee(TreeMap<String, Employee> firstAndLastMap, 
     TreeMap<Integer, Employee> idNumberMap, 
     TreeMap<Employee, Integer> performanceScale) { 
    Scanner keyboardInput = new Scanner(System.in); 
    String firstName; 
    String lastName; 
    int id; 
    int perfScale; 

    System.out.print("Enter first name for the Employee: "); 
    firstName = keyboardInput.nextLine(); 
    System.out.print("Enter last name for the Employeer: "); 
    lastName = keyboardInput.nextLine(); 
    System.out.print("Enter ID number of the Employee: "); 
    id = keyboardInput.nextInt(); 
    System.out.print("Enter Performance Scale rating between 1 to 5: "); 
    perfScale = keyboardInput.nextInt(); 

    Employee addEmployee = new Employee(lastName, firstName, id, perfScale); 

    firstAndLastMap.put(lastName + ", " + firstName, addEmployee); 
    idNumberMap.put(id, addEmployee); 

    performanceScale.put(addEmployee, perfScale); 

} 

public static void removeEmployee(TreeMap<String, Employee> firstAndLastMap, 
     TreeMap<Integer, Employee> idNumberMap, 
     TreeMap<Employee, Integer> performanceScale) { 

    Scanner keyboardInput = new Scanner(System.in); 
    String firstName; 
    String lastName; 
    int id; 
    System.out.print("Enter First name of Employee you want to remove: "); 
    firstName = keyboardInput.nextLine(); 
    System.out.print("Enter last name of Employee you want to remove: "); 
    lastName = keyboardInput.nextLine(); 

    System.out.print("Enter ID number of Employee you want to remove: "); 
    id = keyboardInput.nextInt(); 



    firstAndLastMap.remove(lastName + ", " + firstName); 
    idNumberMap.remove(id); 


} 

public static void modifyPerformanceScale(TreeMap<Integer, Employee> idNumber) { 
    System.out.print("Enter ID: "); 
    Scanner keyboardInput = new Scanner(System.in); 

    int idNumber1; 
    int modScale; 
    idNumber1 = keyboardInput.nextInt(); 
    idNumber.remove(idNumber1); 

    System.out.print("Enter the number you want to change to: "); 
    modScale = keyboardInput.nextInt(); 
} 

public static void printAllperfScale(TreeMap<Employee,Integer> performanceScale) { 
    Set Employee1 = performanceScale.entrySet();  
    Iterator itr1 = Employee1.iterator(); 

    while (itr1.hasNext()) { 
     Map.Entry me = (Map.Entry) itr1.next(); 
     System.out.println(me.getValue()); 
    } 
} 


public static void printLastNameAscending(TreeMap<String, Employee> LastName) { 
    Set Employee1 = LastName.entrySet(); 
    Iterator itr1 = Employee1.iterator(); 

    while (itr1.hasNext()) { 
     Map.Entry me = (Map.Entry) itr1.next(); 
     System.out.println(me.getKey()); 
    } 
} 

    } 

回答

0

我相信你想改变你的方法

public static void modifyPerformanceScale(TreeMap<Integer, Employee> idNumber, TreeMap<Employee, Integer> performanceScale) { 
    System.out.print("Enter ID: "); 
    Scanner keyboardInput = new Scanner(System.in); 

    int idNumber1; 
    int modScale; 
    idNumber1 = keyboardInput.nextInt(); 

    System.out.print("Enter the number you want to change to: "); 
    modScale = keyboardInput.nextInt(); 

    Employee employee = idNumber.get(idNumber1); //note: This will return null if the employee does not exist. Check for this, if necessary. 
    performanceScale.put(employee, modScale); 
} 

这得到与该ID的职员。

这将用该新条目替换该员工以前的条目。 (Map.put添加条目,如果该键不存在,或者它取代了,如果它已经存在于映射键的条目。)

,然后确保在正确的TreeMap s到传递给你的新modifyPerformanceScale方法。

+0

好的谢谢你的回复先生。我会照你的要求去做,并确保我明白代码的原因。 :)几分钟后,我会点击复选标记。 – Christian

+0

我注意到一个bug并修复了它。一定要使用编辑的答案。 –

+0

好的,谢谢你的贡献。 :)感谢帮助。 – Christian