私がJavaでコードを書いている(私は初心者です)宿題のために、コンソールコマンドリストを作成するという問題にぶつかりました。ユーザーは一連のコマンドに直面し、その中から自分の選択肢を番号で選択します。このようなもの:
Enter your choice:
0) Create a Table
1) List All Tables
2) Delete a Table
3) Insert a Record
4) List All Records
5) Delete a Record
6) Find a Record(by =)
7) Find a Record(by >)
8) Find a Record(by <)
9) Exit
私が行った最初の方法は次のとおりです(不要なコード部分は切り捨てられます):
...
outerLoop: while (true) {
Scanner s = new Scanner(System.in);
try {
while (true) {
System.out.println("Enter your choice:");
displayChoiceList();
int choice = s.nextInt();
switch (choice) {
case 1:
processTableCreation();
break;
case 2:
catalog.listAllTables();
break;
case 3:
System.out.println("Enter the name of table:");
String tableName = s.nextLine();
catalog.deleteTable(tableName);
break;
case 4:
processRecordInsertion();
break;
case 5:
processListAllRecords();
break;
case 6:
processDeleteRecord();
break;
case 7:
processFindRecord(Constants.Operator.EQUAL);
break;
case 8:
processFindRecord(Constants.Operator.SMALLER);
break;
case 9:
processFindRecord(Constants.Operator.GREATER);
break;
case 10:
break outerLoop;
}
}
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
return;
}
}
...
private static void displayChoiceList() {
String[] choices = new String[] { "Create Table", "List All Tables",
"Delete a Table", "Insert a Record to a Table",
"List all records", "Delete a record",
"Find by Primary Key(=)", "Find by Primary Key(<)",
"Find by Primary Key(>)", "Exit" };
int id = 0;
for (String choice : choices) {
System.out.println((id + 1) + ") " + choice);
++id;
}
}
次に、これは醜いと考え、列挙型を試すために、次のことを試しました。
private enum Command {
CREATE_TABLE("Create a Table"),
LIST_ALL_TABLES("List All Tables"),
DELETE_TABLE("Delete a Table"),
INSERT_RECORD("Insert a Record"),
LIST_ALL_RECORDS("List All Records"),
DELETE_RECORD("Delete a Record"),
FIND_RECORD_EQ("Find a Record(by =)"),
FIND_RECORD_GT("Find a Record(by >)"),
FIND_RECORD_LT("Find a Record(by <)"),
EXIT("Exit");
private final String message;
Command(String message) {
this.message = message;
}
public String message() { return this.message; }
}
...
outerLoop: while (true) {
Scanner s = new Scanner(System.in);
try {
while (true) {
System.out.println("Enter your choice:");
displayChoiceList();
int choice = s.nextInt();
if (choice == Command.CREATE_TABLE.ordinal())
processTableCreation();
else if (choice == Command.LIST_ALL_TABLES.ordinal())
catalog.listAllTables();
else if (choice == Command.DELETE_TABLE.ordinal()) {
System.out.println("Enter the name of table:");
String tableName = s.nextLine();
catalog.deleteTable(tableName);
}
else if (choice == Command.INSERT_RECORD.ordinal())
processRecordInsertion();
else if (choice == Command.LIST_ALL_RECORDS.ordinal())
processListAllRecords();
else if (choice == Command.DELETE_RECORD.ordinal())
processDeleteRecord();
else if (choice == Command.FIND_RECORD_EQ.ordinal())
processFindRecord(Constants.Operator.EQUAL);
else if (choice == Command.FIND_RECORD_LT.ordinal())
processFindRecord(Constants.Operator.SMALLER);
else if (choice == Command.FIND_RECORD_GT.ordinal())
processFindRecord(Constants.Operator.GREATER);
else if (choice == Command.EXIT.ordinal())
break outerLoop;
else
System.out.println("Invalid command number entered!");
}
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
return;
}
}
...
private static void displayChoiceList() {
for (Command c : Command.values())
System.out.println(c.ordinal() + ") " + c.message());
}
実際、私が念頭に置いているのは、Enum のスイッチで序数値を使用することですが、Java では、スイッチの場合に非定数値を使用できません。この問題を解決する正しい方法は何ですか。最もエレガント/スケーラブル/フレキシブル? 建設的なコメントは大歓迎です!