第28节课-夕夕超市收银系统(下)-笔记

需完成的功能:
交互系统
添加商品至购物车
查看购物车
结算购物车


01 交互系统

包含三部分(界面展示、用户输入、指令处理)

整体代码:

private static void interactionSystem(Scanner scanner) {

// 界面展示

System.out.println("===== 夕夕超市系统 =====");

System.out.println("1. 展示商品列表");

System.out.println("2. 添加商品到购物车");

System.out.println("3. 查看购物车");

System.out.println("4. 结算");

System.out.println("5. 退出");

System.out.print("请选择操作(1-5): ");

// 用户输入

int choice = scanner.nextInt();

scanner.nextLine(); // 消耗换行符

// 指令处理

switch (choice) {

case 1:

displayProductList();

break;

case 2:

addToShoppingCart(scanner);

break;

case 3:

viewShoppingCart();

break;

case 4:

checkout();

break;

case 5:

System.out.println("感谢使用夕夕超市系统,再见!");

System.exit(0);

default:

System.out.println("错误的指令,请重新输入!");

}

}

02 添加商品到购物车

思路:需要一个容器(购物车)装存储所选的商品,其次,展示商品列表以供选择,最后,实现添加商品的逻辑

完整代码:

// 添加到购物车

private static void addToShoppingCart(Scanner scanner) {

// 展示商品列表

displayProductList();

System.out.print("请输入要购买的商品编号: ");

String productId = scanner.nextLine();

if (productDatabase.containsKey(productId)) {

System.out.print("请输入购买数量:");

int quantity = scanner.nextInt();

scanner.nextLine(); // 消耗换行符

// 获取当前购物车中输入的商品数量

Integer currentQuantity = shoppingCart.get(productId);

// 通过if-else判断添加商品到购物车

if (currentQuantity != null) {

shoppingCart.put(productId, currentQuantity + quantity);

} else {

shoppingCart.put(productId, quantity);

}

System.out.println("商品已添加到购物车!");

} else {

System.out.println("无效的商品编号!");

}

}

03 查看购物车

完整代码:

// 查看购物车

private static void viewShoppingCart() {

System.out.println("===== 购物车 =====");

if (shoppingCart.isEmpty()) {

System.out.println("购物车是空的!");

} else {

// 按照要求,使用entrySet()方法遍历购物车

for (Map.Entry<String, Integer> entry : shoppingCart.entrySet()) {

String productId = entry.getKey();

int quantity = entry.getValue();

Product product = productDatabase.get(productId);

System.out.println(product.getName() + " x" + quantity + " - ¥" + product.getPrice() * quantity);

}

}

}

04 结算

// 结算

private static void checkout() {

System.out.println("===== 结算 =====");

// 定义totalAmount保存商品的总金额

double totalAmount = 0.0;

for (Map.Entry<String, Integer> entry : shoppingCart.entrySet()) {

String productId = entry.getKey();

int quantity = entry.getValue();

Product product = productDatabase.get(productId);

// 计算每类商品的金额,然后累加到totalAmount上

totalAmount += product.getPrice() * quantity;

}

System.out.println("总计:¥" + totalAmount);

// 清空购物车

shoppingCart.clear();

System.out.println("结算完成!");

}

}


项目完整代码:

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;

/**

* 夕夕超市收银系统

*/

