Files
Java/com/study/demo13/OrderService.java
2026-01-21 14:35:47 +08:00

59 lines
1.5 KiB
Java

package com.study.demo13;
abstract class Order {
protected double amount;
public Order(double amount) {
this.amount = amount;
}
abstract double calculatePrice();
abstract String getOrderType();
}
class NormalOrder extends Order {
public NormalOrder(double amount) {
super(amount);
}
@Override
double calculatePrice() {
return amount;
}
@Override
String getOrderType() {
return "NORMAL";
}
}
class DiscountOrder extends Order {
public DiscountOrder(double amount) {
super(amount);
}
@Override
double calculatePrice() {
return amount * 0.8;
}
@Override
String getOrderType() {
return "DISCOUNT";
}
}
public class OrderService {
public static void main(String[] args) {
Order normal = new NormalOrder(100.0);
Order discount = new DiscountOrder(100.0);
Order normal2 = new NormalOrder(250.5);
Order discount2 = new DiscountOrder(300.0);
System.out.println("Order Type: " + normal.getOrderType() + ", Final Price: " + normal.calculatePrice());
System.out.println("Order Type: " + discount.getOrderType() + ", Final Price: " + discount.calculatePrice());
System.out.println("Order Type: " + normal2.getOrderType() + ", Final Price: " + normal2.calculatePrice());
System.out.println("Order Type: " + discount2.getOrderType() + ", Final Price: " + discount2.calculatePrice());
}
}