2012-06-27 87 views
1

我正在製作一個EmployeeStore,它將存儲名稱,dob,id,電子郵件地址等......我需要編寫一個編輯方法。我GOOGLE了,我無法找到如何做到這一點,任何人都可以幫忙?這是我的代碼:HashMap EmployeeStore的編輯方法

//Imports. 
import java.util.Scanner; 
//******************************************************************** 
public class MainApp 
{ 
    private static Scanner keyboard = new Scanner(System.in); 

    public static void main(String[] args) 
    { 
     new MainApp().start(); 

    } 
    public void start() 
    { 
     EmployeeStore Store = new EmployeeStore(); 
     Store.add(new Employee ("James O' Carroll", 18,"hotmail.com")); 

     Store.add(new Employee ("Andy Carroll", 1171,"yahoo.com")); 

     Store.add(new Employee ("Luis Suarez", 7,"gmail.com")); 
//Test Code with the new Hashmap.  
     /*Store.print(); 
     Store.clear(); 
     Store.print(); 

     Store.add(new Employee ("James O' Carroll", 18,"hotmail.com")); 

     Store.add(new Employee ("Andy Carroll", 1171,"yahoo.com")); 

     Store.add(new Employee ("Luis Suarez", 7,"gmail.com")); 

     Store.print(); 
     Store.remove("Andy Carroll"); 
     Store.print();*/ 
//******************************************************************** 
     //Switch Statement for use of a menu. 
     int choice; 
      do { 
       choice = getMenuChoice("1.\tLibrarian\n2.\tPublic User\n3.\tExit\n\n", 3, "Please enter choice:", "Error [1,3] only"); 
       switch (choice) { 
        case 1: 
         System.out.println("Librarian Functionality...\n"); 
         break; 
        case 2: 
         System.out.println("Public User functionality...\n"); 

         break; 
        case 3: 
         System.out.println("Program Finished"); 

       } 
      } 
      while (choice != 3); 
} 
//******************************************************************** 
     public static int getMenuChoice(String menuString, int limit, String prompt, String errorMessage) 
     { 
      System.out.println(menuString); 
      int choice = inputAndValidateInt(1, limit, prompt, errorMessage); 
      return choice; 
     } 
//******************************************************************** 



     public static int inputAndValidateInt(int min, int max, String prompt, String errorMessage) { 
      int number; 
      boolean valid; 
      do { 
       System.out.print(prompt); 
       number = keyboard.nextInt(); 
       valid = number <= max && number >= min; 
       if (!valid) { 
        System.out.println(errorMessage); 
       } 
      } while (!valid); 
      return number; 
     } 
//******************************************************************** 
} 


//Imports: 

//******************************************************************** 
//Employee Class. 
public class Employee 
{ 
//Variables. 
    private String employeeName; 
    private int employeeId; 
    private String employeeEmail; 
//******************************************************************** 
//Constructor. 
    public Employee(String employeeName, int employeeId, String employeeEmail) 
    { 
     this.employeeName = employeeName; 
     this.employeeId = employeeId; 
     this.employeeEmail = employeeEmail; 
    } 
//******************************************************************** 
//Getters. 
    public String getEmployeeEmail() { 
     return employeeEmail; 
    } 
    public void setEmployeeEmail(String employeeEmail) { 
     this.employeeEmail = employeeEmail; 
    } 
    public String getEmployeeName() { 
     return employeeName; 
    } 
    public int getEmployeeId() { 
     return employeeId; 
    } 
//******************************************************************** 
//toString method. 
    public String toString() { 
     return "Employee [employeeName=" + employeeName + ", employeeId=" 
       + employeeId + ", employeeEmail=" + employeeEmail + "]"; 
    } 
//******************************************************************** 





} 
//Imports. 
import java.util.HashMap; 
//******************************************************************** 
import java.util.Map; 

public class EmployeeStore 
{ 
    HashMap<String, Employee> map; 

//Constructor. 
    public EmployeeStore() 
    { 
     map = new HashMap<String,Employee>(); 
    } 
//******************************************************************** 
//Hashmap Methods. 
//Add to the Hashmap : Employee. 
    public void add(Employee obj) 
    { 

     map.put(obj.getEmployeeName(), obj); 
    } 
//******************************************************************** 
//Remove from the Hashmap : Employee. 
    public void remove(String key) 
    { 
     //Remove the Employee by name. 
     map.remove(key); 
    } 
//******************************************************************** 
//Clear the Hashmap : Employee. 
    public void clear() 
    { 
     map.clear(); 
    } 
    //******************************************************************** 
//Print the Hashmap : Employee. 
    public void print() 
    { 
     System.out.println("\n********Employee's in the Company.********"); 
     for (Employee employee : map.values()) 
     { 
      System.out.println("Employee Name:\t" + employee.getEmployeeName()); 
      System.out.println("Employee Id:\t" + employee.getEmployeeId()); 
      System.out.println("E-mail:\t"+ employee.getEmployeeEmail()); 
     } 

    } 


//******************************************************************** 
//******************************************************************** 


} 
+0

public item searchForID(int id){ Item e = null; (int i = 0; i Pendo826

+0

太複雜。使用HashMap.get(K)。檢查我的解決方案 –

回答

2

您需要從HashMap中獲取Employee對象,然後修改該對象。例如,要更改電子郵件:

//in class EmployeeStore 
String email = somehowGetNewEmail(); 
Employee toEdit = map.get(somehowGetName()); 
toEdit.setEmail(email) 

或者:

//in EmployeeStore 
public Employee get(String name){ 
    return map.get(name); 
} 

//in any class with reference to an EmployeeStore "store" 
store.get(name).editSomething(something); 
+0

那麼我可以使用store.get(name).editId()類的東西? – Pendo826

+0

@ConorPendlebury是的,但你需要添加一個'EmployeeStore.get(key)'方法。 –

+0

即時通訊如此混亂現在大聲笑 – Pendo826

0

對象是一個HashMap商店引用。這意味着,當您從HashMap讀取(「獲取」)對象並更改其屬性時,這些更改將繼續進行,而不必將其寫回到HashMap中。

因此,您所有的編輯方法都要調用map.get(name)並對返回的Employee對象進行更改。請注意,您無法以這種方式更改HashMap的鍵。爲了「重命名」員工,您必須從哈希映射中刪除舊密鑰的值,並將其插入到新密鑰中。

+0

您可以調用'HashMap.put(key,value)',它會覆蓋該鍵的值並返回鍵的舊值,如果沒有則返回'null'。但是,這是不必要的,因爲'Employee'是可變的。 –