package bushi;
//Customer 类
class Customer {
private String name;
private String address;
private String phoneNumber;
// 构造函数
public Customer(String name, String address, String phoneNumber) {
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
}
// Getter 和 Setter 方法
// ...
// 示例 toString 方法
@Override
public String toString() {
return "Customer{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
", phoneNumber='" + phoneNumber + '\'' +
'}';
}
}
//Order 类
class Order {
private String orderId;
private double totalPrice;
private String status; // 例如: "pending", "paid", "shipped"
// 构造函数
public Order(String orderId) {
this.orderId = orderId;
this.status = "pending"; // 初始状态为待处理
}
// Getter 和 Setter 方法
// ...
// 计算总价的方法
public void calculateTotalPrice(double unitPrice, int quantity) {
this.totalPrice = unitPrice * quantity;
}
// 示例 toString 方法
@Override
public String toString() {
return "Order{" +
"orderId='" + orderId + '\'' +
", totalPrice=" + totalPrice +
", status='" + status + '\'' +
'}';
}
}
//Payment 接口
interface Payment {
void makePayment(Order order);
}
//CreditCardPayment 类
class CreditCardPayment implements Payment {
@Override
public void makePayment(Order order) {
System.out.println("Payment by Credit Card: Order " + order.getOrderId() + " has been paid.");
// 这里可以添加信用卡支付的具体逻辑
order.setStatus("paid"); // 假设支付成功,更新订单状态
}
}
//PayPalPayment 类
class PayPalPayment implements Payment {
@Override
public void makePayment(Order order) {
System.out.println("Payment by PayPal: Order " + order.getOrderId() + " has been paid.");
// 这里可以添加PayPal支付的具体逻辑
order.setStatus("paid"); // 假设支付成功,更新订单状态
}
}
//PaymentFactory 类
class PaymentFactory {
public static Payment createPayment(String paymentType) {
if ("credit_card".equals(paymentType)) {
return new CreditCardPayment();
} else if ("paypal".equals(paymentType)) {
return new PayPalPayment();
} else {
throw new IllegalArgumentException("Invalid payment type: " + paymentType);
}
}
}
//主函数
public class mad {
public static void main(String[] args) {
// 创建客户对象
Customer customer = new Customer("John Doe", "123 Street, City", "1234567890");
// 创建订单对象
Order order = new Order("ORD12345");
order.calculateTotalPrice(100.0, 2); // 假设商品单价为100,数量为2
// 选择支付方式进行支付
Payment payment = PaymentFactory.createPayment("credit_card"); // 假设选择信用卡支付
payment.makePayment(order);
// 打印订单信息
System.out.println(order);
}
}
第二题00
最新推荐文章于 2024-08-16 23:08:28 发布