gpt4 book ai didi

java - 从控制台菜单调用继承该方法的类的方法

转载 作者:行者123 更新时间:2023-12-01 17:00:33 25 4
gpt4 key购买 nike

我遇到了一个问题,我不确定我错过了什么。我将在这里添加大量代码,其中大部分可能不需要回答我的问题。

我有一个名为 Employee 的类,它只是 Employee 类型对象的类:


class Employee extends ObjectIDs implements Output {

private String firstName;
private String lastName;

private static final String VALID_EMPLOYEE_NAME = "^[a-zA-Z]{2,50}$";

Employee(Integer id, String firstName, String lastName) throws Exception {
super(id);
this.setNames(firstName, lastName);
}

private void setNames(String firstName, String lastName) throws Exception {
if ( !firstName.matches(VALID_EMPLOYEE_NAME) || !lastName.matches(VALID_EMPLOYEE_NAME)) {

throw new IllegalArgumentException (Store.ERR_PREFIX+"Please use english letters only ");
}
this.firstName = firstName;
this.lastName = lastName;
}

String getNames () {
return this.firstName+" "+lastName;
}

public String createOutput() {
return this.id+","+this.firstName+","+this.lastName;
}
}

我有一个 Store 类,在其中我正在创建一个员工列表和一堆其他东西。所有其他类的所有方法都在那里。例如,方法 newEmployeeremoveEmployee 就在其中,访问我提到的列表等。它还将包括 Product 等类的其他方法> 但这只是一个例子。这是 Store 类:


class Store {

private ArrayList<Employee> employees = new ArrayList<>();
private ArrayList<Product> products = new ArrayList<>();

static final String EMPLOYEES_FILE_PATH = "employees.csv";
static final String PRODUCTS_FILE_PATH = "items.csv";
static final String EMPLOYEES_LIST = "employees";
static final String PRODUCT_LIST = "prodcuts";
static final String ERR_PREFIX = "*********\nERROR\n*********\n";

Employee newEmployee(Integer id ,String firstName, String lastName) throws Exception { //Create a new employee
for ( Employee e : employees ) {
if (id == e.id) {
throw new IllegalArgumentException(ERR_PREFIX + "This employee ID already exist.");
}
}
Employee e = new Employee(id, firstName, lastName);
this.employees.add(e);
System.out.println("Employee "+e.createOutput()+" added to the employees list");
return e;
}

void deleteEmployee(Integer id) throws Exception { //Remove Employee
for(int i = 0; i<employees.size(); i++) {
if(employees.get(i).id == id) {
System.out.println("Employee "+employees.get(i).createOutput()+" was deleted sucsessfully.");
employees.remove(i);
return;
}
}
throw new IllegalArgumentException("Sorry, no such employee.");
}
}

我有另一个类正在创建菜单。它基本上是一个调用子菜单的菜单,我想从那里执行方法。

import java.util.Scanner;

public class Menu {
/**
* @param args
*/
public static void main(String[] args) {
Menu menu = new Menu();
menu = menu.mainMenu(menu);
System.out.println("Thanks for visiting");
}

private Menu mainMenu(Menu menu) {
System.out.println("Welcome to my store");
int selection = 0;

do {
System.out.println("[1] I'm a customer");
System.out.println("[2] I'm an employee");
System.out.println("[3] Quit");

System.out.print("Insert selection: ");
Scanner sc = new Scanner(System.in);
selection = sc.nextInt();

switch (selection) {
case 1: return menu.customerSubMenu(menu);
case 2: return menu.employeeSubMenu(menu);
case 3: return menu;
default:
System.out.println("The selection was invalid!");
}
} while (selection != 3);
return menu;
}

private Menu customerSubMenu(Menu menu) { //Customer options submenu
System.out.println("Welcome dear customer");

int selection = 0;

do {
System.out.println("[1] Register as new customer");
System.out.println("[2] Buy an item");
System.out.println("[3] Return");

System.out.print("Insert selection: ");
Scanner sc = new Scanner(System.in);
//selection = ++testint
selection = sc.nextInt();

switch (selection) {
// case 1: return
// case 2: return
// case 3:
default:
System.out.println("The selection was invalid!");
}
} while (selection != 3);
return menu;
}

private Menu employeeSubMenu(Menu menu) { //Employees options submenu
System.out.println("Welcome employee");

int selection = 0;

do {
System.out.println("[1] Add a product");
System.out.println("[2] Return a product");
System.out.println("[3] Add an employee");
System.out.println("[4] Show all employees");
System.out.println("[5] Return");

System.out.print("Insert selection: ");
Scanner sc = new Scanner(System.in);
// selection = ++testint;
selection = sc.nextInt();

switch (selection) {
// case 1: return
// case 2: return
// case 3: return
case 5:
return menu.mainMenu(menu);
default:
System.out.println("Invalid selection");
}
} while (selection != 5);
return menu;
}
}

如果您向下滚动到 employeeSubMenu,您将看到我注释掉的 case 3,我想用它来运行 newEmployee 方法位于 Store 类中。问题是,我做错了什么。它不允许我这样做,而且我无法使用 Store.newEmployee 或它的直接名称来调用它。问题是为什么。

我很想获得一些帮助。

如果可能的话,请假设我在您的回答中对 Java 非常陌生,因为我真的只是在学习。

最佳答案

更新您的 Store 类以使用 static 方法,并使用 Store.newEmployee(....) 从开关中的案例进行调用

class Store {

private static ArrayList<Employee> employees = new ArrayList<>();
private static ArrayList<Product> products = new ArrayList<>();

private static final String EMPLOYEES_FILE_PATH = "employees.csv";
private static final String PRODUCTS_FILE_PATH = "items.csv";
private static final String EMPLOYEES_LIST = "employees";
private static final String PRODUCT_LIST = "prodcuts";
private static final String ERR_PREFIX = "*********\nERROR\n*********\n";

public static Employee newEmployee(Integer id ,String firstName, String lastName) throws Exception { //Create a new employee
for ( Employee e : employees ) {
if (id == e.id) {
throw new IllegalArgumentException(ERR_PREFIX + "This employee ID already exist.");
}
}
Employee e = new Employee(id, firstName, lastName);
this.employees.add(e);
System.out.println("Employee "+e.createOutput()+" added to the employees list");
return e;
}

public static void deleteEmployee(Integer id) throws Exception { //Remove Employee
for(int i = 0; i<employees.size(); i++) {
if(employees.get(i).id == id) {
System.out.println("Employee "+employees.get(i).createOutput()+" was deleted sucsessfully.");
employees.remove(i);
return;
}
}
throw new IllegalArgumentException("Sorry, no such employee.");
}
}

,或者在菜单中创建Store对象并将其标记为静态并调用它

private static Store store = new Store();

....
case 1: store.newEmployee(....);
....

关于java - 从控制台菜单调用继承该方法的类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61511324/

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