設計說明:
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:】及目前的外送金額,執行結果如圖所示 。
參考程式碼:
TQC+ Java 試題總整理
- import java.util.*;
- 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;
- }
- }
- class Deliver extends Unit{
- LinkedList<Unit> pcs;
- double total;
- Deliver(){
- pcs=new LinkedList<Unit>();
- }
- void addProduct(A A){
- pcs.add(A);
- }
- void addProduct(B B){
- pcs.add(B);
- }
- public double getTotalPrice(){
- total=0;
- for(Unit apc:pcs){
- total+=apc.getPrice();
- }
- return total;
- }
- public double getTotalCost(){
- total=0;
- for(Unit apc:pcs){
- total+=apc.getCost();
- }
- return total;
- }
- public double getTotalProfit(){
- total=0;
- for(Unit apc:pcs){
- total+=apc.getProfit();
- }
- return total;
- }
- public void checkOut() throws ToException{
- double p = getTotalPrice();
- if(p < 50.0) throw new ToException(p);
- else System.out.println();
- }
- }
- class ToException extends Exception {
- ToException(double p) {
- super("Not enough order for carry out: " + p);
- }
- }
- public class JPA603_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(Exception exp){
- System.out.println(exp.getMessage());
- }
- }
- }
TQC+ Java 試題總整理
聲明:
這裡的範例程式碼皆由本人親自編輯,歡迎轉載本教學,但請註明本網站,尊重一下作者的心血
沒有留言:
張貼留言
歡迎留言,較舊文章需要留言審核看不到自己的留言是正常的。
若長時間無回應請使用以下聯絡方式:
填寫表單:https://forms.gle/hxxX9n4tATcFnhnk8
寄信到:happyplayblogs@gmail.com