网站2331沈欣怡32,JAVA,期末上机考试

package tm1;

class Cat extends Animal32 {
    public Cat(String name, int age) {
        super(name, age);
    }

    @Override
    public void makeSound() {
        System.out.println("喵喵喵");
    }
}

package tm1;

class Dog extends Animal32 {
    public Dog(String name, int age) {
        super(name, age);
    }

    @Override
    public void makeSound() {
        System.out.println("汪汪汪");
    }
}

package tm1;

class Rabbit extends Animal32 {
    public Rabbit(String name, int age) {
        super(name, age);
    }

    @Override
    public void makeSound() {
        System.out.println("吱吱吱");
    }
}

package tm1;

public class AnimalFactory {
    public static Animal32 createAnimal(String type, String name, int age) {
        if (type.equalsIgnoreCase("cat")) {
            return new Cat(name, age);
        } else if (type.equalsIgnoreCase("dog")) {
            return new Dog(name, age);
        } else if (type.equalsIgnoreCase("rabbit")) {
            return new Rabbit(name, age);
        } else {
            throw new IllegalArgumentException("Invalid animal type");
        }
    }
}

package tm1;

public class Main {
    public static void main(String[] args) {
        Animal32 cat = AnimalFactory.createAnimal("cat", "小花", 2);
        Animal32 dog = AnimalFactory.createAnimal("dog", "大黄", 3);
        Animal32 rabbit = AnimalFactory.createAnimal("rabbit", "小白", 1);

        cat.makeSound(); 
        dog.makeSound();
        rabbit.makeSound(); 
    }
}









package tm2;

public class Order {
    private int orderId;
    private double totalPrice;
    private String status;
public double calculateTotalPrice(int quantity, double unitPrice) {
        return quantity * unitPrice;
    }
    public int getOrderId() { return orderId; }
    public void setOrderId(int orderId) { this.orderId = orderId; }
    public double getTotalPrice() { return totalPrice; }
    public void setTotalPrice(double totalPrice) { this.totalPrice = totalPrice; }
    public String getStatus() { return status; }
    public void setStatus(String status) { this.status = status; }
}

package tm2;

public class PaymentFactory {
    public static Payment createPayment(String paymentType) {
        if (paymentType.equals("credit_card")) {
            return new CreditCardPayment();
        } else if (paymentType.equals("paypal")) {
            return new PayPalPayment();
        } else {
            throw new IllegalArgumentException("不支持的支付方式");
        }
    }
}

package tm2;

public class CreditCardPayment implements Payment {
    @Override
    public void makePayment() {
        System.out.println("使用信用卡支付");
    }
}

package tm2;

public class PayPalPayment implements Payment {
    @Override
    public void makePayment() {
        System.out.println("使用PayPal支付");
    }
}

package tm2;

public class Main {
    public static void main(String[] args) {
        Customer32 customer = new Customer32("John Doe", "123 Main St", "555-1234");
        Order order = new Order();

        int quantity = 10;
        double unitPrice = 9.99;
        double totalPrice = order.calculateTotalPrice(quantity, unitPrice);
        order.setTotalPrice(totalPrice);

        String paymentType = "credit_card";
        Payment payment = PaymentFactory.createPayment(paymentType);
        payment.makePayment();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值