用Java实现设计商品类(Product class),包括商品属性和方法,如商品名称、价格、库存数量、加入购物车、从购物车移除等; 设计购物车类(ShoppingCart class),包括购物车属性和方法,如购物车列表、添加商品、移除商品、结算等; 设计用户类(User class),包括用户属性和方法,如用户名、密码、购物车、订单记录等; 设计订单类(Order class),包括订单属性和方法,如订单号、订单时间、订单详情(即购买的商品信息)等; 实现以上类及其方法,并编写主函数,模拟用户使用场景,如浏览商品,添加到购物车,结算下单等。
时间: 2023-07-10 09:29:02 浏览: 201
好的,以下是Java实现的代码:
Product类:
```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类:
```java
import java.util.ArrayList;
import java.util.List;
public class ShoppingCart {
private List<Product> productList = new ArrayList<>();
public void addProduct(Product product) {
if (product.getStock() > 0) {
productList.add(product);
product.setStock(product.getStock() - 1);
} else {
System.out.println("商品库存不足!");
}
}
public void removeProduct(Product product) {
productList.remove(product);
product.setStock(product.getStock() + 1);
}
public double getTotalPrice() {
double total = 0;
for (Product product : productList) {
total += product.getPrice();
}
return total;
}
public void checkout(User user) {
if (user.getShoppingCart() == this) {
if (user.getBalance() >= getTotalPrice()) {
Order order = new Order(user, productList);
user.addOrder(order);
user.setBalance(user.getBalance() - getTotalPrice());
productList.clear();
System.out.println("下单成功!");
} else {
System.out.println("余额不足!");
}
} else {
System.out.println("购物车与用户不匹配!");
}
}
}
```
User类:
```java
import java.util.ArrayList;
import java.util.List;
public class User {
private String username;
private String password;
private double balance;
private ShoppingCart shoppingCart = new ShoppingCart();
private List<Order> orderList = new ArrayList<>();
public User(String username, String password, double balance) {
this.username = username;
this.password = password;
this.balance = balance;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public ShoppingCart getShoppingCart() {
return shoppingCart;
}
public void addOrder(Order order) {
orderList.add(order);
}
}
```
Order类:
```java
import java.util.List;
public class Order {
private static int orderCount = 0;
private int orderId;
private User user;
private List<Product> productList;
public Order(User user, List<Product> productList) {
this.orderId = ++orderCount;
this.user = user;
this.productList = productList;
}
public int getOrderId() {
return orderId;
}
public User getUser() {
return user;
}
public List<Product> getProductList() {
return productList;
}
}
```
主函数:
```java
public class Main {
public static void main(String[] args) {
// 创建商品
Product product1 = new Product("商品1", 10, 5);
Product product2 = new Product("商品2", 20, 3);
Product product3 = new Product("商品3", 30, 2);
// 创建用户
User user1 = new User("用户1", "123456", 100);
// 用户1浏览商品
System.out.println("用户1正在浏览商品...");
System.out.println("商品名称\t商品价格\t商品库存");
System.out.println(product1.getName() + "\t" + product1.getPrice() + "\t" + product1.getStock());
System.out.println(product2.getName() + "\t" + product2.getPrice() + "\t" + product2.getStock());
System.out.println(product3.getName() + "\t" + product3.getPrice() + "\t" + product3.getStock());
// 用户1将商品1和商品2加入购物车
System.out.println("用户1将商品1和商品2加入购物车...");
product1.addToCart(user1.getShoppingCart());
product2.addToCart(user1.getShoppingCart());
// 用户1查看购物车并结算
System.out.println("用户1查看购物车并结算...");
System.out.println("购物车商品列表:");
for (Product product : user1.getShoppingCart().getProductList()) {
System.out.println(product.getName() + "\t" + product.getPrice());
}
System.out.println("总价:" + user1.getShoppingCart().getTotalPrice());
user1.getShoppingCart().checkout(user1);
// 用户1查看订单记录
System.out.println("用户1查看订单记录...");
System.out.println("订单号\t订单时间\t\t订单详情");
for (Order order : user1.getOrderList()) {
System.out.print(order.getOrderId() + "\t" + "2021-01-01 00:00:00" + "\t");
for (Product product : order.getProductList()) {
System.out.print(product.getName() + "\t");
}
System.out.println();
}
}
}
```
阅读全文