Java 我的EmployeeStore中的MainApp出错
嘿,我制作了一个编辑方法来编辑EmployeeStore的内容,比如编辑姓名、id和电子邮件。所以在菜单中我有一个switch语句。在编辑选项im设置中Java 我的EmployeeStore中的MainApp出错,java,hashmap,Java,Hashmap,嘿,我制作了一个编辑方法来编辑EmployeeStore的内容,比如编辑姓名、id和电子邮件。所以在菜单中我有一个switch语句。在编辑选项im设置中 System.out.println("Edit"); Employee employee2 = MenuMethods.userInput(); Store.searchByName(employee2); if (employee != null) {
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方法(字符串)不适用于参数(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;
}
//********************************************************************
publicstaticemployeeuserinput()
{
String temp=keyboard.nextLine();
员工e=null;
System.out.println(“请输入员工姓名:”);
字符串employeeName=keyboard.nextLine();
System.out.println(“请输入员工ID:”);
int employeeId=keyboard.nextInt();
temp=键盘.nextLine();
System.out.println(“请输入员工电子邮件地址:”);
字符串employeeEmail=keyboard.nextLine();
return e=新员工(employeeName、employeeId、employeeEmail);
}
//********************************************************************
//用于验证选择的方法。
公共静态int getMenuChoice(字符串菜单串、int限制、字符串提示、字符串错误消息)
{
System.out.println(菜单显示);
int choice=inputAndValidateInt(1,限制,提示,错误消息);
回报选择;
}
//********************************************************************
//此方法在getMenuChoice方法中使用。
公共静态int-inputAndValidateInt(int-min、int-max、字符串提示、字符串错误消息)
{
整数;
布尔有效;
做{
系统输出打印(提示);
number=键盘.nextInt();
有效=数量=分钟;
如果(!有效){
System.out.println(错误消息);
}
}而(!有效);
返回号码;
}
//********************************************************************
错误消息准确地告诉您出了什么问题:
EmployeeStore类型中的searchByName(字符串)方法不可用
适用于参数(员工)
您的方法searchByName
在向其传递Employee
对象时接受参数String
您的代码中有一些,其中之一:
case 5:
System.out.println("Edit");
Employee employee2 = MenuMethods.userInput();
Store.searchByName(employee2);
您正在传入employee2
,类型为Employee
您的searchByName
方法实现在哪里?
方法名“byName
”表示您希望按名称字符串
进行搜索,而不是按对象
进行搜索。因此,简单的解决方案可能会:
Store.searchByName(employee2.getName());
另一方面,您的代码看起来很糟糕/设计很糟糕。,
MenuMethods.getMenuChoice()
?抱歉,我一直忘了添加它。我现在就编辑它。你想更改什么?是的,可以,但我需要用户首先搜索他们想要的员工姓名。这听起来像是一个完全不同的问题,你可以打开另一个线程,请在代码上设置更好的格式,只发布相关的代码块。同时以清晰、简洁的方式提出你的问题。