Java实现购物车功能_面向对象思维下的购物系统建模

答案:通过面向对象设计,构建商品、购物车项和购物车类,实现添加、删除、更新和计算总价功能。

实现一个购物车功能,关键在于合理使用面向对象的设计思想对系统进行建模。我们需要识别现实中的实体,将其抽象为类,并定义它们之间的关系和行为。下面以Java语言为例,展示如何从零构建一个简洁、可扩展的购物车系统。

1. 系统核心类设计

购物系统中最基本的几个实体是商品(Product)购物车项(CartItem)购物车(ShoppingCart)。每个类承担明确职责,符合单一职责原则。

  • Product:表示商品信息,包含名称、价格、编号等属性。
  • CartItem:代表购物车中的一项,包含商品和购买数量。
  • ShoppingCart:管理所有购物车项,提供添加、删除、修改数量和计算总价等功能。

2. 商品类(Product)

商品是系统的基础单位,应具备不可变性,避免在添加到购物车后被意外修改。

public class Product {
    private final String id;
    private final String name;
    private final double price;
public Product(String id, String name, double price) {
    this.id = id;
    this.name = name;
    this.price = price;
}

// getter方法
public String getId() { return id; }
public String getName() { return name; }
public double getPrice() { return price; }

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Product)) return false;
    Product product = (Product) o;
    return id.equals(product.id);
}

@Override
public int hashCode() {
    return id.hashCode();
}

}

3. 购物车项类(CartItem)

购物车项封装了商品和其购买数量,支持数量更新和小计计算。

public class CartItem {
    private Product product;
    private int quantity;
public CartItem(Product product, int quantity) {
    this.product = product;
    this.quantity = quantity;
}

public void setQuantity(int quantity) {
    if (quantity > 0) {
        this.quantity = quantity;
    }
}

public double getSubtotal() {
    return product.getPrice() * quantity;
}

public Product getProduct() {
    return product;
}

public int getQuantity() {
    return quantity;
}

}

4. 购物车类(ShoppingCart)

购物车是核心管理类,使用Map结构通过商品ID快速查找和合并重复商品。

import java.util.*;

public class ShoppingCart { private Map items;

public ShoppingCart() {
    this.items = new HashMap<>();
}

public void addProduct(Product product, int quantity) {
    if (qua

ntity <= 0) return; if (items.containsKey(product.getId())) { CartItem item = items.get(product.getId()); item.setQuantity(item.getQuantity() + quantity); } else { items.put(product.getId(), new CartItem(product, quantity)); } } public void removeProduct(String productId) { items.remove(productId); } public void updateQuantity(String productId, int quantity) { CartItem item = items.get(productId); if (item != null) { if (quantity <= 0) { items.remove(productId); } else { item.setQuantity(quantity); } } } public double getTotalPrice() { return items.values().stream() .mapToDouble(CartItem::getSubtotal) .sum(); } public void showItems() { for (CartItem item : items.values()) { System.out.printf("商品: %s, 单价: %.2f, 数量: %d, 小计: %.2f%n", item.getProduct().getName(), item.getProduct().getPrice(), item.getQuantity(), item.getSubtotal()); } System.out.printf("总计: %.2f元%n", getTotalPrice()); } public int getItemCount() { return items.size(); } public boolean isEmpty() { return items.isEmpty(); }

}

5. 使用示例

测试代码展示基本操作流程:

public class Main {
    public static void main(String[] args) {
        Product p1 = new Product("P001", "iPhone", 6999.0);
        Product p2 = new Product("P002", "AirPods", 1299.0);
    ShoppingCart cart = new ShoppingCart();
    cart.addProduct(p1, 1);
    cart.addProduct(p2, 2);
    cart.addProduct(p1, 1); // 合并

    cart.showItems();
    // 输出:
    // 商品: iPhone, 单价: 6999.00, 数量: 2, 小计: 13998.00
    // 商品: AirPods, 单价: 1299.00, 数量: 2, 小计: 2598.00
    // 总计: 16596.00元
}

}

基本上就这些。通过合理的类划分和封装,这个购物车系统具备良好的扩展性,后续可以加入折扣策略、库存校验、持久化等功能。重点是保持类职责清晰,数据一致性由对象自身维护。不复杂但容易忽略。