第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;

}

}

相关推荐
不知名的老吴2 分钟前
线程的生命周期之线程同步
java·开发语言·jvm
协享科技4 分钟前
Spring Boot 与 Go 双服务架构实践:从单体拆分到通信设计
java·人工智能·spring boot·后端·架构·golang·ai编程
码语智行1 小时前
地图上图、空间拓扑查询示例
java·arcgis
程序员黑豆1 小时前
AI全栈开发 - Java:变量
java·前端·ai编程
我是一颗柠檬1 小时前
【Java项目技术亮点】分库分表+数据路由策略:单表5000万后的架构升级方案
java·开发语言·分布式·架构
布朗克1681 小时前
25 IO流高级操作——序列化、NIO与Files工具类
java·数据库·io·nio
小研说技术1 小时前
Spring AI实现rag流程(简易版)
java·后端
亓才孓1 小时前
【本地项目引用外部库的类,想修改字段遇到的请缓存的问题】
java·maven
小林敲代码77882 小时前
记录一下IDEA中很多变量变色的方案
java·开发语言·spring boot·idea
南知意-2 小时前
IDEA 2026.1最新版安装教程
java·ide·intellij-idea·idea安装·idea激活