用java编写。1建个商品类,属性有商品编号、商品名称、商品价格、商品数量 2将商品放入集合中 3执行程序时,有3个功能让用户选择,添加商品、删除商品、显示已有商品。删除商品时需判断商品是否存在(可通过商品编号或者商品名称判断),这三个功能均写成方法。用户选择退出时才能退出。
时间: 2024-06-08 11:10:53 浏览: 64
以下是Java代码实现:
```java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class GoodsManagement {
private static List<Goods> goodsList = new ArrayList<>(); // 商品集合
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice = 0;
while (choice != 4) {
System.out.println("1.添加商品");
System.out.println("2.删除商品");
System.out.println("3.显示已有商品");
System.out.println("4.退出");
System.out.print("请输入您的选择:");
choice = sc.nextInt();
switch (choice) {
case 1:
addGoods();
break;
case 2:
deleteGoods();
break;
case 3:
showGoods();
break;
case 4:
System.out.println("谢谢使用!");
break;
default:
System.out.println("输入错误,请重新输入!");
}
}
}
// 添加商品
public static void addGoods() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入商品编号:");
int id = sc.nextInt();
System.out.print("请输入商品名称:");
String name = sc.next();
System.out.print("请输入商品价格:");
double price = sc.nextDouble();
System.out.print("请输入商品数量:");
int quantity = sc.nextInt();
Goods goods = new Goods(id, name, price, quantity);
goodsList.add(goods);
System.out.println("商品添加成功!");
}
// 删除商品
public static void deleteGoods() {
Scanner sc = new Scanner(System.in);
System.out.println("请选择删除方式:");
System.out.println("1.按商品编号删除");
System.out.println("2.按商品名称删除");
System.out.print("请输入您的选择:");
int choice = sc.nextInt();
if (choice == 1) {
System.out.print("请输入商品编号:");
int id = sc.nextInt();
boolean flag = false;
Iterator<Goods> it = goodsList.iterator();
while (it.hasNext()) {
Goods goods = it.next();
if (goods.getId() == id) {
it.remove();
flag = true;
System.out.println("商品删除成功!");
break;
}
}
if (!flag) {
System.out.println("商品不存在!");
}
} else if (choice == 2) {
System.out.print("请输入商品名称:");
String name = sc.next();
boolean flag = false;
Iterator<Goods> it = goodsList.iterator();
while (it.hasNext()) {
Goods goods = it.next();
if (goods.getName().equals(name)) {
it.remove();
flag = true;
System.out.println("商品删除成功!");
break;
}
}
if (!flag) {
System.out.println("商品不存在!");
}
} else {
System.out.println("输入错误,请重新选择!");
}
}
// 显示商品
public static void showGoods() {
System.out.println("商品编号\t商品名称\t商品价格\t商品数量");
for (Goods goods : goodsList) {
System.out.println(goods.getId() + "\t\t" + goods.getName() + "\t\t" + goods.getPrice() + "\t\t" + goods.getQuantity());
}
}
}
class Goods {
private int id; // 商品编号
private String name; // 商品名称
private double price; // 商品价格
private int quantity; // 商品数量
public Goods(int id, String name, double price, int quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
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;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
```
使用时,直接运行 `GoodsManagement` 类即可。
阅读全文