2012-04-11 49 views
-1

對於我最近的任務,我負責創建一個接受付款並允許您取消它們的訂票機。最近的任務擴展是計算添加的便士中有多少是某種類型的便士,例如10p,20p,50p。在「機器」中計算'coin'p硬幣的數量

我設法讓程序精確計算機器內部10p的數量,但當我嘗試20時,我遇到了麻煩。

比如當我此刻的運行它,它完美地運行了10馬力但是當我運行它爲20PS我得到這些結果反饋:

測試:硬幣計數

錯誤:預期4 - 20P硬幣發現5 - 20P硬幣

赫雷什計算10/20/50ps的多少是在機器的代碼:

class ProcessMoney 
{ 
int ticket; 
int cost = 0; 
int machine; 
int pence = 0; 
int calc = 0; 

public void setTicketPrice(int amount) { 

    ticket = amount; 

} 

public int getTicketPrice() { 

    return ticket; 

    } 


public void add(int coin) { 

    cost = cost + coin; 

    if (coin == 10){ 
     calc = calc + 1; 
    } 


    } 

public boolean enough() { return true; } 

public int getPaidSoFar() { return cost; } 

    public void cancel() { 

     cost = 0; 

} 

public void bought() { 

     machine = machine + cost; 
     cost = 0; 

    } 

    public int moneyInMachine() { 

     return machine; 

} 

public int getCoins(int coin) { 

    if (coin == 10){ 
    pence = machine * 100; 
    calc = pence/1000; 
    } 
    else if (coin == 20){ 
     pence = machine * 100; 
     calc = pence/2000; 
     } 

    return calc; 
    } 
} 

赫雷什的代碼測試它:

class Main 
{ 
    private static ProcessMoney pm = new ProcessMoney(); 

    public static void main(String args[]) 
    { 
    int res = 0, expected = 100; 

    test("setTicketPrice() & getTicketPrice() "); 

    pm.setTicketPrice(expected); 
    res = pm.getTicketPrice(); 
    check(res == expected, 
      "Ticket price is %d should be %d", res, expected); 

    expected = 200; 
    pm.setTicketPrice(expected); 
    res = pm.getTicketPrice(); 
    check(res == expected, 
      "Ticket price is %d should be %d", res, expected); 

    test("add() & getPaidSoFar()"); 

    pm.add(10); pm.add(20); pm.add(30); 
    expected = 60; 
    res = pm.getPaidSoFar(); 
    check(res == expected, 
      "Money entered into machine is %d should be %d", res, expected); 
    pm.add(20); pm.add(40); pm.add(40); 
    expected = 160; 
    res = pm.getPaidSoFar(); 
    check(res == expected, 
      "Money entered into machine is %d should be %d", res, expected); 

    test("add() & cancel()"); 

    pm.add(10); pm.add(20); pm.add(30); 
    expected = 0; 
    pm.cancel(); 
    res = pm.getPaidSoFar(); 
    check(res == expected, 
      "money entered into machine is now %d should be 0", res); 

    pm.add(100); pm.add(200); pm.add(300); 
    expected = 0; 
    pm.cancel(); 
    res = pm.getPaidSoFar(); 
    check(res == expected, 
      "money entered into machine is now %d should be 0", res); 

    test("enough()"); 

    pm.setTicketPrice(200); 
    pm.add(100); pm.add(100); pm.add(0); 
    expected = 200; 
    check(pm.enough(), 
      "Enough money entered into machine 200 for 200 ticket"); 
    pm.cancel(); 

    pm.setTicketPrice(210); 
    pm.add(100); pm.add(100); pm.add(20); 
    expected = 200; 
    check(pm.enough(), 
      "Enough money entered into machine 220 for 210 ticket"); 
    pm.cancel(); 

    test("bought() & moneyInMachine()"); 

    pm.setTicketPrice(200); 
    pm.add(100); pm.add(100); pm.add(0); 
    if (pm.enough()) 
    { 
     pm.bought(); 
    } 

    expected = 200; 
    res = pm.moneyInMachine(); 
    check(expected == res, 
      "Total money in machine %d should be %d", res, expected); 
    res = pm.getPaidSoFar(); 
    check(res == 0, 
      "Money for ticket in machine is %d should be 0", res); 
    pm.cancel(); 


    pm.setTicketPrice(200); 
    pm.add(100); pm.add(100); pm.add(10); 
    if (pm.enough()) 
    { 
     pm.bought(); 
    } 

    expected = 410; 
    res = pm.moneyInMachine(); 
    check(expected == res, 
      "Total money in machine %d should be %d", res, expected); 
    res = pm.getPaidSoFar(); 
    check(res == 0, 
      "Money for ticket in machine is %d should be 0", res); 

    test("Count coins"); 
    pm = new ProcessMoney(); 
    checkRecord(10, 2); 
    checkRecord(20, 4); 
    checkRecord(50, 3); 
    checkRecord(100, 3); 
    checkRecord(200, 2); 

    System.out.println("Success"); 
    } 

