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-24 09:03:36 浏览: 24
自学java API(一).md
你可以尝试增加以下代码:
1. 在 Clothes 类中增加 getDetails() 方法,用于返回衣物的详细信息。
```
public String getDetails() {
return name + " (" + color + ", " + size + ")";
}
```
2. 在 Wardrobe 类中增加 findClothes() 方法,用于查找指定名称的衣物,并返回其在衣柜中的位置。
```
public int findClothes(String name) {
for (int i = 0; i < clothesList.size(); i++) {
Clothes clothes = clothesList.get(i);
if (clothes.getName().equals(name)) {
return i;
}
}
return -1;
}
```
3. 在 WardrobeManagementSystem 类中增加 updateClothes() 方法,用于更新指定名称的衣物的颜色和尺寸。
```
public void updateClothes(Wardrobe wardrobe, String name) {
int index = wardrobe.findClothes(name);
if (index == -1) {
System.out.println("衣柜中不存在该衣物!");
return;
}
System.out.println("请输入衣物新颜色:");
String color = scanner.nextLine();
System.out.println("请输入衣物新尺寸:");
int size = scanner.nextInt();
scanner.nextLine();
Clothes clothes = wardrobe.clothesList.get(index);
clothes.color = color;
clothes.size = size;
System.out.println("衣物更新成功!");
}
```
4. 在 WardrobeManagementSystem 类的 switch 语句中增加一个 case,用于更新衣物。
```
case 5:
System.out.println("请输入要更新的衣物名称:");
String name = scanner.nextLine();
updateClothes(wardrobe, name);
break;
```
这样,用户就可以选择更新衣物的颜色和尺寸了。
阅读全文