public class SupermarketSystem {

// 商品集合,键是商品编号,值是商品对象Product

private static Map<String, Product> productDatabase = new HashMap<>();

// 用户购物车,键是商品编号,值是购买数量

private static Map<String, Integer> shoppingCart = new HashMap<>();

public static void main(String[] args) {

initializeProducts();

Scanner scanner = new Scanner(System.in);

while (true) {

interactionSystem(scanner);

}

}

// 交互系统

private static void interactionSystem(Scanner scanner) {

System.out.println("===== 夕夕超市系统 =====");

System.out.println("1. 展示商品列表");

System.out.println("2. 添加商品到购物车");

System.out.println("3. 查看购物车");

System.out.println("4. 结算");

System.out.println("5. 退出");

System.out.print("请选择操作(1-5): ");

int choice = scanner.nextInt();

scanner.nextLine(); // 消耗换行符

switch (choice) {

case 1:

displayProductList();

break;

case 2:

addToShoppingCart(scanner);

break;

case 3:

viewShoppingCart();

break;

case 4:

checkout();

break;

case 5:

System.out.println("感谢使用夕夕超市系统,再见!");

System.exit(0);

default:

System.out.println("错误的指令,请重新输入!");

}

}

// 初始化商品信息

private static void initializeProducts() {

productDatabase.put("001", new Product("001", "苹果", 5.0));

productDatabase.put("002", new Product("002", "香蕉", 3.0));

productDatabase.put("003", new Product("003", "橘子", 4.5));

}

// 展示商品列表

private static void displayProductList() {

System.out.println("===== 商品列表 =====");

for (Map.Entry<String, Product> entry : productDatabase.entrySet()) {

String productId = entry.getKey();

Product product = entry.getValue();

System.out.println(productId + ". " + product.getName() + " - ¥" + product.getPrice());

}

}

// 添加商品到购物车

private static void addToShoppingCart(Scanner scanner) {

displayProductList();

System.out.print("请输入要购买的商品编号: ");

String productId = scanner.nextLine();

if (productDatabase.containsKey(productId)) {

System.out.print("请输入购买数量:");

int quantity = scanner.nextInt();

scanner.nextLine(); // 消耗换行符

Integer currentQuantity = shoppingCart.get(productId);

if (currentQuantity != null) {

shoppingCart.put(productId, currentQuantity + quantity);

} else {

shoppingCart.put(productId, quantity);

}

System.out.println("商品已添加到购物车!");

} else {

System.out.println("无效的商品编号!");

}

}

// 查看购物车

private static void viewShoppingCart() {

System.out.println("===== 购物车 =====");

if (shoppingCart.isEmpty()) {

System.out.println("购物车是空的!");

} else {

// 按照要求,使用entrySet()方法遍历购物车

for (Map.Entry<String, Integer> entry : shoppingCart.entrySet()) {

String productId = entry.getKey();

int quantity = entry.getValue();

Product product = productDatabase.get(productId);

System.out.println(product.getName() + " x" + quantity + " - ¥" + product.getPrice() * quantity);

}

}

}

// 结算

private static void checkout() {

System.out.println("===== 结算 =====");

double totalAmount = 0.0;

for (Map.Entry<String, Integer> entry : shoppingCart.entrySet()) {

String productId = entry.getKey();

int quantity = entry.getValue();

Product product = productDatabase.get(productId);

totalAmount += product.getPrice() * quantity;

}

System.out.println("总计:¥" + totalAmount);

shoppingCart.clear();

System.out.println("结算完成!");

}

}

/**

* 商品类

*/

class Product {

private String id; // 商品编码

private String name; // 商品名称

private double price; // 商品价格

// 构造方法

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;

}

}

相关推荐
zzqssliu13 分钟前
SpringBoot框架搭建跨境独立站|Taocarts代购系统订单模块深度开发
java·spring boot·后端
dinl_vin27 分钟前
FastAPI 系列 ·(四):数据库集成——SQLAlchemy 2.0 异步 ORM 与 Alembic 迁移
java·数据库·fastapi
编码者卢布29 分钟前
【Azure Service Bus】Azure Service Bus Java SDK 中 Token 刷新异常的排查思路
java·python·azure
兰令水31 分钟前
topcode【随机算法题】【2026.5.20打卡-java版本】
java·开发语言·算法
AI瓦力43 分钟前
技术分享 | 彻底解决图片“躺平”问题:Java 后端强制校准图片方向
java
武子康1 小时前
Java-219 RocketMQ Spring Boot 集成指南:生产者与消费者实战
java·spring boot·分布式·kafka·消息队列·rocketmq·java-rocketmq
RainCityLucky1 小时前
Java Swing 自定义组件库分享(七)
java·笔记·后端
小白|1 小时前
cmake:昇腾CANN构建系统完全指南
java·c++·算法
weixin_512976171 小时前
Java 面试宝典 Beta5.0
java
Ting-yu1 小时前
Spring AI Alibaba零基础速成(5) ---- Memory(记忆)
java·人工智能·后端·spring