EmployeeStore的编辑方法(HashMap)

时间:2012-07-27 12:10:38

标签: java hashmap edit

嘿,我的编辑方法运行不正常。我会一步一步告诉你它是如何工作的。 步骤1:用户输入诸如Luis Suarez之类的名称,然后searchByName方法将在Employee Store中搜索此名称。 第2步:用户将再次输入员工详细信息,这次将覆盖他们希望编辑的员工。

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

MainApp

//---------------------------------------------------------------------------------------
//              Name:        Case 4: Edit.
//              Description: Choice 4 gives the user an option to edit the employee's in the store.
//                           This consists of changing the employee's name,id and e-mail address.
//---------------------------------------------------------------------------------------
            case 5:
                System.out.println("Edit");
                Employee employeeEdit = MenuMethods.userInputByName();
                Store.searchByName(employeeEdit.getEmployeeName());
                if (employeeEdit != null) 
                {
                    employeeEdit.setEmployeeName("Joe");
                    employeeEdit.setEmployeeId(1);
                    employeeEdit.setEmployeeEmail("webmail.com");
                    Store.edit(employeeEdit);
                }
                break;

UserInputByName

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

    }

修改

// ---------------------------------------------------------------------------------------
    // Name: Edit.
    // ---------------------------------------------------------------------------------------
    public void edit(Employee employee) 
    {
        map.put(employee.getEmployeeName(), employee);
    }

员工

//---------------------------------------------------------------------------------------
//  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;
    }
//---------------------------------------------------------------------------------------
}

SearchByName

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

2 个答案:

答案 0 :(得分:2)

看看这个小型演示:

HashMap<String, Employee> map = new HashMap<>();
map.put("Pendo826", new Employee("Pendo826", 1, "Pendo826@gmail.com"));

Employee e = map.get("Pendo826"); // get emp instance by name
e.setEmployeeName("Pendo"); // emp name of that instance edited 
System.out.println(map.get("Pendo826").getEmployeeName()); // name is changed within map

只需案例5

System.out.println("Edit");
Employee employeeEdit = MenuMethods.userInputByName();
Employee e = Store.searchByName(employeeEdit.getEmployeeName());
if (e != null) 
{
  e.setEmployeeName("Joe");
  e.setEmployeeId(1);
  e.setEmployeeEmail("webmail.com");
  // Store.edit(employeeEdit); // no need as you already have made changes to reference e
}
break;

之后,如果您查看全部,您将获得更改

答案 1 :(得分:1)

由于商店按名称操作员工,因此在调用编辑时不应更改员工姓名。

  1. 如果您致电编辑,为员工提供尚未存储的名称,则会插入新员工。

  2. 如果您致电编辑,为员工提供已存储的姓名 - 正在更新具有此名称的员工。