用java写一个超市管理系统长一点
时间: 2023-09-23 07:04:31 浏览: 68
好的,我可以为您提供一个基本的超市管理系统的Java代码框架,您可以在此基础上进行修改和完善。代码如下:
```
import java.util.ArrayList;
import java.util.Scanner;
public class SupermarketManagementSystem {
// 商品类
class Commodity {
private String name; // 商品名称
private double price; // 商品价格
private int inventory; // 商品库存
public Commodity(String name, double price, int inventory) {
this.name = name;
this.price = price;
this.inventory = inventory;
}
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 getInventory() {
return inventory;
}
public void setInventory(int inventory) {
this.inventory = inventory;
}
}
// 购物车类
class ShoppingCart {
private ArrayList<Commodity> goods = new ArrayList<>(); // 商品列表
public void add(Commodity commodity) {
goods.add(commodity);
}
public void remove(Commodity commodity) {
goods.remove(commodity);
}
public void clear() {
goods.clear();
}
public ArrayList<Commodity> getGoods() {
return goods;
}
}
// 超市类
class Supermarket {
private ArrayList<Commodity> goods = new ArrayList<>(); // 商品列表
private double balance; // 超市余额
public Supermarket(double balance) {
this.balance = balance;
}
public void add(Commodity commodity) {
goods.add(commodity);
}
public void remove(Commodity commodity) {
goods.remove(commodity);
}
public ArrayList<Commodity> getGoods() {
return goods;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
SupermarketManagementSystem system = new SupermarketManagementSystem();
Supermarket supermarket = new Supermarket(100000); // 初始化超市,余额为100000元
ShoppingCart cart = new ShoppingCart(); // 初始化购物车
// 添加商品到超市
supermarket.add(system.new Commodity("可乐", 3.5, 100));
supermarket.add(system.new Commodity("雪碧", 3.5, 100));
supermarket.add(system.new Commodity("薯片", 5, 50));
supermarket.add(system.new Commodity("巧克力", 10, 20));
// ...
while (true) {
// 显示商品列表
System.out.println("商品列表:");
ArrayList<Commodity> goods = supermarket.getGoods();
for (int i = 0; i < goods.size(); i++) {
Commodity commodity = goods.get(i);
System.out.println((i + 1) + ". " + commodity.getName() + ",单价:" + commodity.getPrice() + "元,库存:" + commodity.getInventory() + "件");
}
System.out.println();
// 显示购物车
System.out.println("购物车:");
ArrayList<Commodity> cartGoods = cart.getGoods();
for (int i = 0; i < cartGoods.size(); i++) {
Commodity commodity = cartGoods.get(i);
System.out.println((i + 1) + ". " + commodity.getName() + ",单价:" + commodity.getPrice() + "元,数量:" + 1);
}
System.out.println();
// 显示余额
System.out.println("余额:" + supermarket.getBalance() + "元");
System.out.println();
// 购物操作
System.out.println("请选择操作:");
System.out.println("1. 添加商品到购物车");
System.out.println("2. 从购物车中删除商品");
System.out.println("3. 结算");
String choice = scanner.nextLine();
System.out.println();
switch (choice) {
case "1":
System.out.println("请输入商品编号:");
int index = Integer.parseInt(scanner.nextLine()) - 1;
Commodity commodity = goods.get(index);
if (commodity.getInventory() > 0) { // 判断库存是否充足
cart.add(commodity); // 添加商品到购物车
commodity.setInventory(commodity.getInventory() - 1); // 减少商品库存
System.out.println("已添加商品:" + commodity.getName());
} else {
System.out.println("库存不足!");
}
break;
case "2":
System.out.println("请输入商品编号:");
index = Integer.parseInt(scanner.nextLine()) - 1;
commodity = cartGoods.get(index);
cart.remove(commodity); // 从购物车中删除商品
commodity.setInventory(commodity.getInventory() + 1); // 增加商品库存
System.out.println("已删除商品:" + commodity.getName());
break;
case "3":
double totalPrice = 0;
cartGoods = cart.getGoods();
for (int i = 0; i < cartGoods.size(); i++) {
commodity = cartGoods.get(i);
totalPrice += commodity.getPrice();
}
if (totalPrice > supermarket.getBalance()) { // 判断余额是否充足
System.out.println("余额不足!");
} else {
// 扣除余额
supermarket.setBalance(supermarket.getBalance() - totalPrice);
// 清空购物车
cart.clear();
System.out.println("结算成功!");
}
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
System.out.println();
}
}
}
```
该代码实现了以下基本功能:
1. 显示超市商品列表。
2. 添加商品到购物车。
3. 从购物车中删除商品。
4. 结算购物车中的商品,并扣除超市余额。
阅读全文