用java写一个电商程序的订单模块
时间: 2024-05-05 12:17:29 浏览: 17
以下是一个简单的电商程序订单模块的Java代码:
```
public class Order {
private int orderId;
private String customerName;
private String customerAddress;
private String customerEmail;
private ArrayList<Product> productList;
public Order(int orderId, String customerName, String customerAddress, String customerEmail, ArrayList<Product> productList) {
this.orderId = orderId;
this.customerName = customerName;
this.customerAddress = customerAddress;
this.customerEmail = customerEmail;
this.productList = productList;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getCustomerAddress() {
return customerAddress;
}
public void setCustomerAddress(String customerAddress) {
this.customerAddress = customerAddress;
}
public String getCustomerEmail() {
return customerEmail;
}
public void setCustomerEmail(String customerEmail) {
this.customerEmail = customerEmail;
}
public ArrayList<Product> getProductList() {
return productList;
}
public void setProductList(ArrayList<Product> productList) {
this.productList = productList;
}
public double getTotalCost() {
double totalCost = 0;
for (Product product : productList) {
totalCost += product.getPrice();
}
return totalCost;
}
}
public class Product {
private int productId;
private String productName;
private double price;
public Product(int productId, String productName, double price) {
this.productId = productId;
this.productName = productName;
this.price = price;
}
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
public class OrderModule {
private ArrayList<Order> orderList;
public OrderModule() {
orderList = new ArrayList<Order>();
}
public void addOrder(Order order) {
orderList.add(order);
}
public void removeOrder(Order order) {
orderList.remove(order);
}
public ArrayList<Order> getOrderList() {
return orderList;
}
public void printAllOrders() {
for (Order order : orderList) {
System.out.println("Order ID: " + order.getOrderId());
System.out.println("Customer Name: " + order.getCustomerName());
System.out.println("Customer Address: " + order.getCustomerAddress());
System.out.println("Customer Email: " + order.getCustomerEmail());
System.out.println("Product List: ");
for (Product product : order.getProductList()) {
System.out.println("Product ID: " + product.getProductId());
System.out.println("Product Name: " + product.getProductName());
System.out.println("Product Price: " + product.getPrice());
}
System.out.println("Total Cost: " + order.getTotalCost());
System.out.println("--------------------");
}
}
}
```
在这个代码中,Order类表示一个订单,包含订单ID、顾客姓名、顾客地址、顾客电子邮件和产品列表。Product类表示一个产品,包含产品ID、产品名称和产品价格。OrderModule类实现了管理订单的方法,包括添加订单、删除订单和打印所有订单的方法。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)