public class Customer19 {
// 客户类
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;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
// 订单类
class Order {
private String orderNumber;
private double orderAmount;
private String orderStatus;
public Order(String orderNumber, double orderAmount) {
this.orderNumber = orderNumber;
this.orderAmount = orderAmount;
}
public double calculateTotalPrice(double quantity, double unitPrice) {
return quantity * unitPrice;
}
}
// 支付接口
interface Payment {
void makePayment(double amount);
}
// 信用卡支付类,实现支付接口
class CreditCardPayment implements Payment {
@Override
public void makePayment(double amount) {
System.out.println("使用信用卡支付了 " + amount + " 元");
}
}
// PayPal 支付类,实现支付接口
class PayPalPayment implements Payment {
@Override
public void makePayment(double amount) {
System.out.println("使用 PayPal 支付了 " + amount + " 元");
}
}
// 支付工厂类
class PaymentFactory {
public Payment createPayment(String paymentType) {
if ("credit card".equals(paymentType)) {
return new CreditCardPayment();
} else if ("paypal".equals(paymentType)) {
return new PayPalPayment();
} else {
throw new IllegalArgumentException("无效的支付类型");
}
}
}
public static void main(String[] args) {
Customer19 oss = new Customer19();
// 创建客户对象
Customer customer = oss.new Customer("张三", "地址 1", "123456789");
// 创建订单对象
Order order = oss.new Order("001", 100.0);
// 通过工厂创建支付对象
PaymentFactory paymentFactory = oss.new PaymentFactory();
Payment payment = paymentFactory.createPayment("credit card");
// 进行支付操作
payment.makePayment(order.calculateTotalPrice(2, 50.0));
}
}