0

我对这种方法有疑问。当用户要求时,它不会输出正确的搜索。

这是我的代码:

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);

}
4

3 回答 3

1

问题是您的程序中没有这样的 if 条件:

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(employee);

它将打印员工对象,直到找到匹配项,当匹配时它将返回该员工对象..

于 2012-07-21T13:21:13.090 回答
0

你应该这样说

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

不需要创建Employee对象的新实例。

于 2012-07-21T12:53:57.270 回答
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);
于 2012-07-21T12:55:09.753 回答