面向对象之衣服管理系统代码
时间: 2023-08-06 17:22:13 浏览: 50
下面是一个简单的面向对象衣服管理系统的示例代码:
```
// 衣服类
public class Cloth {
private String name;
private String style;
private String color;
private String size;
public Cloth(String name, String style, String color, String size) {
this.name = name;
this.style = style;
this.color = color;
this.size = size;
}
public String getName() {
return name;
}
public String getStyle() {
return style;
}
public String getColor() {
return color;
}
public String getSize() {
return size;
}
@Override
public String toString() {
return "Cloth{" +
"name='" + name + '\'' +
", style='" + style + '\'' +
", color='" + color + '\'' +
", size='" + size + '\'' +
'}';
}
}
// 衣柜类
public class Closet {
private List<Cloth> clothes;
public Closet() {
clothes = new ArrayList<>();
}
public void addCloth(Cloth cloth) {
clothes.add(cloth);
System.out.println("成功添加衣服:" + cloth.getName());
}
public void removeCloth(String name) {
for (int i = 0; i < clothes.size(); i++) {
if (clothes.get(i).getName().equals(name)) {
clothes.remove(i);
System.out.println("成功移除衣服:" + name);
return;
}
}
System.out.println("未找到衣服:" + name);
}
public void listClothes() {
if (clothes.size() == 0) {
System.out.println("衣柜为空");
} else {
System.out.println("衣柜中的衣服:");
for (Cloth cloth : clothes) {
System.out.println(cloth);
}
}
}
public void findCloth(String name) {
for (Cloth cloth : clothes) {
if (cloth.getName().equals(name)) {
System.out.println("找到衣服:" + cloth);
return;
}
}
System.out.println("未找到衣服:" + name);
}
}
// 主程序
public class Main {
public static void main(String[] args) {
Closet closet = new Closet();
Cloth cloth1 = new Cloth("T恤", "休闲", "白色", "L");
Cloth cloth2 = new Cloth("衬衫", "正装", "蓝色", "M");
Cloth cloth3 = new Cloth("牛仔裤", "休闲", "黑色", "28");
closet.addCloth(cloth1);
closet.addCloth(cloth2);
closet.addCloth(cloth3);
closet.listClothes();
closet.removeCloth("T恤");
closet.findCloth("衬衫");
closet.findCloth("短裤");
}
}
```
在这个示例中,我们定义了 `Cloth` 和 `Closet` 两个类,分别表示衣服和衣柜。`Cloth` 类包含了衣服的属性和构造方法,`Closet` 类包含了衣柜中衣服的列表和对这些衣服进行管理的方法。在 `Main` 类中,我们实例化了 `Closet` 对象,并添加了三件衣服到衣柜中,然后调用了一些方法对衣柜中的衣服进行操作。
需要注意的是,这个示例只是一个简单的实现,实际的衣服管理系统可能需要更多的属性和方法来满足实际需求。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)