java - 员工商店的编辑方法。(使用Hashmap)

标签 java methods hashmap edit

嘿,任何人都可以帮助我了解如何为我的公司应用程序执行编辑方法。我之前曾寻求过有关搜索方法的帮助。而且我认为编辑方法可能会涉及到搜索方法。

这是我的代码:

EmployeeStore.

//Imports.
import java.util.HashMap;
//********************************************************************
public class EmployeeStore
{
    HashMap<String, Employee> map;

//Constructor.  
    public EmployeeStore()
    {
        map = new HashMap<String,Employee>();
    }
//********************************************************************
//Hashmap Methods.
//Add to the Hashmap : Employee.
    public void add(Employee employee)
    {

        map.put(employee.getEmployeeName(), employee);
    }
//********************************************************************
//Remove from the Hashmap : Employee.
    public void remove(String key)
    {
      //Remove the Employee by name.
        map.remove(key);
    }
//********************************************************************
//Clear the Hashmap : Employee.
    public void clear()
    {
        map.clear();
    }
    //********************************************************************
//Print the Hashmap : Employee. 
    public void print()
    {
        System.out.println("\n********Employee's in the Company.********");
        for (Employee employee : map.values())
        {
            //System.out.println(employee); to print the toString of Employee class
            //or:
            System.out.println("Employee Name:\t" + employee.getEmployeeName());
            System.out.println("Employee Id:\t" + employee.getEmployeeId());
            System.out.println("E-mail:\t"+ employee.getEmployeeEmail());
        }

    }
    public Employee get(String name){
        return map.get(name);
    }
    /*public void searchByName ()
    {
        //(for(Employee e : map.values()) {...}) 
        //and check for each employee if his/her email matches the searched value
        for(Employee e : map.values())
        {
            System.out.println(e);
            map.equals(getClass());

        }
    }*/
//********************************************************************
    public Employee searchByName(String name) 
    {
        Employee employee = map.get(name);    
        System.out.println(employee);
        return employee;
    }
//********************************************************************

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





//********************************************************************  
//********************************************************************


}

员工类别。

//Imports:

//********************************************************************
//Employee Class.
public class Employee
{
//Variables.
    private String employeeName;
    private int employeeId;
    private String employeeEmail;
//********************************************************************  
//Constructor.
    public Employee(String employeeName, int employeeId, String employeeEmail) 
    {
        this.employeeName = employeeName;
        this.employeeId = employeeId;
        this.employeeEmail = employeeEmail;
    }
//********************************************************************
//Getters.
    public String getEmployeeEmail() {
        return employeeEmail;
    }
    public void setEmployeeEmail(String employeeEmail) {
        this.employeeEmail = employeeEmail;
    }
    public String getEmployeeName() {
        return employeeName;
    }
    public int getEmployeeId() {
        return employeeId;
    }
//********************************************************************
//toString method.
    public String toString() {
        return "\t\t\tEmployee\n" +
                "********************************************************************\n"+
                "Employee Name: "+ employeeName +"\n"+ 
                "Employee Id: " + employeeId +"\n"+  
                "Employee Email: " + employeeEmail;
    }
//********************************************************************





}

最佳答案

您也可以使用 java 的 HashMap 的 put 方法来实现此目的。 From the API for HashMap's put method:

If the map previously contained a mapping for this key, the old value is replaced.

所以,类似:

public void edit(Employee employee)
{
    map.put(employee.getEmployeeName(), employee);
}

然后在其他代码中:

Employee employee = getEmployeeByName("Someniceemployeename");
if (employee != null)
{
    employee.setEmployeeEmail("awesomeness@stackoverflow.com");
    edit(employee);
}

对于编辑员工的 ID,您需要采取一些额外的预防措施。首先,您要确保 map 包含您要编辑的 ID(像往常一样)。其次,当“编辑”ID 时,您需要首先从 map 中删除旧员工实例(使用旧 ID),然后使用 put 添加新员工。

关于java - 员工商店的编辑方法。(使用Hashmap),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11311691/

相关文章:

java - 在 SPRING 中设置 ValidationMessages.properties 的路径

java - 为什么这些 BST 方法都不起作用?

java - 反转整数但得到 `StringIndexOutOfBoundsException`

java - 为什么 ChannelHandlerContext.writeAndFlush() 不处理我的字符串?

javascript - JS : why do i need brackets to call a method on number?

java - 关于Oracle的java在线教程中使用HashMap存储anagram的例子

Java 对 List<Value> 的 HashMap 值进行排序

java - 我什么时候应该使用哈希而不是类

java - 你能用Java实例化一个接口(interface)吗

c# - C# 中的 "static method"是什么?