2012-07-03 46 views
0

嘿,任何人都可以幫助我理解如何爲我的公司應用程序編輯方法。我以前曾要求使用搜索方法的幫助。我認爲編輯方法可能涉及搜索方法。Employee Store的編輯方法(使用Hashmap)

這裏是我的代碼:

EmployeeStore. 

//Imports. 
import java.util.HashMap; 
//******************************************************************** 
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 employee) 
    { 

     map.put(employee.getEmployeeName(), employee); 
    } 
//******************************************************************** 
//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); to print the toString of Employee class 
      //or: 
      System.out.println("Employee Name:\t" + employee.getEmployeeName()); 
      System.out.println("Employee Id:\t" + employee.getEmployeeId()); 
      System.out.println("E-mail:\t"+ employee.getEmployeeEmail()); 
     } 

    } 
    public Employee get(String name){ 
     return map.get(name); 
    } 
    /*public void searchByName() 
    { 
     //(for(Employee e : map.values()) {...}) 
     //and check for each employee if his/her email matches the searched value 
     for(Employee e : map.values()) 
     { 
      System.out.println(e); 
      map.equals(getClass()); 

     } 
    }*/ 
//******************************************************************** 
    public Employee searchByName(String name) 
    { 
     Employee employee = map.get(name);  
     System.out.println(employee); 
     return employee; 
    } 
//******************************************************************** 

    public Employee searchByEmail(String email) 
    { 
     for (Employee employee : map.values()) 
     { 
      if (email.equals(employee.getEmployeeEmail())) 
      { 
       System.out.println(employee); 
       return employee; 
      } 
     } 
     return null; 
    } 
//******************************************************************** 





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


} 

Employee類。

//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 "\t\t\tEmployee\n" + 
       "********************************************************************\n"+ 
       "Employee Name: "+ employeeName +"\n"+ 
       "Employee Id: " + employeeId +"\n"+ 
       "Employee Email: " + employeeEmail; 
    } 
//******************************************************************** 





} 
+0

你想通過emp-name或emp-object進行編輯嗎? – duffy356

+0

你是什麼意思的「編輯方法」?一種方法,可以讓您更改以前存儲的Employee實例? – MarcoS

+0

對不起。是的,我想編輯姓名,身份證和電子郵件。 – Pendo826

回答

1

您也可以使用java的HashMap的put方法。 From the API for HashMap's put method:

如果映射先前包含該鍵的映射,則替換舊值。

所以,像這樣:

public void edit(Employee employee) 
{ 
    map.put(employee.getEmployeeName(), employee); 
} 

然後在其他代碼:

Employee employee = getEmployeeByName("Someniceemployeename"); 
if (employee != null) 
{ 
    employee.setEmployeeEmail("[email protected]"); 
    edit(employee); 
} 

至於編輯僱員的ID,你需要採取一些額外的預防措施。首先,您要確保地圖包含您想要編輯的ID(如往常一樣)。其次,在「編輯」ID時,您需要先從地圖中刪除舊僱員實例(使用舊ID),然後使用put添加新僱員。

+0

哦,對了,我想我會做同樣的ID? – Pendo826

+0

已更新爲ID。我把代碼留給了一些鍛鍊樂趣。 :) –

+0

哈哈謝謝。我不認爲這會很容易,我總是在複雜的Java。 – Pendo826