0

嘿,我创建了一个 Edit 方法来编辑 EmployeeStore 的内容,例如编辑姓名、ID 和电子邮件。所以在菜单中我有一个 switch 语句。并在编辑选项即时设置

 System.out.println("Edit");
           Employee employee2 = MenuMethods.userInput();
           Store.searchByName(employee2);
         if (employee != null)
        {
            employee.setEmployeeName("Joe");
            employee.setEmployeeId(1);
            employee.setEmployeeEmail("webmail.com");
           Store.edit(employee);
           Store.print();

但问题是方法 searchByName 有一个错误,内容为:EmployeeStore 类型中的方法 searchByName(String) 不适用于参数 (Employee)。我不知道这有什么问题,我在我的 MainApp 中对 add 方法使用相同的步骤。

这是我的代码。

主应用

//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");
                Employee employee = MenuMethods.userInput();
                Store.add(employee);

                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");
           Employee employee2 = MenuMethods.userInput();
           Store.searchByName(employee2);
         if (employee != null)
        {
            employee.setEmployeeName("Joe");
            employee.setEmployeeId(1);
            employee.setEmployeeEmail("webmail.com");
           Store.edit(employee);
           Store.print();

            break;
        case 6:
             System.out.println("Search");
             Employee employee1 = MenuMethods.userInput();
             Store.searchByName(employee1);


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

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

            break;
        }


        } while (choice != 8);

     }
}

编辑方法

public void edit(Employee employee)
    {
        map.put(employee.getEmployeeName(), employee);
    }

用户输入法

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

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

1 回答 1

2

错误消息准确地说明了问题所在:

EmployeeStore 类型中的方法 searchByName(String) 不适用于参数 (Employee)

您的方法searchByName需要 parameter String,而您将其传递给Employee对象。

您的代码中有一些,其中之一:

 case 5:
           System.out.println("Edit");
           Employee employee2 = MenuMethods.userInput();
           Store.searchByName(employee2);

你正在传入employee2,它的类型Employee

你的searchByName方法实现在哪里?方法名称“ byName”建议您按名称搜索String,而不是object. 如此简单的解决方案可能会:

Store.searchByName(employee2.getName());

在旁注中,您的代码看起来很糟糕/糟糕的设计。

于 2012-07-13T19:24:51.763 回答