1

我正在制作一个 EmployeeStore,它将存储姓名、dob、id、电子邮件地址等......我需要编写一个编辑方法。我已经用谷歌搜索了,但我找不到如何做到这一点,有人可以帮忙吗?这是我的代码:

//Imports.
import java.util.Scanner;
//********************************************************************  
public class MainApp
{
    private static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args)
    {
        new MainApp().start();

    }
    public void start()
    {
        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"));
//Test Code with the new Hashmap.       
        /*Store.print();
        Store.clear();
        Store.print();

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

        Store.print();
        Store.remove("Andy Carroll");
        Store.print();*/
//********************************************************************  
        //Switch Statement for use of a menu.
         int choice;
            do {
                choice = getMenuChoice("1.\tLibrarian\n2.\tPublic User\n3.\tExit\n\n", 3, "Please enter choice:", "Error [1,3] only");
                switch (choice) {
                    case 1:
                        System.out.println("Librarian Functionality...\n");
                        break;
                    case 2:
                        System.out.println("Public User functionality...\n");

                        break;
                    case 3:
                        System.out.println("Program Finished");

                }
            }
            while (choice != 3);
}
//********************************************************************  
      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;
       }
//********************************************************************  



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


//Imports:

//********************************************************************
//Employee Class.
public class Employee
{
//Variables.
    private String employeeName;
    private int employeeId;
    private String employeeEmail;
//********************************************************************  
//Constructor.
    public Employee(String employeeName, int employeeId, String employeeEmail) 
    {
        this.employeeName = employeeName;
        this.employeeId = employeeId;
        this.employeeEmail = employeeEmail;
    }
//********************************************************************
//Getters.
    public String getEmployeeEmail() {
        return employeeEmail;
    }
    public void setEmployeeEmail(String employeeEmail) {
        this.employeeEmail = employeeEmail;
    }
    public String getEmployeeName() {
        return employeeName;
    }
    public int getEmployeeId() {
        return employeeId;
    }
//********************************************************************
//toString method.
    public String toString() {
        return "Employee [employeeName=" + employeeName + ", employeeId="
                + employeeId + ", employeeEmail=" + employeeEmail + "]";
    }
//********************************************************************





}
//Imports.
import java.util.HashMap;
//********************************************************************
import java.util.Map;

public class EmployeeStore 
{
    HashMap<String, Employee> map;

//Constructor.  
    public EmployeeStore()
    {
        map = new HashMap<String,Employee>();
    }
//********************************************************************
//Hashmap Methods.
//Add to the Hashmap : Employee.
    public void add(Employee obj)
    {

        map.put(obj.getEmployeeName(), obj);
    }
//********************************************************************
//Remove from the Hashmap : Employee.
    public void remove(String key)
    {
      //Remove the Employee by name.
        map.remove(key);
    }
//********************************************************************
//Clear the Hashmap : Employee.
    public void clear()
    {
        map.clear();
    }
    //********************************************************************
//Print the Hashmap : Employee. 
    public void print()
    {
        System.out.println("\n********Employee's in the Company.********");
        for (Employee employee : map.values())
        {
            System.out.println("Employee Name:\t" + employee.getEmployeeName());
            System.out.println("Employee Id:\t" + employee.getEmployeeId());
            System.out.println("E-mail:\t"+ employee.getEmployeeEmail());
        }

    }


//********************************************************************  
//********************************************************************


}
4

2 回答 2

2

您需要从 HashMap 中获取 Employee 对象,然后修改该对象。例如,要更改电子邮件:

//in class EmployeeStore
String email = somehowGetNewEmail();
Employee toEdit = map.get(somehowGetName());
toEdit.setEmail(email)

交替:

//in EmployeeStore
public Employee get(String name){
    return map.get(name);
}

//in any class with reference to an EmployeeStore "store"
store.get(name).editSomething(something);
于 2012-06-27T14:04:14.230 回答
0

HashMap 存储对对象的引用。这意味着当您从 HashMap 读取(“获取”)对象并对其属性进行更改时,更改将被保留,而无需您将其写回 HashMap。

因此,您所要做的所有编辑方法就是调用 map.get(name) 并对返回的 Employee 对象进行更改。请注意,您不能以这种方式更改 HashMap 的键。为了“重命名”员工,您必须从哈希映射中删除旧键的值并将其插入到新键下。

于 2012-06-27T14:08:16.650 回答