gpt4 book ai didi

java - EmployeeStore 中的编辑方法

转载 作者:行者123 更新时间:2023-12-02 07:41:45 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com