2012-07-21 68 views
0

我有這個方法的問題。當用戶請求時,它不輸出正確的搜索。searchByEmail方法不起作用

這裏是我的代碼:

System.out.println("Search by Email."); 
Employee employeeSearchEmail = MenuMethods.userInputByEmail(); 
Store.searchByEmail(employeeSearchEmail.getEmployeeEmail()); 

public Employee searchByEmail(String employeeEmail) { 
    for (Employee employee : map.values()) { 
     System.out.println(employee); 
     map.equals(getClass()); 
     map.equals(employee.getEmployeeEmail()); 
     employee = new Employee(employeeEmail); 
     ; 
     return employee; 
    } 
    return null; 
} 

public static Employee userInputByEmail() { 
    // String temp is for some reason needed. If it is not included 
    // The code will not execute properly. 
    String temp = keyboard.nextLine(); 
    Employee e = null; 
    System.out.println("Please enter the Employee Email:"); 
    String employeeEmail = keyboard.nextLine(); 
    // This can use the employeeName's constructor because java accepts the 
    // parameters instead 
    // of the name's. 
    return e = new Employee(employeeEmail); 

} 

回答

1

的問題是,有沒有條件,如果在你的程序是這樣的:

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

這一行: 的System.out.println (僱員);

,直到找到了比賽,當比賽將返回該員工的對象..

+0

當我這樣做,它打印出整個商店。 – Pendo826 2012-07-21 13:24:46

+0

整店意味着所有員工數據 – 2012-07-21 13:26:59

+0

嗯,是所有的員工。 – Pendo826 2012-07-21 13:27:33

0

你應該這樣說

if(employeeEmail.equals(employee.getEmployeeEmail()) return employee; 

沒有必要創建Employee對象的新實例。

0

您想要返回具有特定電子郵件地址的員工。

public Employee searchByEmail(String employeeEmail) 
    { 
      for(Employee employee : map.values()) 
      { 
       if(employee.getEmployeeEmail().equalsIgnoreCase(employeeEmail.trim())) 
          return employee; 
      } 
      return null; 
    } 

順便說一句,什麼是地圖的鍵:因此,如果當前員工的電子郵件地址,等於給定的電子郵件地址,你應該只返回。如果電子郵件地址的關鍵是,你可以簡單地返回:

return map.get(employeeEmail); 
+0

IM在你的代碼的if語句線得到一個錯誤,它會打印Employee對象。 – Pendo826 2012-07-21 12:56:54

+0

沒有名字是關鍵。電子郵件和ID是從員工類繼承的。 – Pendo826 2012-07-21 12:59:25

+0

究竟是什麼錯誤? – Razvan 2012-07-21 12:59:40