答案:实现了一个基于控制台的购物车系统,包含商品浏览、添加、删除、修改数量和查看购物车功能。通过Product类表示商品信息,CartItem类记录商品及数量,ShoppingCart类管理购物车操作,主类ShoppingSystem提供用户交互界面,使用Scanner接收输入,List存储购物项,支持重复商品数量叠加与实时总价计算,适合Java初学者练习面向对象编程基础。

实现一个初级的购物车功能,可以基于控制台(命令行)应用来完成,使用Java基础语法和面向对象思想。以下是实现步骤和代码结构说明,适合初学者理解核心逻辑。
1. 定义商品类(Product)
每个商品应包含基本信息:编号、名称、价格。
class Product {
private int id;
private String name;
private double price;
public Product(int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
}
// Getter方法
public int getId() { return id; }
public String getName() { return name; }
public double getPrice() { return price; }
@Override
public String toString() {
return "ID:" + id + " | 名称:" + name + " | 价格:" + price + "元";
}}
2. 定义购物车项类(CartItem)
表示购物车中某个商品及其数量。
立即学习“Java免费学习笔记(深入)”;
class CartItem {
private Product product;
private int quantity;
public CartItem(Product product, int quantity) {
this.product = product;
this.quantity = quantity;
}
public Product getProduct() { return product; }
public int getQuantity() { return quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }
public double getTotalPrice() {
return product.getPrice() * quantity;
}}
3. 购物车类(ShoppingCart)
管理添加商品、删除商品、修改数量、显示购物车内容等功能。
import java.util.ArrayList; import java.util.List;class ShoppingCart { private List
items = new ArrayList<>(); // 添加商品到购物车 public void addProduct(Product product, int quantity) { for (CartItem item : items) { if (item.getProduct().getId() == product.getId()) { item.setQuantity(item.getQuantity() + quantity); System.out.println("已将商品【" + product.getName() + "】数量增加" + quantity + "个。"); return; } } items.add(new CartItem(product, quantity)); System.out.println("已添加商品【" + product.getName() + "】到购物车。"); } // 删除商品 public void removeProduct(int productId) { items.removeIf(item -> item.getProduct().getId() == productId); System.out.println("已从购物车移除ID为" + productId + "的商品。"); } // 修改商品数量 public void updateQuantity(int productId, int newQuantity) { for (CartItem item : items) { if (item.getProduct().getId() == productId) { if (newQuantity <= 0) { removeProduct(productId); } else { item.setQuantity(newQuantity); System.out.println("商品【" + item.getProduct().getName() + "】数量更新为" + newQuantity); } return; } } System.out.println("购物车中没有找到该商品。"); } // 显示购物车内容 public void showCart() { if (items.isEmpty()) { System.out.println("购物车为空!"); return; } System.out.println("\n--- 购物车内容 ---"); double total = 0.0; for (CartItem item : items) { double itemTotal = item.getTotalPrice(); System.out.printf("商品: %s, 数量: %d, 小计: %.2f元%n", item.getProduct().getName(), item.getQuantity(), itemTotal); total += itemTotal; } System.out.printf("总计: %.2f元%n", total); }}
4. 主程序入口(测试类)
模拟用户操作流程:展示商品、选择操作、管理购物车。
import java.util.Scanner;public class ShoppingSystem { public static void main(String[] args) { Scanner input = new Scanner(System.in);
// 初始化商品列表 Product[] products = { new Product(1, "苹果", 5.0), new Product(2, "香蕉", 3.5), new Product(3, "橙子", 4.0) }; ShoppingCart cart = new ShoppingCart(); while (true) { System.out.println("\n=== 简易购物系统 ==="); System.out.println("1. 浏览商品"); System.out.println("2. 添加商品到购物车"); System.out.println("3. 查看购物车"); System.out.println("4. 修改商品数量"); System.out.println("5. 删除商品"); System.out.println("6. 退出"); System.out.print("请选择操作:"); int choice = input.nextInt(); switch (choice) { case 1: System.out.println("\n--- 商品列表 ---"); for (Product p : products) { System.out.println(p); } break; case 2: System.out.print("输入商品ID:"); int id = input.nextInt(); System.out.print("输入购买数量:"); int qty = input.nextInt(); Product selected = null; for (Product p : products) { if (p.getId() == id) { selected = p; break; } } if (selected != null) { cart.addProduct(selected, qty); } else { System.out.println("商品不存在!"); } break; case 3: cart.showCart(); break; case 4: System.out.print("输入商品ID:"); int updateId = input.nextInt(); System.out.print("输入新数量(0删除):"); int newQty = input.nextInt(); cart.updateQuantity(updateId, newQty); break; case 5: System.out.print("输入要删除的商品ID:"); int delId = input.nextInt(); cart.removeProduct(delId); break; case 6: System.out.println("感谢使用!"); input.close(); return; default: System.out.println("无效选择,请重试。"); } } }}
这个项目涵盖了Java基础的核心知识点:类与对象、封装、集合(List)、循环、条件判断、Scanner输入等。适合练手,后续可扩展数据库存储、图形界面(Swing/JavaFX)或Web版本(Servlet/JSP)。基本上就这些,不复杂但容易忽略细节比如空指针或重复添加处理。自己动手改一改,理解更深刻。










