1

編集メソッドが正しく実行されていません。それがどのように機能するはずがないかを段階的に説明します。ステップ1:ユーザーがLuis Suarezなどの名前を入力すると、searchByNameメソッドが従業員ストアでこの名前を検索します。ステップ2:ユーザーは再び従業員の詳細を入力し、今度は編集したい従業員を上書きします。

コードを表示します。

MainApp

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

UserInputByName

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

SearchByName

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

2 に答える 2

2

この小さなデモを見てください:

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;

その後、すべてを表示すると、変更があります

于 2012-07-31T06:22:59.640 に答える
1

ストアは従業員を名前で運営しているため、編集を呼び出すときに従業員の名前を変更しないでください。

  1. まだ保存されていない名前を従業員に提供してeditを呼び出すと、新しい従業員が挿入されます。

  2. すでに保存されている名前の従業員を指定してeditを呼び出すと、この名前の従業員が更新されます。

于 2012-07-27T12:22:30.483 に答える