import java.util.ArrayList;import java.util.Scanner;class Clothes { private String name; private String color; private int size; public Clothes(String name, String color, int size) { this.name = name; this.color = color; this.size = size; } public String getName() { return name; } public String getColor() { return color; } public int getSize() { return size; }}class Wardrobe { private ArrayList<Clothes> clothesList = new ArrayList<>(); public void addClothes(Clothes clothes) { clothesList.add(clothes); } public void removeClothes(int index) { clothesList.remove(index); } public void listClothes() { for (int i = 0; i < clothesList.size(); i++) { Clothes clothes = clothesList.get(i); System.out.println((i + 1) + ". " + clothes.getName() + " (" + clothes.getColor() + ", " + clothes.getSize() + ")"); } }}public class WardrobeManagementSystem { public static void main(String[] args) { Wardrobe wardrobe = new Wardrobe(); Scanner scanner = new Scanner(System.in); while (true) { System.out.println("请选择操作:"); System.out.println("1. 添加衣物"); System.out.println("2. 删除衣物"); System.out.println("3. 查看衣柜中的衣物"); System.out.println("4. 退出"); int choice = scanner.nextInt(); scanner.nextLine(); switch (choice) { case 1: System.out.println("请输入衣物名称:"); String name = scanner.nextLine(); System.out.println("请输入衣物颜色:"); String color = scanner.nextLine(); System.out.println("请输入衣物尺寸:"); int size = scanner.nextInt(); scanner.nextLine(); Clothes clothes = new Clothes(name, color, size); wardrobe.addClothes(clothes); System.out.println("衣物添加成功!"); break; case 2: System.out.println("请输入要删除的衣物编号:"); int index = scanner.nextInt() - 1; scanner.nextLine(); wardrobe.removeClothes(index); System.out.println("衣物删除成功!"); break; case 3: wardrobe.listClothes(); break; case 4: System.out.println("程序已退出!"); System.exit(0); default: System.out.println("输入有误,请重新输入!"); break; } } }}优化代码
时间: 2024-01-23 11:03:34 浏览: 24
以下是对代码的优化建议:
1. 将衣服的属性封装为一个Clothing类,使代码更加模块化和易读。
2. 在Wardrobe类中,可以使用更加简洁的foreach循环来遍历衣服列表。
3. 在程序退出时,可以使用scanner的close()方法来关闭输入流。
4. 在用户输入选项时,可以使用try-catch语句来避免程序崩溃,例如用户输入了一个非数字字符。
下面是对代码的优化后的示例:
```
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
class Clothing {
private String name;
private String color;
private int size;
public Clothing(String name, String color, int size) {
this.name = name;
this.color = color;
this.size = size;
}
public String getName() {
return name;
}
public String getColor() {
return color;
}
public int getSize() {
return size;
}
}
class Wardrobe {
private ArrayList<Clothing> clothingList = new ArrayList<>();
public void addClothing(Clothing clothing) {
clothingList.add(clothing);
}
public void removeClothing(int index) {
clothingList.remove(index);
}
public void listClothing() {
for (Clothing clothing : clothingList) {
System.out.println((clothingList.indexOf(clothing) + 1) + ". " + clothing.getName() + " (" + clothing.getColor() + ", " + clothing.getSize() + ")");
}
}
}
public class WardrobeManagementSystem {
public static void main(String[] args) {
Wardrobe wardrobe = new Wardrobe();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 添加衣物");
System.out.println("2. 删除衣物");
System.out.println("3. 查看衣柜中的衣物");
System.out.println("4. 退出");
try {
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.println("请输入衣物名称:");
String name = scanner.nextLine();
System.out.println("请输入衣物颜色:");
String color = scanner.nextLine();
System.out.println("请输入衣物尺寸:");
int size = scanner.nextInt();
scanner.nextLine();
Clothing clothing = new Clothing(name, color, size);
wardrobe.addClothing(clothing);
System.out.println("衣物添加成功!");
break;
case 2:
System.out.println("请输入要删除的衣物编号:");
int index = scanner.nextInt() - 1;
scanner.nextLine();
wardrobe.removeClothing(index);
System.out.println("衣物删除成功!");
break;
case 3:
wardrobe.listClothing();
break;
case 4:
System.out.println("程序已退出!");
scanner.close();
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
break;
}
} catch (InputMismatchException e) {
System.out.println("输入有误,请重新输入!");
scanner.nextLine();
}
}
}
}
```