使用Java写衣服管理系统代码
时间: 2024-02-22 13:58:40 浏览: 50
以下是一个简单的 Java 代码示例,用于实现一个基本的衣服管理系统。该系统可以添加、查看、修改和删除衣服信息,并将所有信息存储在内存中。
```java
import java.util.ArrayList;
import java.util.Scanner;
public class ClothesManagementSystem {
private ArrayList<Clothes> clothesList = new ArrayList<>();
private Scanner scanner = new Scanner(System.in);
public void start() {
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 添加衣服");
System.out.println("2. 查看衣服");
System.out.println("3. 修改衣服");
System.out.println("4. 删除衣服");
System.out.println("5. 退出系统");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addClothes();
break;
case 2:
viewClothes();
break;
case 3:
modifyClothes();
break;
case 4:
deleteClothes();
break;
case 5:
System.out.println("谢谢使用!");
System.exit(0);
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
private void addClothes() {
System.out.println("请输入衣服编号:");
int id = scanner.nextInt();
System.out.println("请输入衣服名称:");
String name = scanner.next();
System.out.println("请输入衣服价格:");
double price = scanner.nextDouble();
Clothes clothes = new Clothes(id, name, price);
clothesList.add(clothes);
System.out.println("添加成功!");
}
private void viewClothes() {
if (clothesList.size() == 0) {
System.out.println("暂无衣服信息!");
return;
}
System.out.println("编号\t名称\t价格");
for (Clothes clothes : clothesList) {
System.out.println(clothes.getId() + "\t" + clothes.getName() + "\t" + clothes.getPrice());
}
}
private void modifyClothes() {
System.out.println("请输入要修改的衣服编号:");
int id = scanner.nextInt();
for (Clothes clothes : clothesList) {
if (clothes.getId() == id) {
System.out.println("请输入新的衣服名称:");
String name = scanner.next();
System.out.println("请输入新的衣服价格:");
double price = scanner.nextDouble();
clothes.setName(name);
clothes.setPrice(price);
System.out.println("修改成功!");
return;
}
}
System.out.println("未找到该衣服!");
}
private void deleteClothes() {
System.out.println("请输入要删除的衣服编号:");
int id = scanner.nextInt();
for (int i = 0; i < clothesList.size(); i++) {
Clothes clothes = clothesList.get(i);
if (clothes.getId() == id) {
clothesList.remove(i);
System.out.println("删除成功!");
return;
}
}
System.out.println("未找到该衣服!");
}
public static void main(String[] args) {
ClothesManagementSystem cms = new ClothesManagementSystem();
cms.start();
}
}
class Clothes {
private int id;
private String name;
private double price;
public Clothes(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
```
这是一个基本的命令行程序,可以让用户通过菜单选择要执行的操作。在程序中,我们使用 `ArrayList` 来存储衣服信息,并提供了添加、查看、修改和删除衣服信息的功能。程序还包括一个 `Clothes` 类,用于创建衣服对象。
阅读全文