这个程序比我们在音乐商店中找到的程序要简化。我们输入我们想要的命令,它将使该命令能够完成。这些是选项。
创建“客户” |“音乐” |“销售”
列出“客户” |“音乐” |“销售”
擦除“客户端” |“音乐” |“销售”(使用或不使用我们创建新语句时生成的代码)
该程序必须执行,直到我们键入“关闭”。
在这里,我将发布到目前为止的操作。
主要问题是,如果我们键入错误的命令或键入此处不存在的内容,该如何引发异常。现在,这种情况发生了,它可以追溯到开始。如果您对我认为可能对程序更好的任何更改或修改提供建议,我也将不胜感激,我只是从这一点开始,我知道这可以通过更好的方式来完成:
public class Main {
static GestionarMusica musiclist= new GestionarMusica(20);
static GestionarCliente clientlist= new GestionarCliente(20);
static Scanner input= new Scanner(System.in);
static String instructions;
public static void main (String[] args){
do{
try{
System.out.println("Waiting for instructions: ");
instructions= input.nextLine();
switch (instructions){
case "create client":
createClient();
break;
case "create music":
createMusic();
break;
case "create selling":
//createSelling();
break;
case "list client":
listClient();
break;
case "list music":
listMusic();
break;
case "list selling":
//listSelling();
break;
}
}catch (NullPointerException npe){
System.out.println("There are not articles on the list");
}
if (instructions.equals("erase client")){
eraseClientWithoutCode();
}
if (instructions.length() <= 18 && instructions.length() >= 17 && instructions.substring(0, 16).equals("erase client")){
String client_code = instructions.substring(16);
client_code = client_code.trim();
int code = Integer.parseInt(client_code);
eraseClientWithCode(code);
}
if (instruction.equals("erase music")){
eraseMusicWithoutCode();
}
if (instructions.length() <= 17 && instructions.length() >= 16 && instructions.substring(0, 15).equals("erase music")){
String music_code = instructions.substring(15);
music_code = music_code.trim();
int code = Integer.parseInt(music_code);
eraseMusicWithCode(code);
}
if (instructions.equals("erase selling")){
eraseSellingWithoutCode();
}
if (instructions.length() <= 16 && instructions.length() >= 15 && instructions.substring(0, 14).equals("erase selling")){
String selling_code = instructions.substring(14);
selling_code = selling_code.trim();
int code = Integer.parseInt(selling_code);
eraseSellingWithCode(code);
}
}while(!instructions.equals("close"));
}
public static void createMusic() {
System.out.println("Insert album title: ");
String title = teclado.nextLine();
System.out.println("Insert album autor: ");
String autor = teclado.nextLine();
System.out.println("Insert format: ");
String format = input.nextLine();
musiclist.add(new Music(title, autor, format, musiclist.generateCode()));
}
public static void listMusic() {
System.out.println(musiclist.toString());
}
public static void eraseMusicWithCode(int code) {
musiclist.delete(code);
System.out.println("Article deleted");
}
public static void eraseMusicWithoutCode() {
System.out.println("Insert article code: ");
int code = input.nextInt();
musiclist.delete(code);
System.out.println("Article deleted");
}
UPDATE-关于在switch语句中使用default
由于这个原因,我无法使用默认值。在交换机内部,我具有创建和列出命令,但是我不得不在巫婆外部设置擦除命令,因为这些取决于我是否输入带有代码的命令,也可以不输入代码。因此,如果我输入了擦除命令,并且开关中具有默认值,它将显示异常。
如果要使用switch/case
adddefault
处理未知命令。您不需要任何例外。只需正确处理错误的输入即可。因此,您将必须准备输入并确定这些输入是否为带有三个参数的情况。将特殊情况的代码移入内部switch/case
并检查第3个参数。我做到了一种情况,这样您就可以了解想法。因此,默认情况下,有些脏的,未经测试的代码将是:
public static void main (String[] args){
do{
try{
System.out.println("Waiting for instructions: ");
instructions= input.nextLine();
String preparsedInstructions = instructions;
int from = instructions.indexOf(" ");
if(from > -1){
int to = preparsedInstructions.indexOf(" ", from + 1);
if(to > -1){
preparsedInstructions = preparsedInstructions.substring(0, to);
}
}
switch (preparsedInstructions){
case "create client":
createClient();
break;
case "create music":
createMusic();
break;
case "create selling":
//createSelling();
break;
case "list client":
listClient();
break;
case "list music":
listMusic();
break;
case "list selling":
//listSelling();
break;
case "erase client":
if (instructions.length() <= 18 && instructions.length() >= 17 && instructions.substring(0, 16).equals("erase client")){
String client_code = instructions.substring(16);
client_code = client_code.trim();
int code = Integer.parseInt(client_code);
eraseClientWithCode(code);
}else{
eraseClientWithoutCode();
}
break;
...//do for erase music,selling
default: //error handling
}
}catch (NullPointerException npe){
System.out.println("There are not articles on the list");
}
}while(!instructions.equals("close"));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句