在Java面向对象编程中,我们可以构建一个电子商务系统来模拟在线购物的过程。这个系统通常包括以下几个关键部分:
1. 用户类 (User):包含用户的基本信息,如用户名、密码、邮箱等。
2. 商品类 (Product):包含商品的详细信息,如商品名称、价格、描述、库存等。
3. 购物车类 (ShoppingCart):用于管理用户添加到购物车的商品和数量。
4. 订单类 (Order):表示用户提交的购买订单,包括商品列表、总价、收货地址等。
5. 电子商务系统类 (ECommerceSystem):用于管理用户、商品、购物车和订单,提供注册、登录、浏览商品、添加到购物车、提交订单等功能。
下面是这个电子商务系统的具体实现:
1. 用户类 (User)
java复制代码
|
public class User { |
|
private String username; |
|
private String password; |
|
private String email; |
|
|
|
public User(String username, String password, String email) { |
|
this.username = username; |
|
this.password = password; |
|
this.email = email; |
|
} |
|
|
|
// Getters and Setters |
|
public String getUsername() { |
|
return username; |
|
} |
|
|
|
public void setUsername(String username) { |
|
this.username = username; |
|
} |
|
|
|
public String getPassword() { |
|
return password; |
|
} |
|
|
|
public void setPassword(String password) { |
|
this.password = password; |
|
} |
|
|
|
public String getEmail() { |
|
return email; |
|
} |
|
|
|
public void setEmail(String email) { |
|
this.email = email; |
|
} |
|
|
|
// toString method |
|
@Override |
|
public String toString() { |
|
return "User{" + |
|
"username='" + username + '\'' + |
|
", email='" + email + '\'' + |
|
'}'; |
|
} |
|
} |
2. 商品类 (Product)
java复制代码
|
public class Product { |
|
private String name; |
|
private double price; |
|
private String description; |
|
private int stock; |
|
|
|
public Product(String name, double price, String description, int stock) { |
|
this.name = name; |
|
this.price = price; |
|
this.description = description; |
|
this.stock = stock; |
|
} |
|
|
|
// Getters and Setters |
|
public String getName() { |
|
return name; |
|
} |
|
|
|
public void setName(String name) { |
|
this.name = name; |
|
} |
|
|
|
public double getPrice() { |
|
return price; |
|
} |
|
|
|
public void setPrice(double price) { |
|
this.price = price; |
|
} |
|
|
|
public String getDescription() { |
|
return description; |
|
} |
|
|
|
public void setDescription(String description) { |
|
this.description = description; |
|
} |
|
|
|
public int getStock() { |
|
return stock; |
|
} |
|
|
|
public void setStock(int stock) { |
|
this.stock = stock; |
|
} |
|
|
|
// toString method |
|
@Override |
|
public String toString() { |
|
return "Product{" + |
|
"name='" + name + '\'' + |
|
", price=" + price + |
|
", description='" + description + '\'' + |
|
", stock=" + stock + |
|
'}'; |
|
} |
|
} |
3. 购物车类 (ShoppingCart)
java复制代码
|
import java.util.ArrayList; |
|
import java.util.HashMap; |
|
import java.util.List; |
|
import java.util.Map; |
|
|
|
public class ShoppingCart { |
|
private Map<Product, Integer> items; // 商品和数量的映射 |
|
|
|
public ShoppingCart() { |
|
items = new HashMap<>(); |
|
} |
|
|
|
public void addItem(Product product, int quantity) { |
|
if (items.containsKey(product)) { |
|
int currentQuantity = items.get(product); |
|
items.put(product, currentQuantity + quantity); |
|
} else { |
|
items.put(product, quantity); |
|
} |
|
} |
|
|
|
public void removeItem(Product product, int quantity) { |
|
if (items.containsKey(product)) { |
|
int currentQuantity = items.get(product); |
|
if (currentQuantity >= quantity) { |
|
items.put(product, currentQuantity - quantity); |
|
} else { |
|
items.remove(product); |
|
} |
|
} |
|
} |
|
|
|
public double getTotalPrice() { |
|
double |