Java代码简单的面向对象之衣柜管理系统
时间: 2023-08-28 18:03:31 浏览: 58
好的,下面是一个简单的衣柜管理系统的Java代码,采用面向对象的方式实现:
```java
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;
}
}
}
}
```
这个衣柜管理系统包含了衣物类 Clothes 和衣柜类 Wardrobe ,其中 Clothes 类包含了衣物的名称、颜色和尺寸属性,并提供了获取这些属性的方法。Wardrobe 类包含了一个衣物列表,提供了添加、删除和列出衣物的方法。最后,WardrobeManagementSystem 类包含了主函数,通过用户输入实现了对衣柜的管理。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)