2016-02-25 51 views
0
package homeWork; 

public class ShoppingBag { 

private int items; 
private float totalRetailCost; 
private float taxRate; 

public ShoppingBag(float taxRate){ 
    this.taxRate = taxRate; 
    items = 0; 
    totalRetailCost = 0.0f; 
} 

// Transformer 
public void place(int numItems, float theCost){ 
    items = items += numItems; 
    totalRetailCost += (numItems * theCost); 
} 

public int getItems(){ 
    return items; 
} 

public float getRetailCost(){ 
    return totalRetailCost; 
} 

public float getTotalCost(){ 
    return totalRetailCost + (1 + taxRate); 
} 

public String toString(){ 
    String result = "The bag contains " + items + " items"; 
      result += "The retail cost of items is" + totalRetailCost; 
      result += "The total cost = " + getTotalCost(); 
    return result; 

    } 
} 

package homeWork; 

import java.util.*; 

public class MainClass { 

public static void main(String[] args){ 
    Scanner conIn = new Scanner(System.in); 
    ShoppingBag sb = new ShoppingBag(0.06f); 
    int count = 0; 
    float cost = 0.0f; 
    System.out.print("Enter count (0 to stop):"); 
    count = conIn.nextInt(); 

    while(count != 0){ 
     System.out.print("Enter cost: "); 
     cost = conIn.nextFloat(); 
     sb.place(count, cost); 
     System.out.print("Enter count (0 to stop):"); 
     count = conIn.nextInt(); 
    } 
} 

} 

我已經嘗試了所有我在這裏找到的在輸入完成後返回結果。我已經完成了我的書讓我做的事情,但我沒有得到結果。只要朝着正確的方向推動就會有所幫助。不知道如何打印toString

+1

你試圖得到什麼結果? –

+1

所以你只是試圖在最後顯示輸出?如果是這樣,你只需要System.out.print你的對象 – Logan

+0

@C.Metzan接受答案,如果這解決了你的問題,謝謝 –

回答

2

您不打印任何對象。打印對象

System.out.print(sb); 
+0

哇,我不能相信它是如此簡單。謝謝 –

+0

@ C.Metzan如果答案幫助你,你能接受嗎?謝謝 –

+0

對不起,我以爲我接受了它。 –

相關問題