package w1;
public class Example2 {
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 {
public void makePayment(double amount) {
System.out.println("用信用卡付款了 " + amount + " 人民币");
}
}
class PayPalPayment implements Payment {
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) {
Example2 oss = new Example2();
Customer customer = oss.new Customer("林志峰", "地址 1", "123456789");
Order order = oss.new Order("17", 100.0);
PaymentFactory paymentFactory = oss.new PaymentFactory();
Payment payment = paymentFactory.createPayment("credit card");
payment.makePayment(order.calculateTotalPrice(10,95740));
}
}
【无标题】
最新推荐文章于 2024-07-29 18:00:00 发布