TQC+ 物件導向程式語言 Java 6 603 冰品計價系統
題目前後相關,請閱讀完這個題組的五個題目之後再作答,每題答案皆能獨立執行。請將需要重複或共同使用的程式片段撰寫成函式,以供在類別中呼叫使用,避免在不同的類別中重複寫相同的程式碼,否則將酌量扣分。
題目一:
題目說明:
請開啟檔案 JPD06_1.java,設計「冰品計價設計」程式。避免在不同的類別中重複寫相同的程式碼,請依下列題意完成作答。將 JPD06_1.java 內的 class JPD06_1 修改為 class JPA06_1,將檔案另存為 JPA06_1.java 後編譯為 JPA06_1.class,所有題目中有使用到的類別也請編譯後一併儲存。
設計說明:
(1) 請為冰品的每個原料 Apple (蘋果)、Banana (香蕉)、Pudding (布丁)、Strawberry (草莓)、 Mango (芒果) 設計類別及繼承的架構,並提供 getCost (成本) 及 getPrice (售價) 二個方法。
(2) 每種類原料的成本與售價如下表:
class name (類別名稱) | 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 |
(3) 請各產生一個 Apple、Banana、Pudding 之物件,並印出它們的 cost 及 price。
執行結果參考畫面:
程式碼:
class Fruit {
double cost, price;
Fruit(double cost, double price) {
this.cost = cost;
this.price = price;
}
double getCost() {
return cost;
}
double getPrice() {
return price;
}
}
class Apple extends Fruit {
Apple() {
super(6.0, 10.0);
}
}
class Banana extends Fruit {
Banana() {
super(2.0, 5.0);
}
}
class Pudding extends Fruit {
Pudding() {
super(3.0, 5.0);
}
}
class Strawberry extends Fruit {
Strawberry() {
super(1.0, 5.0);
}
}
class Mango extends Fruit {
Mango() {
super(2.0, 5.0);
}
}
class JPA06_1 {
public static void main(String args[]) {
Apple ab = new Apple();
Banana bb = new Banana();
Pudding pt = new Pudding();
System.out.println("Apple cost:" + ab.getCost());
System.out.println("Apple price:" + ab.getPrice());
System.out.println("Banana cost:" + bb.getCost());
System.out.println("Banana price:" + bb.getPrice());
System.out.println("Pudding cost:" + pt.getCost());
System.out.println("Pudding price:" + pt.getPrice());
}
}
題目二:
題目說明:
請開啟 JPD06_2.java,該冰店提供 A 套餐與 B 套餐兩類產品可供客人選擇配料;請依下列題意完成作答。將 JPD06_2.java 内的 class JPD06_2 修改為 class JPA06_2,將檔案另存為 JPA06_2.java 後編譯為 JPA06_2.class,所有題目中有使用到的類別也請編譯後一併儲存。
設計說明:
(1) 該冰店提供 A 與 B 兩類套餐產品。
(2) A 套餐可任點兩種原料,B 套餐可任點三種原料。
(3) 每種類原料的成本與售價如下表:
class name (類別名稱) | 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 |
(4) 某客人點了以下的產品,請利用 getPrice (價格) 函式取得 price,利用getProfit (利潤) 取得 profit,並印出以下三種產品的 price 及 profit。
利潤 = 總收入 – 總成本。
a. A tl = new A(new Apple(), new Banana());
b. B t2 = new B(new Banana(), new Pudding(), new Strawberry());
C. B t3 = new B(new Apple(), new Banana(), new Mango());
執行結果參考畫面:
程式碼:
class Fruit {
double cost, price;
Fruit(double cost, double price) {
this.cost = cost;
this.price = price;
}
double getCost() {
return cost;
}
double getPrice() {
return price;
}
}
class Apple extends Fruit {
Apple() {
super(6.0, 10.0);
}
}
class Banana extends Fruit {
Banana() {
super(2.0, 5.0);
}
}
class Pudding extends Fruit {
Pudding() {
super(3.0, 5.0);
}
}
class Strawberry extends Fruit {
Strawberry() {
super(1.0, 5.0);
}
}
class Mango extends Fruit {
Mango() {
super(2.0, 5.0);
}
}
abstract class Combo {
Fruit f1, f2, f3;
Combo(Fruit f1, Fruit f2) {
this.f1 = f1;
this.f2 = f2;
}
Combo(Fruit f1, Fruit f2, Fruit f3) {
this.f1 = f1;
this.f2 = f2;
this.f3 = f3;
}
abstract double getCost(); // 取得總成本
abstract double getPrice(); // 取得總售價
double getProfit() {
return getPrice() - getCost();
}
}
class A extends Combo {
A(Fruit f1, Fruit f2) {
super(f1, f2);
}
double getCost() {
return f1.getCost() + f2.getCost();
}
double getPrice() {
return f1.getPrice() + f2.getPrice();
}
}
class B extends Combo {
B(Fruit f1, Fruit f2, Fruit f3) {
super(f1, f2, f3);
}
double getCost() {
return f1.getCost() + f2.getCost() + f3.getCost();
}
double getPrice() {
return f1.getPrice() + f2.getPrice() + f3.getPrice();
}
}
class JPA06_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());
}
}
題目三:
題目說明:
請開啟 JPD06_3.java,該冰店另提供 C 套餐與 D 套餐兩類產品,此兩款是除了可分別選擇兩種料及三種料外,「冰」的份量都加倍,請依下列題意完成作答。將 JPD06_3.java 内的 class JPD06_3 修改為 class JPA06_3,將檔案另存為 JPA06_3.java 後編譯為 JPA06_3.class,所有題目中有使用到的類別也請編譯後一併儲存。
設計說明:
(1) 該冰店另提供 C 套餐與 D 套餐兩類產品。C 套餐可任點兩種原料,冰的份量加倍;D 套餐可任點三種原料,冰的份量加倍。
(2) 每種類原料的成本與售價如下表:
class name (類別名稱) | 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 |
(3) C 套餐與 D 套餐的冰加倍,成本一律增加 2 元,銷售的價格則訂為「原料總售價」的 1.5 倍。
- 例如 C套餐 選擇 Apple 及 Mango,成本為 6.0 + 2.0 + 2 = 10 元
售價為 (10.0 + 5.0) * 1.5 = 22.5 元
(4) 某客人點了以下的產品,請取得下列三種冰加倍的產品,分別印出其 cost (成本)、price (價格) 及 profit (利潤)。
利潤 = 總收入 – 總成本。
a. C tl = new Double C(new Apple(), new Banana());
b. D t2 = new Double D(new Banana(), new Pudding(), new Strawberry());
C. D t3 = new Double D(new Apple(), new Banana(), new Mango());
執行結果參考畫面:
程式碼:
class Fruit {
double cost, price;
Fruit(double cost, double price) {
this.cost = cost;
this.price = price;
}
double getCost() {
return cost;
}
double getPrice() {
return price;
}
}
class Apple extends Fruit {
Apple() {
super(6.0, 10.0);
}
}
class Banana extends Fruit {
Banana() {
super(2.0, 5.0);
}
}
class Pudding extends Fruit {
Pudding() {
super(3.0, 5.0);
}
}
class Strawberry extends Fruit {
Strawberry() {
super(1.0, 5.0);
}
}
class Mango extends Fruit {
Mango() {
super(2.0, 5.0);
}
}
abstract class Combo {
Fruit f1, f2, f3;
Combo(Fruit f1, Fruit f2) {
this.f1 = f1;
this.f2 = f2;
}
Combo(Fruit f1, Fruit f2, Fruit f3) {
this.f1 = f1;
this.f2 = f2;
this.f3 = f3;
}
abstract double getCost(); // 取得總成本
abstract double getPrice(); // 取得總售價
double getProfit() {
return getPrice() - getCost();
}
}
class A extends Combo {
A(Fruit f1, Fruit f2) {
super(f1, f2);
}
double getCost() {
return f1.getCost() + f2.getCost();
}
double getPrice() {
return f1.getPrice() + f2.getPrice();
}
}
class B extends Combo {
B(Fruit f1, Fruit f2, Fruit f3) {
super(f1, f2, f3);
}
double getCost() {
return f1.getCost() + f2.getCost() + f3.getCost();
}
double getPrice() {
return f1.getPrice() + f2.getPrice() + f3.getPrice();
}
}
class C extends Combo {
C(Fruit f1, Fruit f2) {
super(f1, f2);
}
double getCost() {
return f1.getCost() + f2.getCost() + 2;
}
double getPrice() {
return (f1.getPrice() + f2.getPrice()) * 1.5;
}
}
class D extends Combo {
D(Fruit f1, Fruit f2, Fruit f3) {
super(f1, f2, f3);
}
double getCost() {
return f1.getCost() + f2.getCost() + f3.getCost() + 2;
}
double getPrice() {
return (f1.getPrice() + f2.getPrice() + f3.getPrice()) * 1.5;
}
}
class JPA06_3 {
public static void main(String args[]) {
C t1 = new C (new Apple(), new Banana());
D t2 = new D (new Banana(), new Pudding(), new Strawberry());
D t3 = new D (new Apple(), new Banana(), new Mango());
System.out.println("t1 cost:" + t1.getCost());
System.out.println("t1 price:" + t1.getPrice());
System.out.println("t1 profit:" + t1.getProfit());
System.out.println("t2 cost:" + t2.getCost());
System.out.println("t2 price:" + t2.getPrice());
System.out.println("t2 profit:" + t2.getProfit());
System.out.println("t3 cost:" + t3.getCost());
System.out.println("t3 price:" + t3.getPrice());
System.out.println("t3 profit:" + t3.getProfit());
}
}
題目四:
題目說明:
請開啟 JPD06_4.java,該店為了擴展商機,新增外送服務,請依下列題意完成作答。將 JPD06_4.java 内的 class JPD06_4 修改為 class JPA06_4,將檔案另存為 JPA06_4.java 後編譯為 JPA06_4.class,所有題目中有使用到的類別也請編譯後一併儲存。
設計說明:
(1) 請使用 LinkedList,另外寫一個類別支援該店的外送服務。
(2) 外送服務可以計算一次外送服務的總售價、總成本及總利潤。
(3) 請為以下兩次的外送,分別計算其總售價、總成本及總利潤。
a. 外送 1 (冰品皆為一般,無加倍)
a-1 : Apple、Banana
a-2 : Banana、Pudding、Strawberry
b. 外送 2 (冰品皆為一般,無加倍)
b-1 : Apple、Banana、Mango
b-2 : Apple、Banana
b-3: Banana、Pudding、Strawberry
b-4 : Apple、Banana、Mango
執行結果參考畫面:
程式碼:
import java.util.*;
class Fruit {
double cost, price;
Fruit(double cost, double price) {
this.cost = cost;
this.price = price;
}
double getCost() {
return cost;
}
double getPrice() {
return price;
}
}
class Apple extends Fruit {
Apple() {
super(6.0, 10.0);
}
}
class Banana extends Fruit {
Banana() {
super(2.0, 5.0);
}
}
class Pudding extends Fruit {
Pudding() {
super(3.0, 5.0);
}
}
class Strawberry extends Fruit {
Strawberry() {
super(1.0, 5.0);
}
}
class Mango extends Fruit {
Mango() {
super(2.0, 5.0);
}
}
abstract class Combo {
Fruit f1, f2, f3;
Combo(Fruit f1, Fruit f2) {
this.f1 = f1;
this.f2 = f2;
}
Combo(Fruit f1, Fruit f2, Fruit f3) {
this.f1 = f1;
this.f2 = f2;
this.f3 = f3;
}
abstract double getCost(); // 取得總成本
abstract double getPrice(); // 取得總售價
double getProfit() {
return getPrice() - getCost();
}
}
class A extends Combo {
A(Fruit f1, Fruit f2) {
super(f1, f2);
}
double getCost() {
return f1.getCost() + f2.getCost();
}
double getPrice() {
return f1.getPrice() + f2.getPrice();
}
}
class B extends Combo {
B(Fruit f1, Fruit f2, Fruit f3) {
super(f1, f2, f3);
}
double getCost() {
return f1.getCost() + f2.getCost() + f3.getCost();
}
double getPrice() {
return f1.getPrice() + f2.getPrice() + f3.getPrice();
}
}
class C extends Combo {
C(Fruit f1, Fruit f2) {
super(f1, f2);
}
double getCost() {
return f1.getCost() + f2.getCost() + 2;
}
double getPrice() {
return (f1.getPrice() + f2.getPrice()) * 1.5;
}
}
class D extends Combo {
D(Fruit f1, Fruit f2, Fruit f3) {
super(f1, f2, f3);
}
double getCost() {
return f1.getCost() + f2.getCost() + f3.getCost() + 2;
}
double getPrice() {
return (f1.getPrice() + f2.getPrice() + f3.getPrice()) * 1.5;
}
}
class Deliver {
LinkedList<Combo> list;
Deliver() {
list = new LinkedList<Combo>();
}
void addProduct(Combo combo) {
list.add(combo);
}
double getTotalPrice() {
double totalprice = 0;
for(Combo combo : list)
totalprice += combo.getPrice();
return totalprice;
}
double getTotalCost() {
double totalcost = 0;
for(Combo combo : list)
totalcost += combo.getCost();
return totalcost;
}
double getTotalProfit() {
return getTotalPrice() - getTotalCost();
}
}
class JPA06_4 {
public static void main(String args[]){
Deliver d1 = new Deliver();
d1.addProduct(new A(new Apple(), new Banana()));
d1.addProduct(new B(new Banana(), new Pudding(), new Strawberry()));
System.out.println("a Price: " + d1.getTotalPrice());
System.out.println("a Cost: " + d1.getTotalCost());
System.out.println("a Profit: " + d1.getTotalProfit());
Deliver d2 = new Deliver();
d2.addProduct(new B(new Apple(), new Banana(), new Mango()));
d2.addProduct(new A(new Apple(), new Banana()));
d2.addProduct(new B(new Banana(), new Pudding(), new Strawberry()));
d2.addProduct(new B(new Apple(), new Banana(), new Mango()));
System.out.println("b Price: " + d2.getTotalPrice());
System.out.println("b Cost: " + d2.getTotalCost());
System.out.println("b Profit: " + d2.getTotalProfit());
}
}
題目五:
題目說明:
請開啟 JPD06_5.java,接續題目四的外送服務,請依下列題意完成作答。將 JPD06_5.java 内的 class JPD06_5 修改為 class JPA06_5,將檔案另存為 JPA06_5.java 後編譯為 JPA06_5.class,所有題目中有使用到的類別也請編譯後一併儲存。
設計說明:
(1) 請使用 LinkedList,另外寫一個類別支援該店的外送服務。
(2) 外送服務可以計算一次外送服務的總售價、總成本及總利潤。
(3) 請為以下兩次的外送,分別計算其總售價、總成本及總利潤。
a. 外送 1 (冰品皆為一般,無加倍)
a-1 : Apple、Banana
a-2 : Banana、Pudding、Strawberry
a-3 : Banana、Pudding、Strawberry
a-4 : Apple、Banana、Mango
b. 外送 2 (冰品皆為一般,無加倍)
b-1 : Apple、Banana、Mango
b-2 : Apple、Banana
(4) 請使用自定例外類別處理,將總額不到 50 元的外送,以 Exception 印出錯誤信息【Not enough order for carry out:】及目前的外送金額。
執行結果參考畫面:
程式碼:
import java.util.*;
class Fruit {
double cost, price;
Fruit(double cost, double price) {
this.cost = cost;
this.price = price;
}
double getCost() {
return cost;
}
double getPrice() {
return price;
}
}
class Apple extends Fruit {
Apple() {
super(6.0, 10.0);
}
}
class Banana extends Fruit {
Banana() {
super(2.0, 5.0);
}
}
class Pudding extends Fruit {
Pudding() {
super(3.0, 5.0);
}
}
class Strawberry extends Fruit {
Strawberry() {
super(1.0, 5.0);
}
}
class Mango extends Fruit {
Mango() {
super(2.0, 5.0);
}
}
abstract class Combo {
Fruit f1, f2, f3;
Combo(Fruit f1, Fruit f2) {
this.f1 = f1;
this.f2 = f2;
}
Combo(Fruit f1, Fruit f2, Fruit f3) {
this.f1 = f1;
this.f2 = f2;
this.f3 = f3;
}
abstract double getCost(); // 取得總成本
abstract double getPrice(); // 取得總售價
double getProfit() {
return getPrice() - getCost();
}
}
class A extends Combo {
A(Fruit f1, Fruit f2) {
super(f1, f2);
}
double getCost() {
return f1.getCost() + f2.getCost();
}
double getPrice() {
return f1.getPrice() + f2.getPrice();
}
}
class B extends Combo {
B(Fruit f1, Fruit f2, Fruit f3) {
super(f1, f2, f3);
}
double getCost() {
return f1.getCost() + f2.getCost() + f3.getCost();
}
double getPrice() {
return f1.getPrice() + f2.getPrice() + f3.getPrice();
}
}
class C extends Combo {
C(Fruit f1, Fruit f2) {
super(f1, f2);
}
double getCost() {
return f1.getCost() + f2.getCost() + 2;
}
double getPrice() {
return (f1.getPrice() + f2.getPrice()) * 1.5;
}
}
class D extends Combo {
D(Fruit f1, Fruit f2, Fruit f3) {
super(f1, f2, f3);
}
double getCost() {
return f1.getCost() + f2.getCost() + f3.getCost() + 2;
}
double getPrice() {
return (f1.getPrice() + f2.getPrice() + f3.getPrice()) * 1.5;
}
}
class Deliver {
LinkedList<Combo> list;
Deliver() {
list = new LinkedList<Combo>();
}
void addProduct(Combo combo) {
list.add(combo);
}
double getTotalPrice() {
double totalprice = 0;
for(Combo combo : list)
totalprice += combo.getPrice();
return totalprice;
}
double getTotalCost() {
double totalcost = 0;
for(Combo combo : list)
totalcost += combo.getCost();
return totalcost;
}
double getTotalProfit() {
return getTotalPrice() - getTotalCost();
}
void checkOut() {
if(getTotalPrice() < 50)
throw new PriceNotEnoughException(getTotalPrice());
}
}
class PriceNotEnoughException extends RuntimeException { // 自訂的例外類別一定要是 Throwable 的衍生類別
PriceNotEnoughException(double totalprice) {
System.out.println("Not enough order for carry out: " + totalprice);
}
}
class JPA06_5 {
public static void main(String args[]) {
try {
Deliver d1 = new Deliver();
d1.addProduct(new A(new Apple(), new Banana()));
d1.addProduct(new B(new Banana(), new Pudding(), new Strawberry()));
d1.addProduct(new B(new Banana(), new Pudding(), new Strawberry()));
d1.addProduct(new B(new Apple(), new Banana(), new Mango()));
System.out.println("a Price: " + d1.getTotalPrice());
System.out.println("a Cost: " + d1.getTotalCost());
System.out.println("a Profit: " + d1.getTotalProfit());
System.out.println("");
d1.checkOut();
Deliver d2 = new Deliver();
d2.addProduct(new B(new Apple(), new Banana(), new Mango()));
d2.addProduct(new A(new Apple(), new Banana()));
System.out.println("b Price: " + d2.getTotalPrice());
System.out.println("b Cost: " + d2.getTotalCost());
System.out.println("b Profit: " + d2.getTotalProfit());
d2.checkOut();
} catch(PriceNotEnoughException e) {}
}
}