How do I create a switch-statement in my main class that when that case is selected will 运行 another main class in my program?
How do I create a switch-statement in my main class that when that case is selected will run another main class in my program?
我有多个 class,例如客户、客户、员工、员工、产品、产品。带有 's' 的程序是主要的 classes。我有一个 Main class,我希望它成为程序的起点。我想要一个 switch 语句。我想请用户输入 1-3,然后从他们的输入中 运行 那个主要的 class。例如,如果输入为 1,它将 运行 客户为主,等等。我该怎么做?
导入java.util.Scanner;
public class Main {
public static void main(String[] args) throws InputValidationException {
//run customer add/edit/remove
int choice = 0;
while (true) {
displayMenu();
switch (choice) {
case 1:
customers.main(null);
break;
case 2:
cars.main(null);
break;
case 3:
Staff.main(null);
break;
}
}
}
private static void displayMenu() {
Scanner input;
{
input = new Scanner(System.in);
{
System.out.println("1. Customers");
System.out.println("2. Cars");
System.out.println("3. Staff");
System.out.println("Which would you like to add/edit: ");
String choice = input.nextLine();
}
}
}
}
My other classes are fairly similar to this:
import java.util.Scanner;
import java.util.concurrent.CopyOnWriteArrayList;
//creates and array of the customers
public final class customers {
public static void main(String[] args) throws InputValidationException {
//add new customer
CopyOnWriteArrayList<customer> customers = new CopyOnWriteArrayList<>();
//List will fail in case of remove due to ConcurrentModificationException
//loop getting input
//input 'q' to quit
Scanner input;
{
//scanner to get the input
input = new Scanner(System.in);
{
while (true) {
//ask user for input and get input
System.out.println("Enter id (press 'q' to quit): ");
String temp = input.nextLine();
if (temp.equals("q")) break;
int id = Integer.parseInt(temp);
System.out.println("Enter first name:");
String firstName = input.nextLine();
System.out.println("Enter last name:");
String lastName = input.nextLine();
//add to array list
customers.add(new customer(id, firstName, lastName));
}
}
}
//Display All
System.out.println("Current List: ");
for (customer customer : customers) {
System.out.println(customer.toString());
}
// search
System.out.println("Enter name to search and display");
String searchString = input.nextLine();
for (customer customer : customers) {
if (customer.search(searchString) != null) {
System.out.println(customer.toString());
}
}
//Remove
System.out.println("Enter name to search & remove");
searchString = input.nextLine();
for (customer customer : customers) {
if (customer.search(searchString) != null) {
System.out.println(customer.toString() + " is removed from the List");
customers.remove(customer);
}
}
}
}
客户class只包含变量、setter、getter和构造函数
import java.util.Scanner;
public class MainClasss {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String option = scanner.next();
switch (option) {
case "1" :
AnotherMainClass.main(args);
break;
case "2" :
AnotherAnotherMainClass.main(args);
break;
default:
System.out.println("not found!");
}
}
}
class AnotherMainClass {
public static void main(String[] args) {
System.out.println("Hello from AnotherMainClass.main!");
}
}
class AnotherAnotherMainClass {
public static void main(String[] args) {
System.out.println("Hello from AnotherAnotherMainClass.main!");
}
}
输入:
1
输出:
Hello from AnotherMainClass.main!
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String input;
do {
input = displayMenu();
switch (input) {
case "1":
customers.main(args);
break;
case "2":
cars.main(args);
break;
case "3":
Staff.main(args);
break;
}
} while (!"exit".equals(input));
}
private static String displayMenu() {
Scanner input = new Scanner(System.in);
System.out.println("1. Customers");
System.out.println("2. Cars");
System.out.println("3. Staff");
System.out.println("Which would you like to add/edit: ");
return input.nextLine();
}
}
class customers {
public static void main(String[] args) {
System.out.println("hello from 'customers'!");
}
}
class cars {
public static void main(String[] args) {
System.out.println("hello from 'cars'");
}
}
class Staff {
public static void main(String[] args) {
System.out.println("hello from 'Staff'");
}
}
我有多个 class,例如客户、客户、员工、员工、产品、产品。带有 's' 的程序是主要的 classes。我有一个 Main class,我希望它成为程序的起点。我想要一个 switch 语句。我想请用户输入 1-3,然后从他们的输入中 运行 那个主要的 class。例如,如果输入为 1,它将 运行 客户为主,等等。我该怎么做?
导入java.util.Scanner;
public class Main {
public static void main(String[] args) throws InputValidationException {
//run customer add/edit/remove
int choice = 0;
while (true) {
displayMenu();
switch (choice) {
case 1:
customers.main(null);
break;
case 2:
cars.main(null);
break;
case 3:
Staff.main(null);
break;
}
}
}
private static void displayMenu() {
Scanner input;
{
input = new Scanner(System.in);
{
System.out.println("1. Customers");
System.out.println("2. Cars");
System.out.println("3. Staff");
System.out.println("Which would you like to add/edit: ");
String choice = input.nextLine();
}
}
}
}
My other classes are fairly similar to this:
import java.util.Scanner;
import java.util.concurrent.CopyOnWriteArrayList;
//creates and array of the customers
public final class customers {
public static void main(String[] args) throws InputValidationException {
//add new customer
CopyOnWriteArrayList<customer> customers = new CopyOnWriteArrayList<>();
//List will fail in case of remove due to ConcurrentModificationException
//loop getting input
//input 'q' to quit
Scanner input;
{
//scanner to get the input
input = new Scanner(System.in);
{
while (true) {
//ask user for input and get input
System.out.println("Enter id (press 'q' to quit): ");
String temp = input.nextLine();
if (temp.equals("q")) break;
int id = Integer.parseInt(temp);
System.out.println("Enter first name:");
String firstName = input.nextLine();
System.out.println("Enter last name:");
String lastName = input.nextLine();
//add to array list
customers.add(new customer(id, firstName, lastName));
}
}
}
//Display All
System.out.println("Current List: ");
for (customer customer : customers) {
System.out.println(customer.toString());
}
// search
System.out.println("Enter name to search and display");
String searchString = input.nextLine();
for (customer customer : customers) {
if (customer.search(searchString) != null) {
System.out.println(customer.toString());
}
}
//Remove
System.out.println("Enter name to search & remove");
searchString = input.nextLine();
for (customer customer : customers) {
if (customer.search(searchString) != null) {
System.out.println(customer.toString() + " is removed from the List");
customers.remove(customer);
}
}
}
}
客户class只包含变量、setter、getter和构造函数
import java.util.Scanner;
public class MainClasss {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String option = scanner.next();
switch (option) {
case "1" :
AnotherMainClass.main(args);
break;
case "2" :
AnotherAnotherMainClass.main(args);
break;
default:
System.out.println("not found!");
}
}
}
class AnotherMainClass {
public static void main(String[] args) {
System.out.println("Hello from AnotherMainClass.main!");
}
}
class AnotherAnotherMainClass {
public static void main(String[] args) {
System.out.println("Hello from AnotherAnotherMainClass.main!");
}
}
输入:
1
输出:
Hello from AnotherMainClass.main!
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String input;
do {
input = displayMenu();
switch (input) {
case "1":
customers.main(args);
break;
case "2":
cars.main(args);
break;
case "3":
Staff.main(args);
break;
}
} while (!"exit".equals(input));
}
private static String displayMenu() {
Scanner input = new Scanner(System.in);
System.out.println("1. Customers");
System.out.println("2. Cars");
System.out.println("3. Staff");
System.out.println("Which would you like to add/edit: ");
return input.nextLine();
}
}
class customers {
public static void main(String[] args) {
System.out.println("hello from 'customers'!");
}
}
class cars {
public static void main(String[] args) {
System.out.println("hello from 'cars'");
}
}
class Staff {
public static void main(String[] args) {
System.out.println("hello from 'Staff'");
}
}