2015-02-07 80 views
0

我創建了一個非常簡單的收銀機類,並希望以單獨的方法對其進行測試。當我從測試方法中調用在類中創建的方法時,有一個錯誤說明某個方法在它創建的類中是未定義的,但事實並非如此。有人可以解釋爲什麼我得到這個錯誤?謝謝。創建的類和公共方法無法通過測試方法

類:

/** 
* A simulated cash register that tracks the item count and 3 the total 
* amount due. 
*/ 
public class CashRegister { 
    private int itemCount; 
    private double totalPrice; 


    public CashRegister() { 
     itemCount = 0; 
     totalPrice = 0; 
    } 


    public void addItem(double price) { 
     itemCount++; 
     totalPrice = totalPrice + price; 
    } 

    public double getTotal() { 
     return totalPrice; 
    } 


    public int getCount() { 
     return itemCount; 
    } 

    public void clear() { 
     itemCount = 0; 
     totalPrice = 0; 
    } 
} 

測試的類:

public class cashRegisterTester { 

    public static void main(String[] args) { 

     cashRegister register1 = new cashRegister(); 

     register1.addItem(0.95); 
     register1.addItem(2.50); 
     System.out.println(register1.getCount()); 
     System.out.println("Expected: 3"); 
     System.out.printf("%.2f\n", register1.getTotal()); 
     System.out.println("Expected: 5.40"); 

    } 

} 
+0

您正在聲明一個完全不包含成員的cashregister類,並且聲明瞭另一個具有一些成員的嵌套cashregister類。然後,你試圖測試外部類。當然,它不會編譯,因爲外部類沒有成員。 – 2015-02-07 16:27:46

回答

0

你有類類中:

public class cashRegister { /** * A simulated cash register that tracks the item count and 3 the total * amount due. */ 
    public class CashRegister 

只刪除public class CashRegister,它應該爲你工作。

1

擺脫第一public class cashRegister {線和最終}的。

+0

非常感謝你! – Michael 2015-02-07 16:37:48

+0

沒問題!我們在這裏提供幫助。 – 2015-02-07 16:44:20