設計說明:
1. 該冰店提供A與B兩類套餐產品。
2. A套餐可任點兩種原料,B套餐可任點三種原料。
3. 每種類原料的成本與售價如下表:
類別名稱
|
Cost(成本)
|
Price(售價)
|
Apple
|
6.0
|
10.0
|
Banana
|
2.0
|
5.0
|
Pudding
|
3.0
|
5.0
|
Strawberry
|
1.0
|
5.0
|
Mango
|
2.0
|
5.0
|
參考程式碼:
TQC+ Java 試題總整理
- class Unit{
- double getCost,getPrice,getProfit;
- Unit(){getCost = 0; getPrice = 0; getProfit = 0;}
- public double getCost(){return getCost;}
- public double getPrice(){return getPrice;}
- public double getProfit(){return getProfit;}
- }
- class Apple extends Unit{
- Apple(){
- getCost=6;
- getPrice=10;
- }
- }
- class Banana extends Unit{
- Banana(){
- getCost=2;
- getPrice=5;
- }
- }
- class Pudding extends Unit{
- Pudding(){
- getCost=3;
- getPrice=5;
- }
- }
- class Strawberry extends Unit{
- Strawberry(){
- getCost=1;
- getPrice=5;
- }
- }
- class Mango extends Unit{
- Mango(){
- getCost=2;
- getPrice=5;
- }
- }
- class A extends Unit{
- A(Unit A,Unit B){
- getCost=A.getCost()+B.getCost();
- getPrice=A.getPrice()+B.getPrice();
- getProfit=getPrice-getCost;
- }
- }
- class B extends Unit{
- B(Unit A,Unit B,Unit C){
- getCost=A.getCost()+B.getCost()+C.getCost();
- getPrice=A.getPrice()+B.getPrice()+C.getPrice();
- getProfit=getPrice-getCost;
- }
- }
- public class JPA603_2{
- public static void main(String args[]){
- A t1 = new A(new Apple(), new Banana());
- B t2 = new B(new Banana(), new Pudding(), new Strawberry());
- B t3 = new B(new Apple(), new Banana(), new Mango());
- System.out.println("t1 price:" + t1.getPrice());
- System.out.println("t1 profit:" + t1.getProfit());
- System.out.println("t2 price:" + t2.getPrice());
- System.out.println("t2 profit:" + t2.getProfit());
- System.out.println("t3 price:" + t3.getPrice());
- System.out.println("t3 profit:" + t3.getProfit());
- }
- }
TQC+ Java 試題總整理
聲明:
這裡的範例程式碼皆由本人親自編輯,歡迎轉載本教學,但請註明本網站,尊重一下作者的心血
沒有留言:
張貼留言
歡迎留言,較舊文章需要留言審核看不到自己的留言是正常的。
若長時間無回應請使用以下聯絡方式:
填寫表單:https://forms.gle/hxxX9n4tATcFnhnk8
寄信到:happyplayblogs@gmail.com