0

ユーザーが従業員名を入力して従業員を削除しようとすると、アプリケーションが従業員の削除に失敗するため、削除メソッドが機能していないようです。起こるはずのことは次のとおりです。

  1. ユーザーは、私が作成した userInputByName というメソッドを使用して従業員の名前を入力します。
  2. アプリケーションは、店舗でこの従業員を検索します。
  3. 従業員が削除されます。

従業員がまだそこにいるストアを印刷すると、ステップ 3 が機能しません。

私のコードをお見せします。

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 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;
    }
//---------------------------------------------------------------------------------------
}

削除方法

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<String, Employee> map;
    private static Scanner keyboard = new Scanner(System.in);

    public EmployeeStore() {
        map = new HashMap<String, Employee

名前で検索

// ---------------------------------------------------------------------------------------
    // Name: Search by Name.
    // //---------------------------------------------------------------------------------------
    public Employee searchByName(String employeeName) {
        Employee employee = map.get(employeeName);
        System.out.println(employee);
        return employee;
    }

ユーザー入力

//---------------------------------------------------------------------------------------
//  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);

    }

ハッシュマップへの追加

//---------------------------------------------------------------------------------------
//   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"));
4

1 に答える 1

2

あなたHashmapはとして宣言されていHashMap<String, Employee> mapます。

そこから何かを削除したい場合は、Stringではなく、を渡す必要がありますEmployee。次のようなものを試してください:

public Employee remove(String key) 
{
    return map.remove(key);
}

HashMapキーを削除する前に、にキーが含まれているかどうかを確認する必要はありません。メソッドが返さnullれます。

編集:これがコンパイル時エラーをスローしなかったことに驚いています。

edit2:わかりました。つまり、オブジェクトを作成しEmployee、それをadd()メソッドに渡します。それは問題ありませんが、あなたはあなたの方法をあなたが作った方法とadd()一致させる必要があります。remove()したがって、実行している場合は、メソッドを次のように表示EmployeeStore.remove(<employee.getEmployeeName())する必要があります。add()

public Employee add(Employee input)
{
    return map.put(input.getEmployeeName(), input);
}

この関数は、Employee以前にその下にオブジェクトが格納されていた場合はオブジェクトを返しKeyますが、その値を無視することを選択できます。このようなメソッドを使用すると、メソッドadd()と一致するはずremove()です。従業員ID番号を持っているのでKey、名前よりも一意である可能性があるため、その整数に変更することもできます。

于 2012-07-25T14:29:57.087 に答える