update:demo13

This commit is contained in:
dichgrem
2026-01-21 14:35:47 +08:00
parent a7334f14dc
commit 441cb7d898
6 changed files with 392 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
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());
}
}