将用户输入添加到Employee Store

时间:2012-07-13 16:58:28

标签: java hashmap add

我制作了一个EmployeeStore来显然存储员工详细信息,例如姓名,身份证和电子邮件。我设置了一个菜单,现在每当用户想要添加一名员工时,我就会陷入困境。 这是我的代码:

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 void userInput()
    {

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


}

添加方法:

//Add to the Hashmap : Employee.
    public void add(Employee employee)
    {

        map.put(employee.getEmployeeName(), employee);
    }
//********************************************************************

MainApp:

//Imports.
import java.util.Scanner;
//********************************************************************  
public class MainApp
{
    //The Scanner is declared here for use throughout the whole MainApp.
    private static Scanner keyboard = new Scanner(System.in);

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

    }
    public void start()
    {
//Create a Store named Store and add Employee's to the Store.
        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.
        Store.searchByName("James O' Carroll");
        Store.print();
        Store.searchByEmail("gmail.com");
        Employee andy = Store.searchByEmail("hotmail.com");
        System.out.println(andy);
        Employee employee = Store.searchByName("James O' Carroll");
        if (employee != null)
        {
            employee.setEmployeeName("Joe");
            employee.setEmployeeId(1);
            employee.setEmployeeEmail("webmail.com");
           Store.edit(employee);
           Store.print();
        }*/
//********************************************************************      

        int choice ;
        System.out.println("Welcome to the Company Database.");
        do
        {
         choice = MenuMethods.getMenuChoice(
                "1.\tView All" +
                "\n2.\tAdd" +
                "\n3.\tDelete" +
                "\n4.\tDelete All " +
                "\n5.\tEdit" +
                "\n6.\tSearch" +
                "\n7.\tPrint"+
                "\n8.\tExit", 8, "Please enter your choice:", "Error [1,8] Only");
         //String temp = keyboard.nextLine();  This prevented entering the choice.
        switch (choice) 
        {
            case 1:
                 System.out.println("View All");
                Store.print();

                break;

        case 2:
             System.out.println("Add");
                //Store.add();

                break;

        case 3:
             System.out.println("Delete");
                //Store.delete();


                break;

        case 4:
                System.out.println("Delete All");
                Store.clear();

                break;
        case 5:
           System.out.println("Edit");

            break;
        case 6:
             System.out.println("Search");
            Store.clear();

            break;
        case 7:
             System.out.println("Print");
            Store.print();

            break;
        case 8:
             System.out.println("Exit");

            break;
        }


        } while (choice != 8);

     }
}

1 个答案:

答案 0 :(得分:1)

在添加员工之前,我认为您错过了接下来的步骤。向用户询问员工姓名和电子邮件,然后将其添加到您的商店。