2013-10-28 173 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