[英]Delete method not deleting the employee of EmployeeStore
My delete method doesn't seem to be functioning because the application fails to delete the employee when the user attempts to by entering the employees name. 我的删除方法似乎无法正常运行,因为当用户尝试输入员工姓名时,应用程序无法删除该员工。 What is supposed to happen is the following: 应该发生的是以下内容:
Step 3 does not work when I then print out the store the employee is still there. 当我打印出员工仍在那里的商店时,步骤3不起作用。
I will now show you my code. 我现在将向您展示我的代码。
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 雇员
//---------------------------------------------------------------------------------------
// 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;
}
//---------------------------------------------------------------------------------------
}
Delete method 删除方法
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 declaration Hashmap声明
HashMap<String, Employee> map;
private static Scanner keyboard = new Scanner(System.in);
public EmployeeStore() {
map = new HashMap<String, Employee
SearchByName SearchByName
// ---------------------------------------------------------------------------------------
// Name: Search by Name.
// //---------------------------------------------------------------------------------------
public Employee searchByName(String employeeName) {
Employee employee = map.get(employeeName);
System.out.println(employee);
return employee;
}
UserInput 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);
}
Adding to the hashmap 添加到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"));
Your Hashmap
is declared as HashMap<String, Employee> map
. 您的Hashmap
被声明为HashMap<String, Employee> map
。
If you want to remove something from it then you need to pass in a String
, not an Employee
. 如果要从中删除某些内容,则需要传入String
,而不是Employee
。 Try something like: 尝试类似的东西:
public Employee remove(String key)
{
return map.remove(key);
}
There is no need to check if the HashMap
contains the key before removing it. 在删除之前,无需检查HashMap
包含密钥。 The method will return null
for you. 该方法将为您返回null
。
edit: I'm surprised that this didn't throw a compile-time error for you. 编辑:我很惊讶,这并没有为您抛出编译时错误。
edit2: OK. edit2:好的。 So you are creating your Employee
object and then passing that into your add()
method. 因此,您要创建Employee
对象,然后将其传递给add()
方法。 That's fine, but you need to have your add()
method match the remove()
method that you made. 没关系,但你需要让你的add()
方法与你所做的remove()
方法相匹配。 So if you are doing EmployeeStore.remove(<employee.getEmployeeName())
then you should make your add()
method look like this: 因此,如果您正在使用EmployeeStore.remove(<employee.getEmployeeName())
那么您应该使您的add()
方法如下所示:
public Employee add(Employee input)
{
return map.put(input.getEmployeeName(), input);
}
This function does return an Employee
object if there were previously one stored under that Key
, but you can choose to ignore that value. 此功能无法返回的Employee
,如果以前有一个,根据存储的对象Key
,但你可以选择忽略该值。 Having your add()
method like this should make it match your remove()
method. 像这样的add()
方法应该使它与remove()
方法匹配。 Since you have an employee ID number you could even change your Key
to that integer since it will probably be more unique than a name. 由于您有一个员工ID号,您甚至可以将Key
更改为该整数,因为它可能比名称更独特。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.