在EmployeeStore中编辑方法

时间:2012-07-14 15:15:39

标签: java hashmap

嘿我在EmployeeStore中遇到编辑方法有问题。基本上我试图要求用户按名称在地图中搜索员工,然后我想打印出他们搜索的员工,然后我想让用户编辑员工类的3个变量。谁能帮忙。这是我的代码:

Edit choice in the mainApp
  case 5:
           System.out.println("Edit");
           Employee employee2 = MenuMethods.userInput();
           Store.searchByName(employee2.getEmployeeName());
         if (employee2 != null)
        {
            employee2.setEmployeeName("Joe");
            employee2.setEmployeeId(1);
            employee2.setEmployeeEmail("webmail.com");
           Store.edit(employee2);
           Store.print();
        }

            break;

MenuMethods

//Imports
import java.util.Scanner;
//********************************************************************

public class MenuMethods 
{
    private static Scanner keyboard = new Scanner(System.in);



    //Methods for the Company Application menu.
    //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;
         }
    //********************************************************************
    //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;
            }
    //********************************************************************
    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);

    }
    //********************************************************************
    public static Employee userInputByName()
    {

         Employee employee = null;
         System.out.println("Please enter the Employee Name:");
         String employeeName = keyboard.nextLine();
         return employee = new Employee(employeeName , null, null);

    }
    //********************************************************************



}

EmployeeStore

//Imports.
import java.util.HashMap;
import java.util.Scanner;
//********************************************************************
public class EmployeeStore
{
    HashMap<String, Employee> map;
    private static Scanner keyboard = new Scanner(System.in);

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

        map.put(employee.getEmployeeName(), employee);
    }
//********************************************************************
//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); to print the toString of Employee class
            //or:
            System.out.println("Employee Name:\t" + employee.getEmployeeName());
            System.out.println("Employee Id:\t" + employee.getEmployeeId());
            System.out.println("E-mail:\t"+ employee.getEmployeeEmail());
        }

    }
    public Employee get(String name){
        return map.get(name);
    }
    /*public void searchByName ()
    {
        //(for(Employee e : map.values()) {...}) 
        //and check for each employee if his/her email matches the searched value
        for(Employee e : map.values())
        {
            System.out.println(e);
            map.equals(getClass());

        }
    }*/
//********************************************************************
    public Employee searchByName(String name) 
    {
        Employee employee = map.get(name);    
        System.out.println(employee);
        return employee;
    }
//********************************************************************

    public Employee searchByEmail(String email) 
    {
        for (Employee employee : map.values())
        {
            if (email.equals(employee.getEmployeeEmail()))
            {
                System.out.println(employee);
                return employee;
            }
        }
        return null;
    }
//********************************************************************
    public void edit(Employee employee)
    {
        map.put(employee.getEmployeeName(), employee);
    }


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


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


}

1 个答案:

答案 0 :(得分:2)

(持久性)商店根本不应该有编辑方法。它应该提供方法

  • C - 创建新项目
  • R - 读取项目
  • U - 更新现有项目
  • D - 删除现有项目

编辑由编辑器完成,编辑器将使用商店创建方法(用于新项目)或读取和更新方法(用于编辑现有项目)


对于userInputByName

- 从键盘获取名称并使用商店返回员工(如果有):

System.out.println("Please enter the Employee Name:");
String employeeName = keyboard.nextLine();
return getStore().searchByName();

您只能找到实现魔术getStore()方法的方法,以便获得可以为员工提供的员工商店实例。