4. 订单类 (Order)
java复制代码
|
import java.time.LocalDate; |
|
import java.util.ArrayList; |
|
import java.util.List; |
|
|
|
public class Order { |
|
private int orderId; |
|
private User user; |
|
private LocalDate orderDate; |
|
private List<OrderItem> orderItems; |
|
private double totalPrice; |
|
private String shippingAddress; |
|
|
|
public Order(User user, LocalDate orderDate, String shippingAddress) { |
|
this.user = user; |
|
this.orderDate = orderDate; |
|
this.orderItems = new ArrayList<>(); |
|
this.shippingAddress = shippingAddress; |
|
} |
|
|
|
// 添加订单项 |
|
public void addOrderItem(Product product, int quantity) { |
|
orderItems.add(new OrderItem(product, quantity)); |
|
calculateTotalPrice(); |
|
} |
|
|
|
// 计算总价 |
|
private void calculateTotalPrice() { |
|
totalPrice = orderItems.stream() |
|
.mapToDouble(orderItem -> orderItem.getProduct().getPrice() * orderItem.getQuantity()) |
|
.sum(); |
|
} |
|
|
|
// Getters and Setters |
|
public int getOrderId() { |
|
return orderId; |
|
} |
|
|
|
public void setOrderId(int orderId) { |
|
this.orderId = orderId; |
|
} |
|
|
|
public User getUser() { |
|
return user; |
|
} |
|
|
|
public LocalDate getOrderDate() { |
|
return orderDate; |
|
} |
|
|
|
public List<OrderItem> getOrderItems() { |
|
return orderItems; |
|
} |
|
|
|
public double getTotalPrice() { |
|
return totalPrice; |
|
} |
|
|
|
public String getShippingAddress() { |
|
return shippingAddress; |
|
} |
|
|
|
public void setShippingAddress(String shippingAddress) { |
|
this.shippingAddress = shippingAddress; |
|
} |
|
|
|
// toString method |
|
@Override |
|
public String toString() { |
|
return "Order{" + |
|
"orderId=" + orderId + |
|
", user=" + user + |
|
", orderDate=" + orderDate + |
|
", orderItems=" + orderItems + |
|
", totalPrice=" + totalPrice + |
|
", shippingAddress='" + shippingAddress + '\'' + |
|
'}'; |
|
} |
|
|
|
// 订单项类 |
|
static class OrderItem { |
|
private Product product; |
|
private int quantity; |
|
|
|
public OrderItem(Product product, int quantity) { |
|
this.product = product; |
|
this.quantity = quantity; |
|
} |
|
|
|
public Product getProduct() { |
|
return product; |
|
} |
|
|
|
public int getQuantity() { |
|
return quantity; |
|
} |
|
|
|
// toString method |
|
@Override |
|
public String toString() { |
|
return "OrderItem{" + |
|
"product=" + product + |
|
", quantity=" + quantity + |
|
'}'; |
|
} |
|
} |
|
} |
5. 电子商务系统类 (ECommerceSystem)
java复制代码
|
import java.util.ArrayList; |
|
import java.util.HashMap; |
|
import java.util.List; |
|
import java.util.Map; |
|
|
|
public class ECommerceSystem { |
|
private Map<String, User> users; // 用户名和用户的映射 |
|
private List<Product> products; // 商品列表 |
|
private Map<Integer, Order> orders; // 订单ID和订单的映射 |
|
|
|
public ECommerceSystem() { |
|
users = new HashMap<>(); |
|
products = new ArrayList<>(); |
|
orders = new HashMap<>(); |
|
} |
|
|
|
// 注册新用户 |
|
public void registerUser(String username, String password, String email) { |
|
User newUser = new User(username, password, email); |
|
users.put(username, newUser); |
|
} |
|
|
|
// 用户登录 |
|
public User login(String username, String password) { |
|
User user = users.get(username); |
|
if (user != null && user.getPassword().equals(password)) { |
|
return user; |
|
} |
|
return null; |
|
} |
|
|
|
// 浏览商品 |
|
public List<Product> browseProducts() { |
|
return products; |
|
} |
|
|
|
// 添加到购物车 |
|
public ShoppingCart addToCart(User user, Product product, int quantity) { |
|
ShoppingCart cart = new ShoppingCart(); |
|
cart.addItem(product, quantity); |
|
// 在实际应用中,应该返回用户已有的购物车,或者将商品添加到用户现有的购物车中 |
|
return |