购物车是电商类Java项目的核心功能模块之一,对于初学者来说,设计购物车功能需要先从业务场景出发,梳理清楚核心流程,再通过面向对象的思路拆分功能模块,最终实现商品添加和总价计算的基础逻辑。

购物车功能的核心需求梳理
基础版本的购物车需要满足以下核心需求:
- 可以存储多个不同商品的信息,包含商品名称、单价、购买数量
- 支持添加商品到购物车,若商品已存在则累加购买数量
- 可以实时计算购物车中所有商品的总价,总价等于每个商品单价乘以购买数量的总和
基础类的设计与定义
首先需要定义两个核心类,分别是Product商品类和ShoppingCart购物车类,通过类的属性存储对应信息,通过方法实现对应功能。
商品类Product定义
商品类需要包含商品的基本属性和对应的访问方法,代码如下:
public class Product {
// 商品id
private String id;
// 商品名称
private String name;
// 商品单价
private double price;
// 购买数量
private int quantity;
// 构造方法
public Product(String id, String name, double price, int quantity) {
this.id = id;
this.name = name;
this.price = price;
this.quantity = quantity;
}
// 计算单个商品的总价
public double getTotalPrice() {
return price * quantity;
}
// getter和setter方法
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;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
购物车类ShoppingCart定义
购物车类需要维护一个商品列表,同时提供添加商品、计算总价的方法,代码如下:
import java.util.ArrayList;
import java.util.List;
public class ShoppingCart {
// 存储购物车中的商品列表
private List<Product> productList;
public ShoppingCart() {
productList = new ArrayList<>();
}
/**
* 添加商品到购物车
* 如果商品已存在,累加数量;如果不存在,新增商品到列表
* @param product 待添加的商品
*/
public void addProduct(Product product) {
// 先遍历现有商品列表,判断商品是否已存在
for (Product item : productList) {
if (item.getId().equals(product.getId())) {
// 商品已存在,累加购买数量
item.setQuantity(item.getQuantity() + product.getQuantity());
return;
}
}
// 商品不存在,新增到列表
productList.add(product);
}
/**
* 计算购物车所有商品的总价
* @return 购物车总价
*/
public double getTotalPrice() {
double total = 0.0;
for (Product product : productList) {
total += product.getTotalPrice();
}
return total;
}
/**
* 获取购物车中的商品列表
* @return 商品列表
*/
public List<Product> getProductList() {
return productList;
}
}
功能测试与逻辑验证
完成类的定义后,可以编写测试代码验证商品添加和总价计算的逻辑是否符合预期:
public class ShoppingCartTest {
public static void main(String[] args) {
// 创建购物车实例
ShoppingCart cart = new ShoppingCart();
// 创建两个商品实例
Product product1 = new Product("p001", "Java入门教程", 89.9, 1);
Product product2 = new Product("p002", "编程笔记本", 29.9, 2);
// 添加商品到购物车
cart.addProduct(product1);
cart.addProduct(product2);
// 再次添加p001商品,数量为1,测试数量累加逻辑
Product product3 = new Product("p001", "Java入门教程", 89.9, 1);
cart.addProduct(product3);
// 打印购物车商品信息
System.out.println("购物车商品列表:");
for (Product item : cart.getProductList()) {
System.out.println("商品名称:" + item.getName() + ",单价:" + item.getPrice() + ",数量:" + item.getQuantity() + ",单商品总价:" + item.getTotalPrice());
}
// 计算并打印购物车总价
double total = cart.getTotalPrice();
System.out.println("购物车总价:" + total);
}
}
运行上述测试代码,输出结果如下:
购物车商品列表: 商品名称:Java入门教程,单价:89.9,数量:2,单商品总价:179.8 商品名称:编程笔记本,单价:29.9,数量:2,单商品总价:59.8 购物车总价:239.6
可以看到,重复添加相同商品时数量正确累加,总价计算也符合预期,说明基础逻辑实现正确。
逻辑优化与扩展思路
上述实现是基础版本,初学者可以在此基础上做更多扩展:
- 增加商品移除方法,支持从购物车中删除指定商品
- 增加修改商品数量的方法,支持手动调整购买数量
- 增加商品库存校验逻辑,添加商品时判断库存是否充足
- 将商品列表的存储改为使用Map结构,以商品id为key,提升查找效率
通过逐步扩展功能,可以更深入理解购物车模块的设计思路,也能提升Java面向对象的编程能力。