用java写一个超市管理系统长一点

时间: 2023-09-23 21:04:31 浏览: 41
好的,我可以为您提供一个基本的超市管理系统的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. 结算购物车中的商品,并扣除超市余额。

相关推荐

最新推荐

recommend-type

Java水果超市管理系统.docx

水果超市管理系统共分两个主要界面,一个是进入程序后的主窗口界面。另一个是在主窗口中点击进入系统后的商品管理界面。 在商品管理界面中可以通过点击相应的功能按钮,呼出指定的对话框。输入相应的数据,来达到...
recommend-type

java控制台输出图书馆管理系统

主要为大家详细介绍了java控制台输出图书馆管理系统,只用java代码不用数据库和GUI等,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

基于java的超市管理系统.docx

本文着重论述超市管理方面的有关超市进货、销售、库存管理、人员管理等内容,紧紧围绕超市管理系统的实际运作展开具体论述。 超市管理系统主要完成的功能有:商品基本信息的录入、修改和查询;商品销售信息的录入、...
recommend-type

java代码实现银行管理系统

主要为大家详细介绍了java代码实现银行管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

基于Java+sql教师信息管理系统课设报告

基于Java+sql教师信息管理系统课设报告,教师信息管理系统主要用于实现用户名的登陆,退出,教师信息的添加,查找, 删除,修改等功能。本系统的开发需要实现以下功能模块:对系统的图形界面化进行 操作,对sql...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。