用Java实现设计商品类(Product class),包括商品属性和方法,如商品名称、价格、库存数量、加入购物车、从购物车移除等; 设计购物车类(ShoppingCart class),包括购物车属性和方法,如购物车列表、添加商品、移除商品、结算等; 设计用户类(User class),包括用户属性和方法,如用户名、密码、购物车、订单记录等; 设计订单类(Order class),包括订单属性和方法,如订单号、订单时间、订单详情(即购买的商品信息)等; 实现以上类及其方法,并编写主函数,模拟用户使用场景,如浏览商品,添加到购物车,结算下单等。
时间: 2023-07-10 14:29:12 浏览: 112
用java实现购物车
好的,以下是使用Java实现的商品类(Product class):
```java
public class Product {
private String name;
private double price;
private int stock;
public Product(String name, double price, int stock) {
this.name = name;
this.price = price;
this.stock = stock;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public void addToCart(ShoppingCart cart) {
cart.addProduct(this);
}
public void removeFromCart(ShoppingCart cart) {
cart.removeProduct(this);
}
}
```
购物车类(ShoppingCart class):
```java
import java.util.ArrayList;
import java.util.List;
public class ShoppingCart {
private List<Product> productList;
public ShoppingCart() {
productList = new ArrayList<>();
}
public List<Product> getProductList() {
return productList;
}
public void addProduct(Product product) {
productList.add(product);
}
public void removeProduct(Product product) {
productList.remove(product);
}
public double calculateTotalPrice() {
double totalPrice = 0;
for (Product product : productList) {
totalPrice += product.getPrice();
}
return totalPrice;
}
}
```
用户类(User class):
```java
public class User {
private String username;
private String password;
private ShoppingCart cart;
private List<Order> orderList;
public User(String username, String password) {
this.username = username;
this.password = password;
cart = new ShoppingCart();
orderList = new ArrayList<>();
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public ShoppingCart getCart() {
return cart;
}
public List<Order> getOrderList() {
return orderList;
}
public void addToCart(Product product) {
product.addToCart(cart);
}
public void removeFromCart(Product product) {
product.removeFromCart(cart);
}
public void checkout() {
Order order = new Order(cart);
orderList.add(order);
cart = new ShoppingCart();
}
}
```
订单类(Order class):
```java
import java.time.LocalDateTime;
import java.util.List;
public class Order {
private static int count = 0;
private int orderId;
private LocalDateTime orderTime;
private List<Product> productList;
private double totalPrice;
public Order(ShoppingCart cart) {
orderId = ++count;
orderTime = LocalDateTime.now();
productList = cart.getProductList();
totalPrice = cart.calculateTotalPrice();
}
public int getOrderId() {
return orderId;
}
public LocalDateTime getOrderTime() {
return orderTime;
}
public List<Product> getProductList() {
return productList;
}
public double getTotalPrice() {
return totalPrice;
}
}
```
主函数:
```java
public class Main {
public static void main(String[] args) {
// 创建商品
Product product1 = new Product("商品1", 10.0, 5);
Product product2 = new Product("商品2", 20.0, 3);
// 创建用户
User user = new User("用户名", "密码");
// 用户浏览商品
System.out.println("用户浏览商品:");
System.out.println(product1.getName() + ",价格:" + product1.getPrice() + ",库存:" + product1.getStock());
System.out.println(product2.getName() + ",价格:" + product2.getPrice() + ",库存:" + product2.getStock());
// 用户添加商品到购物车
System.out.println("用户添加商品到购物车:");
user.addToCart(product1);
user.addToCart(product2);
// 用户从购物车移除商品
System.out.println("用户从购物车移除商品:");
user.removeFromCart(product1);
// 用户结算
System.out.println("用户结算:");
user.checkout();
// 用户查看订单记录
System.out.println("用户查看订单记录:");
List<Order> orderList = user.getOrderList();
for (Order order : orderList) {
System.out.println("订单号:" + order.getOrderId() + ",下单时间:" + order.getOrderTime() + ",商品列表:");
List<Product> productList = order.getProductList();
for (Product product : productList) {
System.out.println(product.getName() + ",价格:" + product.getPrice());
}
System.out.println("总价:" + order.getTotalPrice());
}
}
}
```
阅读全文