嘿,任何人都可以帮助我了解如何为我的公司应用程序执行编辑方法。我之前曾向搜索方法寻求过帮助。我认为编辑方法可能涉及搜索方法。
这是我的代码:
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;
}
//********************************************************************
}
答案 0 :(得分:1)
您也可以使用java的HashMap的put方法。 From the API for HashMap's put method:
如果地图以前包含此键的映射,则替换旧值。
所以,比如:
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,您需要采取一些额外的预防措施。首先,您要确保地图包含您要编辑的ID(照例)。第二,当&#34;编辑&#34;首先从地图中删除旧员工实例(使用旧ID)所需的ID,然后使用put添加新员工。