创建商品类(Product),属性:商品编号,商品名称,商品单价,商品库存数量,在控制台可以实现商品信息的添加,删除,修改,查询。 在main方法中我们需要通过一个循环体来重复显示菜单栏,通过键盘输入来执行操作do{}while循环可以有效的实现这一系列操作,并且通过控制台输入来控制程序的执行及终止。
时间: 2023-12-06 11:02:31 浏览: 55
好的,我可以帮你实现这个功能。下面是代码实现:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class Product {
private String productId;
private String productName;
private double unitPrice;
private int stockQuantity;
public Product(String productId, String productName, double unitPrice, int stockQuantity) {
this.productId = productId;
this.productName = productName;
this.unitPrice = unitPrice;
this.stockQuantity = stockQuantity;
}
public String getProductId() {
return productId;
}
public void setProductId(String productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public double getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
public int getStockQuantity() {
return stockQuantity;
}
public void setStockQuantity(int stockQuantity) {
this.stockQuantity = stockQuantity;
}
public static void main(String[] args) {
ArrayList<Product> productList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
do {
// 显示菜单栏
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:
// 添加商品
System.out.println("请输入商品编号:");
String productId = scanner.next();
System.out.println("请输入商品名称:");
String productName = scanner.next();
System.out.println("请输入商品单价:");
double unitPrice = scanner.nextDouble();
System.out.println("请输入商品库存数量:");
int stockQuantity = scanner.nextInt();
Product product = new Product(productId, productName, unitPrice, stockQuantity);
productList.add(product);
System.out.println("添加成功!");
break;
case 2:
// 删除商品
System.out.println("请输入要删除的商品编号:");
String deleteProductId = scanner.next();
boolean found = false;
for (int i = 0; i < productList.size(); i++) {
Product p = productList.get(i);
if (p.getProductId().equals(deleteProductId)) {
productList.remove(i);
found = true;
break;
}
}
if (found) {
System.out.println("删除成功!");
} else {
System.out.println("没有找到该商品!");
}
break;
case 3:
// 修改商品库存数量
System.out.println("请输入要修改的商品编号:");
String modifyProductId = scanner.next();
System.out.println("请输入新的商品库存数量:");
int newStockQuantity = scanner.nextInt();
found = false;
for (int i = 0; i < productList.size(); i++) {
Product p = productList.get(i);
if (p.getProductId().equals(modifyProductId)) {
p.setStockQuantity(newStockQuantity);
found = true;
break;
}
}
if (found) {
System.out.println("修改成功!");
} else {
System.out.println("没有找到该商品!");
}
break;
case 4:
// 查询商品信息
System.out.println("请输入要查询的商品编号:");
String queryProductId = scanner.next();
found = false;
for (Product p : productList) {
if (p.getProductId().equals(queryProductId)) {
System.out.println("商品编号:" + p.getProductId());
System.out.println("商品名称:" + p.getProductName());
System.out.println("商品单价:" + p.getUnitPrice());
System.out.println("商品库存数量:" + p.getStockQuantity());
found = true;
break;
}
}
if (!found) {
System.out.println("没有找到该商品!");
}
break;
case 5:
// 退出
System.exit(0);
default:
System.out.println("输入错误,请重新输入!");
break;
}
} while (true);
}
}
```
上面的代码实现了一个简单的商品类,包含商品编号、商品名称、商品单价、商品库存数量四个属性,以及添加、删除、修改、查询四种操作。在 main 方法中通过一个 do-while 循环体来重复显示菜单栏,通过键盘输入来执行相应的操作,并且通过控制台输入来控制程序的执行及终止。
阅读全文