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();
}
}
网站2331沈欣怡32,JAVA,期末上机考试
于 2024-06-28 12:03:46 首次发布