    private static void checkRecord(int coin, int howMany) 
    { 
    pm.setTicketPrice(howMany * coin); 

    for (int i=1; i<=howMany*2; i++) 
    { 
     pm.add(coin); 
    } 
    pm.cancel(); 

    for (int i=1; i<=howMany; i++) 
    { 
     pm.add(coin); 
    } 

    pm.bought(); 
    int actual = pm.getCoins(coin); 
    check(howMany == actual, 
      "Expected %d - %dp coins found %d - %dp coins", 
      howMany, coin, actual, coin ); 
    } 

    private static String what = ""; 

    public static void check(boolean ok, String fmt, Object... params) 
    { 
    if (! ok) 
    { 
     System.out.println(what); 
     System.out.print("ERROR: "); 
     System.out.printf(fmt, params); 
     System.out.println(); 
     System.exit(-1); 
    } 
    } 

    public static void test(String str) 
    { 
    what = "Test: " + str; 
    } 

} 
+1

你是否已經通過調試器完成了代碼?你可以很容易地找到你的問題。 – Msonic 2012-04-11 20:01:20

回答

0

問題是您的機器錢不會從一個支票呼叫清除到下一個。這裏是我測試你的代碼:

private static void checkRecord(int coin, int howMany) { 

pm.setTicketPrice(howMany * coin); 

System.out.println("Before, Machine has"+pm.moneyInMachine());<---added print 
for (int i=1; i<=howMany*2; i++) 
{ 
    pm.add(coin); 
} 
pm.cancel(); 

for (int i=1; i<=howMany; i++) 
{ 
    pm.add(coin); 
} 

pm.bought(); 
System.out.println("After, Machine has"+pm.moneyInMachine());<---added print 
int actual = pm.getCoins(coin); 
check(howMany == actual, 
     "Expected %d - %dp coins found %d - %dp coins", 
     howMany, coin, actual, coin );} 

這裏是它印對我來說(我也停止了您的系統的出口,並始終打印出來):

Before, Machine has0 
After, Machine has20 
Test: Count coins 
ERROR: Expected 2 - 10p coins found 2 - 10p coins 
Before, Machine has20 
After, Machine has100 
Test: Count coins 
ERROR: Expected 4 - 20p coins found 5 - 20p coins 
Before, Machine has100 
After, Machine has250 
Test: Count coins 
ERROR: Expected 3 - 50p coins found 5 - 50p coins 
Before, Machine has250 
After, Machine has550 
Test: Count coins 
ERROR: Expected 3 - 100p coins found 5 - 100p coins 
Before, Machine has550 
After, Machine has950 
Test: Count coins 
ERROR: Expected 2 - 200p coins found 5 - 200p coins 
+0

只是爲了澄清,因爲印刷有點奇怪。第一次通話(加入了2個10p硬幣)的20個,仍在機器中。因此,當預期80時,支票會發現100p總數。因此,您需要清除機器中的硬幣從一張支票到下一張支票,或者考慮到預期數量。 – 2012-04-11 20:28:55

0

看看你add(int coin)方法。這就是你的問題 開始的地方。

另外@ Msonic的建議使用調試器是非常好的。