EmployeeStore の edit メソッドに問題があります。基本的に、名前で従業員のマップを検索するようにユーザーに依頼しようとしています。次に、検索した従業員を印刷してから、ユーザーに従業員クラスの 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);
}
//********************************************************************
}
EmployeeStore
//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);
}
//********************************************************************
//********************************************************************
//********************************************************************
}