答案:系统实现商品管理、购物车操作与结算功能,包含Product、CartItem和SupermarketSystem类,支持浏览商品、添加至购物车、查看 cart、会员折扣结算及打印小票,通过控制台交互完成购物流程。

用Java实现一个控制台交互式超市结账系统,核心目标是模拟真实购物场景下的商品管理、顾客选购、结算和打印小票功能。整个项目结构清晰,适合初学者掌握面向对象编程、集合操作和控制台输入输出处理。
系统应具备以下基本功能:
采用面向对象方式设计以下几个类:
① 商品类(Product)
立即学习“Java免费学习笔记(深入)”;
封装商品的基本属性和行为。
```java public class Product { private String id; private String name; private double price; private int stock;public Product(String id, String name, double price, int stock) {
this.id = id;
this.name = name;
this.price = price;
this.stock = stock;
}
// Getter 和 Setter 方法
public String getId() { return id; }
public String getName() { return name; }
public double getPrice() { return price; }
public int getStock() { return stock; }
public void setStock(int stock) { this.stock = stock; }
@Override
public String toString() {
return "ID:" + id + " | 名称:" + name + " | 价格:" + price + "元 | 库存:" + stock;
}}
<font color="#0066cc">② 购物车条目类(CartItem)</font><br>
<p>记录每种商品的购买数量和对应商品信息。</p>
```java
public class CartItem {
private Product product;
private int quantity;
public CartItem(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}
public Product getProduct() { return product; }
public int getQuantity() { return quantity; }
public double getTotalPrice() { return product.getPrice() * quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }
}③ 超市管理系统主类(SupermarketSystem)
包含商品列表、购物车、控制台交互逻辑。
```java import java.util.*;public class SupermarketSystem {
private List
public SupermarketSystem() {
products = new ArrayList<>();
cart = new ArrayList<>();
scanner = new Scanner(System.in);
initProducts(); // 初始化一些测试商品
}
// 初始化默认商品
private void initProducts() {
products.add(new Product("001", "矿泉水", 2.0, 50));
products.add(new Product("002", "面包", 8.0, 30));
products.add(new Product("003", "牛奶", 15.0, 20));
products.add(new Product("004", "苹果", 6.5, 100));
}
// 显示菜单
private void showMenu() {
System.out.println("\n=== 欢迎光临超市自助结账系统 ===");
System.out.println("1. 浏览所有商品");
System.out.println("2. 添加商品到购物车");
System.out.println("3. 查看购物车");
System.out.println("4. 结算并打印小票");
System.out.println("5. 退出系统");
System.out.print("请选择操作:");
}
// 执行主循环
public void run() {
while (true) {
showMenu();
String choice = scanner.nextLine();
switch (choice) {
case "1" -> displayProducts();
case "2" -> addToCart();
case "3" -> viewCart();
case "4" -> checkout();
case "5" -> {
System.out.println("感谢使用,再见!");
return;
}
default -> System.out.println("无效选择,请重新输入。");
}
}
}
// 展示所有商品
private void displayProducts() {
System.out.println("\n--- 商品列表 ---");
for (Product p : products) {
System.out.println(p);
}
}
// 添加商品到购物车
private void addToCart() {
System.out.print("请输入商品编号: ");
String id = scanner.nextLine();
Product p = findProductById(id);
if (p == null) {
System.out.println("未找到该商品!");
return;
}
System.out.print("请输入购买数量: ");
int qty;
try {
qty = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException e) {
System.out.println("数量格式错误!");
return;
}
if (qty <= 0 || qty > p.getStock()) {
System.out.println("库存不足或数量无效!");
return;
}
// 减少库存
p.setStock(p.getStock() - qty);
// 添加到购物车(如果已有则合并)
for (CartItem item : cart) {
if (item.getProduct().getId().equals(id)) {
item.setQuantity(item.getQuantity() + qty);
System.out.println("已将 " + qty + " 件 " + p.getName() + " 加入购物车(合并)");
return;
}
}
cart.add(new CartItem(p, qty));
System.out.println("已将 " + qty + " 件 " + p.getName() + " 加入购物车");
}
// 查看购物车内容
private void viewCart() {
if (cart.isEmpty()) {
System.out.println("购物车为空!");
return;
}
System.out.println("\n--- 当前购物车 ---");
for (CartItem item : cart) {
System.out.printf("商品: %s | 数量: %d | 小计: %.2f元\n",
item.getProduct().getName(),
item.getQuantity(),
item.getTotalPrice());
}
}
// 结算并打印小票
private void checkout() {
if (cart.isEmpty()) {
System.out.println("购物车为空,无法结算!");
return;
}
System.out.println("\n正在生成小票...");
System.out.println("================== 超市收据 ==================");
double total = 0.0;
for (CartItem item : cart) {
double itemTotal = item.getTotalPrice();
System.out.printf("%s × %d = %.2f元\n",
item.getProduct().getName(),
item.getQuantity(),
itemTotal);
total += itemTotal;
}
System.out.println("============================================");
System.out.printf("总计: %.2f元\n", total);
// 可扩展:会员折扣
boolean isMember = askIfMember();
if (isMember) {
double discount = 0.9; // 9折
double discounted = total * discount;
System.out.printf("会员9折后: %.2f元\n", discounted);
total = discounted;
}
System.out.printf("实付: %.2f元\n", total);
System.out.println("谢谢惠顾,欢迎下次光临!");
// 清空购物车
cart.clear();
}
// 询问是否为会员
private boolean askIfMember() {
System.out.print("是否为会员?(y/n): ");
String input = scanner.nextLine().trim().toLowerCase();
return input.equals("y") || input.equals("yes");
}
// 根据ID查找商品
private Product findProductById(String id) {
for (Product p : products) {
if (p.getId().equals(id)) {
return p;
}
}
return null;
}}
<H3>3. 启动类 Main</H3>
<p>程序入口,运行系统。</p>
```java
public class Main {
public static void main(String[] args) {
new SupermarketSystem().run();
}
}编译并运行后,用户可在控制台看到菜单,例如:
=== 欢迎光临超市自助结账系统 === 1. 浏览所有商品 2. 添加商品到购物车 3. 查看购物车 4. 结算并打印小票 5. 退出系统 请选择操作:1
选择后可完成完整购物流程。结算时会自动计算总金额,并根据会员状态打折。
基本上就这些。这个系统虽然简单,但涵盖了输入处理、数据封装、集合操作和业务流程控制,非常适合练习Java基础和OOP思想。后续可扩展数据库存储、图形界面或商品分类等功能。
以上就是Java实现超市结账系统_控制台交互式项目完整逻辑的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号