0

嘿,我在 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;

菜单方法

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

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



}

员工商店

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


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


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


}
4

1 回答 1

2

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

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

编辑由编辑器完成,它将使用 stores create 方法(用于新项目)或 read 和 update 方法(用于编辑现有项目)


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

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

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

于 2012-07-14T15:24:46.750 回答