0

I'm making an employee store. The delete function in the employeeStore does not work. Instead of deleting the employee it does nothing can anyone see anything wrong.

Here is my code: Main

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;

Delete method

// ---------------------------------------------------------------------------------------
// Name: Remove.
// ---------------------------------------------------------------------------------------
public Employee remove(Employee key) {
    // Remove the Employee by name.
    if (map.containsKey(key))
        return map.remove(key); // if its there remove and return
    else
        return null; // if its not there return 0x00000000 address
}

Menu methods

//---------------------------------------------------------------------------------------
//  Name:           Imports.
//  Description:    To allow the use of different Java classes.
//---------------------------------------------------------------------------------------
import java.util.Scanner;
//---------------------------------------------------------------------------------------
public class MenuMethods 
{
    private static Scanner keyboard = new Scanner(System.in);
//---------------------------------------------------------------------------------------
//  Methods for the Company Application menu.
//---------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------
//  Name:           getMenuChoice.
//  Description:    Method for validating the choice.
//---------------------------------------------------------------------------------------
    public static int getMenuChoice(String menuString, int limit,String prompt, String errorMessage) 
    {
        System.out.println(menuString);
        int choice = inputAndValidateInt(1, limit, prompt, errorMessage);
        return choice;
    }

//---------------------------------------------------------------------------------------
//  Name:        inputAndValidateInt.
//  Description: This method is used in the getMenuChoice method.
//---------------------------------------------------------------------------------------
    public static int inputAndValidateInt(int min, int max, String prompt,String errorMessage) 
    {
        int number;
        boolean valid;
        do 
        {
            System.out.print(prompt);
            number = keyboard.nextInt();
            valid = number <= max && number >= min;
            if (!valid) 
            {
                System.out.println(errorMessage);
            }
        } while (!valid);
        return number;
    }

//---------------------------------------------------------------------------------------
//  Name:        userInput
//  Description: This method is used in the MainApp to give the user capability to enter
//               the details when adding details of an employee into the store.
//---------------------------------------------------------------------------------------
    public static Employee userInput() 
    {
        String temp = keyboard.nextLine();
        Employee e = null;
        System.out.println("Please enter the Employee Name:");
        String employeeName = keyboard.nextLine();
        System.out.println("Please enter the Employee ID:");
        int employeeId = keyboard.nextInt();
        temp = keyboard.nextLine();
        System.out.println("Please enter the Employee E-mail address:");
        String employeeEmail = keyboard.nextLine();
        return e = new Employee(employeeName, employeeId, employeeEmail);

    }

//---------------------------------------------------------------------------------------
//  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:        userInputByEmail
//  Description: This method is used in the MainApp to give the user capability to search by email.
//---------------------------------------------------------------------------------------
    public static String userInputByEmail() 
    {
        // 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 Email:");
        String employeeEmail = keyboard.nextLine();
        // This can use the employeeName's constructor because java accepts the
        // parameters instead
        // of the name's.
        return employeeEmail;

    }
//---------------------------------------------------------------------------------------

}

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;
    }
//---------------------------------------------------------------------------------------
}
1
  • How is the employee map created? Do you store the employee object as both key and value? Commented Jul 23, 2012 at 15:34

3 Answers 3

3

This method

public static Employee userInputByName

is creating a new Employee. (It's also assigning it to a local variable pointlessly... the code style is really weird here - as is the extraneous call to nextLine().)

Unless you've overridden equals and hashCode in Employee, it won't consider that new Employee object to be equal to the existing one within the HashMap. Unfortunately you haven't shown us the Employee class, but presumably it's meant to compare employees as equal (and give the same hash code) if they have the same name.

Sign up to request clarification or add additional context in comments.

2 Comments

i'll edit employee into the question. I really dont understand what you mean by the equals and hashcode. By this i mean i dont know how there involved.
@Pendo826 How do you think containsKey() tests keys for equality? Overriding equals() and hashCode() means that you get to determine what is or isn't equal. Otherwise, equals() and hashCode() (and by extension, containsKey()) basically have no choice but to compare based on Object reference.
1

All you need to do is

public Employee remove(Employee key) {
    // Remove the Employee by name.
    return map.remove(key); // if its there remove and return
}

the most common cause of this problem is that either hashCode and equals are not correct, do not match or depend on a mutable field.

Comments

1

The classical: you are probably missing equals() and hashcode() on the Employee object. You should also consider using a simple key for your map, like employee.id: like it stands now, your EmployeeMap can better be modeled as an EmployeeSet.

Looking further in your code, you probably want to create several Maps as indexes for your employee objects:

Map<Integer, Employee> employeeById
Map<String, Employee> employeeByEmail;
MultiMap<String, Employee> employeeByName; 

note the suggested com.google.common.collect.Multimap on the last line, to potentially hold several employee references that share the same John Doe name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.