删除方法不删除EmployeeStore的员工

时间:2012-07-25 14:17:49

标签: java hashmap

我的删除方法似乎无法正常运行,因为当用户尝试输入员工姓名时,应用程序无法删除该员工。应该发生的事情如下:

  1. 用户使用我编写的名为userInputByName的mnethod输入员工的姓名。
  2. 应用程序在商店中搜索此员工。
  3. 员工已被删除。
  4. 当我打印出员工仍在那里的商店时,步骤3不起作用。

    我现在将向您展示我的代码。

    MainApp()
    
    //---------------------------------------------------------------------------------------
    //  Name:        Case 3: Delete by Name.
    //  Description: Choice 3 gives the user an option to delete an employee by name.
    //---------------------------------------------------------------------------------------
                case 3:
                    System.out.println("Delete by Name.");
                    Employee employeeDelete = MenuMethods.userInputByName();
                    Store.searchByName(employeeDelete.getEmployeeName());
                    System.out.println("Your choice is: "+ employeeDelete);
                    Store.remove(employeeDelete);
                    break;
    

    员工

    //---------------------------------------------------------------------------------------
    //  Employee class.
    //---------------------------------------------------------------------------------------
    public class Employee
    {
    //---------------------------------------------------------------------------------------
    //  Variables to be used in the employee store.
    //---------------------------------------------------------------------------------------
        private String employeeName;
        private int employeeId;
        private String employeeEmail;
    //---------------------------------------------------------------------------------------
    //  Name:        Constructors.
    //  Description:
    //---------------------------------------------------------------------------------------
        public Employee(String employeeName, int employeeId, String employeeEmail) 
        {
            this.employeeName = employeeName;
            this.employeeId = employeeId;
            this.employeeEmail = employeeEmail;
        }
    //---------------------------------------------------------------------------------------
    //  Overloading the constructor for the use with userInputByName method.
    //---------------------------------------------------------------------------------------
        public Employee(String employeeName) 
        {
            this.employeeName = employeeName;
        }
    //---------------------------------------------------------------------------------------
    //  Name:   Getters.
    //---------------------------------------------------------------------------------------
        public String getEmployeeEmail() 
        {
            return employeeEmail;
        }
    
        public String getEmployeeName() 
        {
            return employeeName;
        }
        public int getEmployeeId() 
        {
            return employeeId;
        }
    //---------------------------------------------------------------------------------------
    //  Name:   Setters.
    //---------------------------------------------------------------------------------------
        public void setEmployeeEmail(String employeeEmail) 
        {
            this.employeeEmail = employeeEmail;
        }
        public void setEmployeeName(String employeeName) 
        {
            this.employeeName = employeeName;
        }
        public void setEmployeeId(int employeeId)
        {
            this.employeeId = employeeId;
        }
    
    //---------------------------------------------------------------------------------------
    //  Name:   toString.
    //---------------------------------------------------------------------------------------
        public String toString() 
        {
            return "\t\t\tEmployee\n" +
                    "********************************************************************\n"+
                    "Employee Name: "+ employeeName +"\n"+ 
                    "Employee Id: " + employeeId +"\n"+  
                    "Employee Email: " + employeeEmail;
        }
    //---------------------------------------------------------------------------------------
    }
    

    删除方法

    public Employee remove(Employee key) {
            // Remove the Employee by name.
            if (map.containsKey(key))
                return map.remove(key); // if it is there remove and return.
            else
                return null; // if its not there return nothing.
        }
    

    Hashmap声明

    HashMap<String, Employee> map;
        private static Scanner keyboard = new Scanner(System.in);
    
        public EmployeeStore() {
            map = new HashMap<String, Employee
    

    SearchByName

    // ---------------------------------------------------------------------------------------
        // Name: Search by Name.
        // //---------------------------------------------------------------------------------------
        public Employee searchByName(String employeeName) {
            Employee employee = map.get(employeeName);
            System.out.println(employee);
            return employee;
        }
    

    UserInput

    //---------------------------------------------------------------------------------------
    //  Name:        userInputByName.
    //  Description: This method is used in the MainApp to give the user capability to search by name.
    //---------------------------------------------------------------------------------------
        public static Employee userInputByName() 
        {
            // 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 Name:");
            String employeeName = keyboard.nextLine();
    
            return e = new Employee(employeeName);
    
        }
    

    添加到hashmap

    //---------------------------------------------------------------------------------------
    //   Create a Store named Store and add Employee's to the Store.
    //---------------------------------------------------------------------------------------
            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"));
    

1 个答案:

答案 0 :(得分:2)

您的Hashmap被声明为HashMap<String, Employee> map

如果您想从中移除某些内容,则需要传递String,而不是Employee。尝试类似:

public Employee remove(String key) 
{
    return map.remove(key);
}

在删除密钥之前,无需检查HashMap是否包含密钥。该方法将为您返回null

编辑:我很惊讶这并没有给你带来编译时错误。

edit2:好的。因此,您要创建Employee对象,然后将其传递到add()方法中。这样很好,但您需要使用add()方法匹配您所做的remove()方法。因此,如果您正在执行EmployeeStore.remove(<employee.getEmployeeName()),那么您应该使add()方法看起来像这样:

public Employee add(Employee input)
{
    return map.put(input.getEmployeeName(), input);
}

如果之前存储了一个Employee对象,则此函数会返回Key对象,但您可以选择忽略该值。使用这样的add()方法可以使其与remove()方法匹配。由于您拥有员工ID号,您甚至可以将Key更改为该整数,因为它可能比名称更独特。