- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嘿,我的编辑方法运行不正确。我会一步一步地告诉你它是如何不工作的。第 1 步:用户输入一个名字,例如 Luis Suarez,然后 searchByName 方法将在 Employee Store 中搜索这个名字。第 2 步:用户将再次输入员工详细信息,这次它将覆盖他们希望编辑的员工。
现在我将向您展示我的代码:
主应用
//---------------------------------------------------------------------------------------
// 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;
用户输入名
//---------------------------------------------------------------------------------------
// 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;
}
//---------------------------------------------------------------------------------------
}
按名称搜索
// ---------------------------------------------------------------------------------------
// Name: Search by Name.
// ---------------------------------------------------------------------------------------
public Employee searchByName(String employeeName)
{
Employee employee = map.get(employeeName);
System.out.println(employee);
return employee;
}
最佳答案
看看这个小演示:
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;
之后,如果您查看全部,您将看到更改
关于java - EmployeeStore (HashMap) 的编辑方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11687505/
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我目前正在申请存储员工详细信息,例如姓名、ID 和电子邮件地址。我正在使用 HashMap 来完成此操作。我目前在 searchByName、id 和电子邮件地址方法方面遇到困难。我该如何写一个? 这
嘿,我的编辑方法运行不正确。我会一步一步地告诉你它是如何不工作的。第 1 步:用户输入一个名字,例如 Luis Suarez,然后 searchByName 方法将在 Employee Store 中
嘿,我一直在四处寻找,但找不到一种方法来让 searchByEmail 方法发挥作用。该应用程序存储公司员工的详细信息,例如姓名、ID 和电子邮件。我需要一种通过员工的电子邮件地址搜索 HashMap
嘿,我做了一个编辑方法来编辑 EmployeeStore 的内容,例如编辑姓名、ID 和电子邮件。所以在菜单中我有一个 switch 语句。并在编辑选项中设置 System.out.println(
我是一名优秀的程序员,十分优秀!