mirror of
https://github.com/Dichgrem/Java.git
synced 2026-02-05 02:51:57 -05:00
update:demo13
This commit is contained in:
58
com/study/demo13/OrderService.java
Normal file
58
com/study/demo13/OrderService.java
Normal 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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user