java - EmployeeStore 中的编辑方法

标签 java hashmap

嘿,我在 EmployeeStore 中的编辑方法遇到了问题。基本上,我试图要求用户按姓名在 map 中搜索员工,然后我想打印出他们搜索的员工,然后我想让用户编辑员工类的 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);
    }


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


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


}

最佳答案

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

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

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

<小时/>

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

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

你只需要找到一种方法来实现神奇的getStore()方法,这样你就可以获得可以提供员工的员工商店的实例。

关于java - EmployeeStore 中的编辑方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11484872/

相关文章:

java - DropWizard 认证领域

java - ArrayList<X> 作为 HashMap 中的键,但 X[] 数组不起作用

Java 通用 HashMap 实现 : Object cannot be converted V

java - 打印 HashMap 中的所有数据

java - 通过变量从 HashMap 中获取键

java - 从 HAPI FHIR 中创建操作的 MethodOutcome/结果中提取 HTTP 状态代码

java - 找不到类,为什么?

java - 无法找到我从 Chrome 浏览器的检查窗口中看到的带有 Selenium 的 WebElements

java - Actionbarsherlock 在 Android 4.x 中不会显示菜单项

object - 如何使用 LLVM 实现动态对象?