创建控制台命令列表 - 如何使用 Java 正确执行此操作
对于我用 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());
}
实际上,我有什么要记住的是在 switch 中使用枚举及其序数值,但 Java 不允许在 switch 情况下使用非常量值。解决这个问题的正确方法是什么?最优雅/可扩展/灵活?任何建设性意见都将受到高度赞赏!
For a homework I code in Java(with which I'm a newbie), I bump into the problem of creating a console command list. The user will be confronted with a set of commands, among which s/he will choose his/her choice with a number. Something like this:
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
The first way I have done it is as follows(unnecessary code parts are truncated):
...
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;
}
}
Then, thinking that this is ugly, and for the sake of experimenting with Enums, I have tried the following:
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());
}
Actually, what I have in mind is using Enum's in switch with their ordinal values, but Java does not allow non-constant values in switch cases. What is the right way to solve this problem; the most elegant/scalable/flexible? Any constructive comments are highly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
Command.values()
,它包含按顺序排列的枚举:一种更优雅且可维护的方法是使用多态性消除 switch 语句:
以及其他地方:
优点:
You could use
Command.values()
, which contains the enums in ordinal order:A more elegant and maintainable way is to elimate the switch statement using polymorphism:
and elsewhere:
Advantages:
使用 Jakarta Commons CLI 会作弊吗? ;-)
Would using Jakarta Commons CLI be cheating? ;-)
嗯,这是一种奇怪的方法,但如果您只想查看枚举的使用,那么不建议使用“ordinal()”。
最好创建一个有 2 个成员的枚举。不理想,但它可能会有所帮助..
现在您可以使用静态
Command.get(int)
获取枚举Well, it's a curious approach, but if you just want to look at the use of Enums, then "ordinal()" is not recommended.
Better to make an Enum with 2 members. Not ideal, but it might help ..
Now you can get an enum using the static
Command.get(int)
使用序数作为标识符被认为是相当糟糕的形式,但也许您可以这样使用它?
在你的命令处理类中:
但是如果你不想走枚举路线,那么没有什么能真正阻止你创建非枚举命令对象。这样做的好处是它们可以实现或可以配置为调用实际命令的实现:
Using ordinals as identifiers is considered rather poor form, but perhaps you could use it like this?
and in your command handling class:
But if you don't want to go the enum route there's nothing really stopping you from creating non-enum command objects. The upside of that is that they can implement, or can be configured to call the implementation of, the actual command: