需完成的功能:
交互系统
添加商品至购物车
查看购物车
结算购物车
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;
}
}