設計說明:
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
參考程式碼:
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 class JPA603_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());
- }
- }
TQC+ Java 試題總整理
聲明:
這裡的範例程式碼皆由本人親自編輯,歡迎轉載本教學,但請註明本網站,尊重一下作者的心血
沒有留言:
張貼留言
歡迎留言,較舊文章需要留言審核看不到自己的留言是正常的。
若長時間無回應請使用以下聯絡方式:
填寫表單:https://forms.gle/hxxX9n4tATcFnhnk8
寄信到:happyplayblogs@gmail.com