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

}

}

相关推荐
葡萄成熟时 !3 小时前
JAVA 常用API学习笔记
java·笔记·学习
Rain的Java大神之路4 小时前
介绍一下分布式事务
java·分布式·后端·spring·spring cloud·架构·springcloud
吴声子夜歌5 小时前
Java面试题——基础(一)
java·开发语言
南城以南溫暖如初1475 小时前
外卖CPS实战指南:从技术选型到运营落地全流程解析
java·spring boot·mysql·架构·vue
傻啦嘿哟6 小时前
英雄联盟皮肤爬虫:爬取全皮肤价格与特效,做比价工具
java·c++·爬虫
码匠许师傅7 小时前
【C++ 面试真题】24. 聊聊 C++ 的时间日期处理
java·c++·面试
mqiqe8 小时前
AgentScope Java 2.0 Agent 状态存储(AgentStateStore)深度解析:构建可恢复、可扩展的智能体运行时
java·运维·网络
zhifou1234568 小时前
java 17升级安装
java·开发语言
用户3721574261358 小时前
如何使用 Java 将 Markdown 转换为 PDF(含自定义设置)
java
用户3721574261359 小时前
如何使用 Java 在 Word 文档中添加和删除水印:分步指南
java