正文
目录会跟随阅读位置移动。
阅读进度

最近一直在练面向对象、类、集合这些基础知识点,总觉得光看理论太抽象。所以写一个简单的项目关于购物车程序. 本程序实现功能 :
创建商品(编号、名称、价格)
添加商品到购物车(防重复)
按 ID 删除购物车商品
按 ID 修改商品数量(累加)
自动计算单个商品小计
自动计算购物车总价
控制台展示购物车详情
Demo01
Product.java
CartItem.java
ShoppingCart.java
Test01.java
package Demo01;
/**
* 商品实体类:描述商品的基本信息
*/
public class Product {
// 商品编号
private String id;
// 商品名称
private String name;
// 商品价格
private double price;
// 无参构造
public Product() {
}
// 带参构造
public Product(String name, double count, String id) {
this.name = name;
this.price = count;
this.id = id;
}
// 获取价格
public double getPrice() {
return price;
}
// 设置价格
public void setPrice(double price) {
this.price = price;
}
// 获取商品名称
public String getName() {
return name;
}
// 设置商品名称
public void setName(String name) {
this.name = name;
}
// 获取商品编号
public String getId() {
return id;
}
// 设置商品编号
public void setId(String id) {
this.id = id;
}
// 重写toString方法
@Override
public String toString() {
return "Product{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", count=" + price +
'}';
}
}
package Demo01;
/**
* 购物车项:表示购物车里的一个商品项(商品 + 数量)
*/
public class CartItem {
// 商品对象
private Product product;
// 购买数量
private int count;
// 无参构造
public CartItem() {
}
// 带参构造:绑定商品和数量
public CartItem(Product product, int count) {
this.product = product;
this.count = count;
}
// 获取数量
public int getCount() {
return count;
}
// 设置数量
public void setCount(int count) {
this.count = count;
}
// 获取当前项的商品
public Product getProduct() {
return product;
}
// 计算当前商品项的小计:单价 × 数量
public double getSubTotal(){
return product.getPrice()*this.count;
}
}
##模块 3. ShoppingCart.java
package Demo01;
import java.util.ArrayList;
import java.util.List;
/**
* 购物车类:管理所有商品项
*/
public class ShoppingCart {
// 购物车项集合
private List<CartItem> cartItems;
// 初始化购物车
public ShoppingCart(){
this.cartItems =new ArrayList<>();
}
/**
* 添加商品到购物车
* 不存在则添加,存在则不处理
*/
public void addItem(Product product,int count) {
boolean found = false;
// 遍历判断商品是否已存在
for (CartItem item : cartItems) {
if(item.getProduct().getId().equals(product.getId())){
found = true;
break;
}
}
// 不存在则新增
if(!found){
CartItem newItem = new CartItem(product,count);
cartItems.add(newItem);
System.out.println("添加新商品"+product.getName());
}
}
/**
* 根据商品ID删除商品
*/
public void removeItem(String productId) {
boolean removed = cartItems.removeIf(Item -> Item.getProduct().getId().equals(productId));
if(removed){
System.out.println("【删除】商品ID为 " + productId + " 的商品已移除");
}
else{
System.out.println("【提示】购物车中未找到ID为 " + productId + " 的商品");
}
}
/**
* 更新商品数量(累加)
*/
public void updateQuantity(String productId,int count){
boolean found = false;
for (CartItem item : cartItems) {
if(item.getProduct().getId().equals(productId)){
int oldCount = item.getCount();
int newCount = item.getCount() + count;
item.setCount(newCount);
found=true;
System.out.println("商品"+item.getProduct().getName()+"已更新"+oldCount+"→"+item.getCount());
break;
}
}
if(!found){
System.out.println("无该商品,需要添加");
}
}
// 购物车总金额
double subtotal;
// 临时存储单个商品小计
double itemTotal;
/**
* 计算购物车总金额
*/
public void subTotal(){
for (CartItem item : cartItems) {
itemTotal = item.getSubTotal();
subtotal += itemTotal;
}
}
/**
* 展示购物车所有内容与总价
*/
public void show(){
System.out.println("=======我的购物车======");
for (CartItem item : cartItems) {
System.out.println("商品"+item.getProduct().getName()+"\n"+ "单价"+item.getProduct().getPrice()+"\n"+ "数量"+item.getCount()+"\n"+ "商品单个总和"+item.getSubTotal());
}
System.out.println("=========================");
// 计算并输出总价
subTotal();
System.out.println("商品价格总和: " + subtotal);
}
}
package Demo01;
/**
* 测试类:程序入口
*/
public class Test01 {
public static void main(String[] args) {
// 创建商品
Product p1 = new Product("iPhone 15", 5999.0, "001");
Product p2 = new Product("AirPods Pro", 1899.0, "002");
Product p3 = new Product("MacBook Pro", 12999.0, "003");
// 创建购物车
ShoppingCart cart = new ShoppingCart();
// 添加商品
cart.addItem(p1, 1);
cart.addItem(p2, 2);
cart.addItem(p1, 1);
// 展示购物车内容与总价
cart.show();
}
}
添加新商品iPhone 15
添加新商品AirPods Pro
=======我的购物车======
商品iPhone 15
单价5999.0
数量1
商品单个总和5999.0
商品AirPods Pro
单价1899.0
数量2
商品单个总和3798.0
=========================
商品价格总和: 9797.0