2017-05-02 44 views
1

我在賦值「甜點專櫃」,基本上就是我需要做的是創造DessertItem子類CandyCookieIceCreamSundaeCheckout工作。我還提供DessertItem類& DessertShoppe,我不允許修改它們。Java的重載方法發出

雖然我已經創建了所有這些子類,但是當我在TestCheckout.java上運行它時,它不起作用,而是在聖代中顯示getName()無法覆蓋DessertItem中的getName()。

public final String getName(){ 
        ^
    overriden method is final. 

我會爲您提供我現在所做的所有課程。

public class DessertShoppe { 

    public final static double TAX_RATE = 6.5;  // 6.5% 
    public final static String STORE_NAME = "M & M Dessert Shoppe"; 
    public final static int MAX_ITEM_NAME_SIZE = 25; 
    public final static int COST_WIDTH = 6; 

    public static String cents2dollarsAndCents(int cents) { 
     String s = ""; 

     if (cents < 0) { 
      s += "-"; 
      cents *= -1; 
     } 

     int dollars = cents/100; 
     cents = cents % 100; 

     if (dollars > 0) 
      s += dollars; 

     s +="."; 

     if (cents < 10) 
      s += "0"; 

     s += cents; 
     return s; 
    } 
} 


public abstract class DessertItem { 

    protected String name; 

    public DessertItem() { 
     this(""); 
    } 

    public DessertItem(String name) { 
     if (name.length() <= DessertShoppe.MAX_ITEM_NAME_SIZE) 
      this.name = name; 
     else 
      this.name = name.substring(0,DessertShoppe.MAX_ITEM_NAME_SIZE); 
    } 

    public final String getName() { 
     return name; 
    } 

    public abstract int getCost(); 
} 


public class Cookie extends DessertItem{ 

    protected double number; 
    protected double pricePerDoze; 

    public Cookie(String _n, double _ppd, int _number){ 
     super(_n); 
     pricePerDoze = _ppd; 
     number = _number; 
    } 

    public int getCost(){ 
     return (int)Math.round(number/12 * pricePerDoze); 
    } 
} 


public class Candy extends DessertItem{ 

    protected double weight; 
    protected double pricePerPound; 

    public Candy(String _n, double _ppp, int _w){ 
     //using parent's constructor with name while storing its own properties 
     super(_n); 
     pricePerPound = _ppp; 
     weight = _w; 
    } 

    public int getCost(){ 
     return (int)Math.round(weight * pricePerPound); 
    } 
} 


public class IceCream extends DessertItem{ 

    protected int cost; 

    public IceCream(String _n, int _cost){ 
     super(_n); 
     cost = _cost; 
    } 

    public int getCost(){ 
     return cost; 
    } 
} 


public class Sundae extends IceCream{ 

    protected String topName; 
    protected int topCost; 

    public Sundae(String _n0, int _cost0, String _n1, int _cost1){ 
     //put the icecream name in icecream while putting top name and cost in a separate property 
     super(_n0, _cost0); 
     topName = _n1; 
     topCost = _cost1; 
    } 

    public final String getName(){ 
     //return both the icecream name and the topping name 
     return name + " " + topName; 
    } 

    public int getCost(){ 
     //return the sum of the icecream and the topping 
     return cost + topCost; 
    } 
} 


public class Checkout{ 

    protected int size; 
    protected DessertItem[] dessertItems; 
    protected int amount; 
    protected int sum; 
    protected final double taxRate; 

    Checkout(){ 
     size = 100; 
     dessertItems = new DessertItem[size]; 
     amount = 0; 
     sum = 0; 
     taxRate = DessertShoppe.TAX_RATE; 
    } 

    public void enterItem(DessertItem d){ 
     dessertItems[amount] = d; 
     amount ++; 
    } 

    public int numberOfItems(){ 
     return amount; 
    } 

    public int totalCost(){ 
     //make sum into zero, and calculate price from every item 
     sum = 0; 
     for(int i = 0; i < amount; i ++){ 
      sum += dessertItems[i].getCost(); 
     } 
     return sum; 
    } 

    public int totalTax(){ 
     //use the totalCost method 
     return (int)(Math.round(this.totalCost() * taxRate/100)); 
    } 

    public void clear(){ 
     //clear the array 
     for(DessertItem d : dessertItems){ 
      d = null; 
     } 
     amount = 0; 
     sum = 0; 
    } 

    public String toString(){ 
     String result = "Thank You! \n"; 

     result += DessertShoppe.STORE_NAME + "\n"; 

     result += "Purchased: "; 

     String totalPay = DessertShoppe.cents2dollarsAndCents(totalCost()+totalTax()); 
     if(totalPay.length() > DessertShoppe.COST_WIDTH){ 
      totalPay = totalPay.substring(0, DessertShoppe.COST_WIDTH); 
     } 
     result += "$" + totalPay; 
     return result; 
    } 
} 


public class TestCheckout { 

    public static void main(String[] args) { 

     Checkout checkout = new Checkout(); 

     checkout.enterItem(new Candy("Peanut Butter Fudge", 2.25, 399)); 
     checkout.enterItem(new IceCream("Vanilla Ice Cream",105)); 
     checkout.enterItem(new Sundae("Choc. Chip Ice Cream",145, "Hot Fudge", 50)); 
     checkout.enterItem(new Cookie("Oatmeal Raisin Cookies", 4, 399)); 

     System.out.println("\nNumber of items: " + checkout.numberOfItems() + "\n"); 
     System.out.println("\nTotal cost: " + checkout.totalCost() + "\n"); 
     System.out.println("\nTotal tax: " + checkout.totalTax() + "\n"); 
     System.out.println("\nCost + Tax: " + (checkout.totalCost() + checkout.totalTax()) + "\n"); 
     System.out.println(checkout); 

     checkout.clear(); 

     checkout.enterItem(new IceCream("Strawberry Ice Cream",145)); 
     checkout.enterItem(new Sundae("Vanilla Ice Cream",105, "Caramel", 50)); 
     checkout.enterItem(new Candy("Gummy Worms", 1.33, 89)); 
     checkout.enterItem(new Cookie("Chocolate Chip Cookies", 4, 399)); 
     checkout.enterItem(new Candy("Salt Water Taffy", 1.5, 209)); 
     checkout.enterItem(new Candy("Candy Corn",3.0, 109)); 

     System.out.println("\nNumber of items: " + checkout.numberOfItems() + "\n"); 
     System.out.println("\nTotal cost: " + checkout.totalCost() + "\n"); 
     System.out.println("\nTotal tax: " + checkout.totalTax() + "\n"); 
     System.out.println("\nCost + Tax: " + (checkout.totalCost() + checkout.totalTax()) + "\n"); 
     System.out.println(checkout); 
    } 
} 

預期的輸出應爲:

Number of items: 4 


Total cost: 1331 


Total tax: 87 


Cost + Tax: 1418 


M & M Dessert Shoppe 
-------------------- 

2.25 lbs. @ 3.99 /lb. 
Peanut Butter Fudge  8.98 
Vanilla Ice Cream   1.05 
Hot Fudge Sundae with 
Choc. Chip Ice Cream  1.95 
4 @ 3.99 /dz. 
Oatmeal Raisin Cookies  1.33 
Tax       .87 
Total Cost    14.18 

Number of items: 6 
+2

在'Sundae'的構造函數中調用'super(_n0 +「」+ _n1,_cost0 + _cost1);';那麼你不需要重寫任何方法。爲了你自己的緣故,給你的變量更好的名字。 –

回答

2

當一個方法被標記爲最終,這意味着它不能在子類中被覆蓋。

因此,您需要使用現有的getName()方法,並找出如何將適當的值存入name變量。

幸運的是,有一個在IceCream一個構造函數,這樣做,因此,所有你需要做的就是進入你想getName()返回什麼構造函數(和你想getCost()返回什麼):

public Sundae(String _n0, int _cost0, String _n1, int _cost1){ 
    super(_n1 + " Sundae with\n" + _n0, _cost0 + _cost1); 
} 

這方式,您的聖代課程不需要getName()getCost()方法。

+0

所以這意味着對於聖代課程,我需要寫的是你的答案? – Harry

+0

我調整了你的建議並編譯了測試程序。它實際上編譯。但是,輸出顯示的不同於預期的輸出。 – Harry

+0

以什麼方式?也許構造函數的name參數應該是'_n1 +「與\ n」+ _n0'的聖代? – Jason

0

正如其他答案中所述,final修飾符表示該方法不能被其任何子類覆蓋。

這種情況有解決方法有兩種:

首先是參數傳遞到Sundae構造如圖傑森:

super(_n0 + " " + _n1, _cost0 + _cost1); 

第二是利用一個事實,即聖代是IceCream的子類,它是DessertItem的子類。 name字段爲protected,這意味着您可以直接從構造函數中訪問該字段。

請記住,對於第二種選擇,您需要明確檢查以確保您提供的name短於DessertShoppe.MAX_ITEM_NAME_SIZE以符合該標準長度。

更簡單和可能預期的解決方案將是第一個,但都是有效的。

+0

您好,感謝您的幫助!我使用了Jason的方法,但是hwen編譯並運行,它只顯示沒有這些Dessert名稱和成本的預期輸出......你能建議爲什麼是這樣嗎? – Harry