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

}

}

相关推荐
Goldn.4 小时前
Java核心技术栈全景解析:从Web开发到AI融合
java· spring boot· 微服务· ai· jvm· maven· hibernate
李慕婉学姐5 小时前
【开题答辩过程】以《基于Android的出租车运行监测系统设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·后端·vue
m0_740043735 小时前
SpringBoot05-配置文件-热加载/日志框架slf4j/接口文档工具Swagger/Knife4j
java·spring boot·后端·log4j
编织幻境的妖5 小时前
SQL查询连续登录用户方法详解
java·数据库·sql
未若君雅裁5 小时前
JVM面试篇总结
java·jvm·面试
kk哥88996 小时前
C++ 对象 核心介绍
java·jvm·c++
招风的黑耳6 小时前
我用SpringBoot撸了一个智慧水务监控平台
java·spring boot·后端
xunyan62346 小时前
面向对象(下)-接口的理解
java·开发语言
程序员游老板6 小时前
基于SpringBoot3+vue3的爱心陪诊平台
java·spring boot·毕业设计·软件工程·课程设计·信息与通信
期待のcode6 小时前
Springboot核心构建插件
java·spring boot·后端