答:系统实现商品管理、购物车操作和结算功能,包括Product、cartitem和supermarketsystem支持浏览商品,添加到购物车中,查看 cart、通过控制台交互完成会员折扣结算和打印收据的购物过程。

Java实现控制台交互式超市结账系统的核心目标是模拟真实购物场景下的商品管理、客户购买、结算和收据打印功能。整个项目结构清晰,适合初学者掌握对象编程、集合操作和控制台输入输出处理。
1. 系统功能需求分析系统应具备以下基本功能:
- 商品管理:添加商品(编号、名称、价格、库存)
- 浏览商品2>
- 顾客购物:根据商品编号选择,输入购买数量
- 结算功能:计算总价,支持折扣或会员折扣
- 打印收据:显示购买商品的细节和最终金额
- 退出系统:安全退出程序
以下类别采用面向对象设计:
① 商品类(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;
}
}
逻辑智能
InsiderX:构建每个团队都可以轻松定制的智能员工
83
<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("无效的选择,请重新输入。");
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();
}
}4. 使用说明和操作效果
用户可以在控制台上看到菜单,例如:
=== 欢迎光临超市自助结账系统 === 1. 浏览所有商品 2. 将商品添加到购物车中 3. 查看购物车 4. 结算并打印小票 5. 退出系统 请选择操作:1
完整的购物过程可以在选择后完成。总金额将在结算时自动计算,并根据会员状态进行折扣。
基本上就是这样。该系统虽然简单,但涵盖了输入处理、数据包装、集合操作和业务流程控制,非常适合Java基础和OOP思维的实践。数据库存储、图形界面或商品分类等功能可以随后扩展。
以上是Java超市结账系统_控制台交互式项目完整逻辑的详细内容。请关注图灵教育的其他相关文章!
相关标签:
java 苹果 ai switch 面向对象编程 会员 java实现 Java String 面向对象 封装 int double 数据封装 class public private 对象 数据库
大家都在看:
Java文件执行指南:正确使用java命令和claspathh Java的深入分析 String的不可变性和方法参数传输机制 对Java有深刻的理解 String的不可变性和方法参数传输机制 Java数组最小值的正确搜索方法和常见陷阱分析 Java程序设计:月降雨统计与分析教程