2012-07-21 140 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