下面是一个简单的超市管理系统,只实现了简单的增,删,改,查功能
创建一个主类
import java.util.Scanner;
import java.util.ArrayList;
public class Supermarket {
public static ArrayList<Product> productList = new ArrayList<>();//创建一个产品动态数组
public static void main(String[] args) {
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 添加商品" + "\t" + "2. 修改商品" + "\t" + "3. 删除商品" + "\t" + "4. 查询商品");
System.out.println("请输入选项:");
Scanner scanner = new Scanner(System.in);
String choice = scanner.nextLine();
switch (choice) {
case "1" -> addProduct.add();
case "2" -> changeProduct.change();
case "3" -> deleteProduct.delete();
case "4" -> searchProduct.search();
default -> System.out.println("无效的选项,请重新输入!");
}
}
}
}
创建一个Product类
public class Product {
private String id;//编号
private String name;//名字
private double price;//价格
public Product(){
}
public Product(String id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
public String getId() {
return id;
}
public void setId(String 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 String InforShow() {
return "商品编号:" + id + " 商品名称:" + name + " 商品单价:" + price + "元。";
}
}
创建一个addProduct类,实现增加商品
import java.util.Scanner;
public class addProduct {
//增加商品
public static void add() {
Scanner scanner =new Scanner(System.in);
System.out.println("请输入商品编号:");
String id = scanner.next();
System.out.println("请输入商品名称:");
String name = scanner.next();
System.out.println("请输入商品单价:");
double price = scanner.nextDouble();
Product product = new Product(id, name, price);
Supermarket.productList.add(product);
System.out.println("商品添加成功!");
}
}
创建一个deleteProduct类,实现删除商品
import java.util.Scanner;
public class deleteProduct {
//删除商品
public static void delete() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要删除的商品编号:");
String id = scanner.nextLine();
boolean found = false;
for (Product product : Supermarket.productList) {
if (product.getId().equals(id)) {
Supermarket.productList.remove(product);
found = true;
System.out.println("商品删除成功!");
break;
}
}
if (!found) {
System.out.println("未找到该商品!");
}
}
}
创建一个changProduct类,实现更改商品
public class changeProduct {
//更改商品信息
public static void change() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的商品编号:");
String id = scanner.nextLine();
boolean found = false;
for (Product product : Supermarket.productList) {
if (product.getId().equals(id)) {
System.out.println("请输入新的商品名称:");
String name = scanner.nextLine();
System.out.println("请输入新的商品单价:");
double price = scanner.nextDouble();
product.setName(name);
product.setPrice(price);
found = true;
System.out.println("商品修改成功!");
break;
}
}
if (!found) {
System.out.println("未找到该商品!");
}
}
}
创建一个searchProduct类,实现查找商品
import java.util.Scanner;
//查询商品信息
public class searchProduct {
public static void search() {
for (Product product:Supermarket.productList) {
System.out.println(product.InforShow());
}
}
}