import java.util.Scanner;
// 客户类
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 and Setter methods
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 int orderId;
private double totalPrice;
private String orderStatus;
// 这里简化处理,没有直接关联商品和价格计算逻辑,只示范结构
public double calculateTotalPrice(int quantity, double unitPrice) {
return totalPrice = quantity * unitPrice;
}
public double getTotalPrice() {
// TODO Auto-generated method stub
return 0;
}
// 其他getter和setter省略...
}
// 支付接口
interface Payment {
void makePayment(double amount);
}
// 信用卡支付类
class CreditCardPayment implements Payment {
@Override
public void makePayment(double amount) {
System.out.println("Processing payment of $" + amount + " via credit card.");
}
}
// PayPal支付类
class PayPalPayment implements Payment {
@Override
public void makePayment(double amount) {
System.out.println("Processing payment of $" + amount + " via PayPal.");
}
}
// 支付简单工厂类
class PaymentFactory {
public static Payment createPayment(String paymentMethod) {
if ("credit_card".equalsIgnoreCase(paymentMethod)) {
return new CreditCardPayment();
} else if ("paypal".equalsIgnoreCase(paymentMethod)) {
return new PayPalPayment();
} else {
throw new IllegalArgumentException("Invalid payment method.");
}
}
}
public class OnlineShoppingSystem18 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("客户名称:");
String customerName = scanner.nextLine();
String customerAddress = scanner.nextLine();
String phoneNumber = scanner.nextLine();
Customer customer = new Customer(customerName, customerAddress, phoneNumber);
System.out.println("创建订单...");
Order order = new Order();
// 示例:假设的商品数量和单价,实际应用中应从UI或数据库获取
order.calculateTotalPrice(2, 50); // 假设订购了2件商品,每件50元
System.out.println("支付方式 [微信, 支付宝]:");
String chosenPaymentMethod = scanner.nextLine().toLowerCase();
Payment payment = PaymentFactory.createPayment(chosenPaymentMethod);
payment.makePayment(order.getTotalPrice()); // 假定getTotalPrice已正确实现获取总价
scanner.close();
}
}
期末作业二
最新推荐文章于 2024-09-27 00:45:01 发布