previous | start | next

File Purse.java

1 import java.util.ArrayList;
2
3 /**
4     A purse holds a collection of coins.
5 */
6 public class Purse
7 {
8    /**
9        Constructs an empty purse.
10     */
11    public Purse()
12    {
13       coins = new ArrayList();
14    }
15
16    /**
17        Add a coin to the purse.
18       @param aCoin the coin to add
19     */
20    public void add(Coin aCoin)
21    {
22       coins.add(aCoin);
23    }
24
25    /**
26        Get the total value of the coins in the purse.
27       @return the sum of all coin values
28     */
29    public double getTotal()
30    {
31       double total = 0;
32       for (int i = 0; i < coins.size(); i++)
33       {
34          Coin aCoin = (Coin)coins.get(i);
35          total = total + aCoin.getValue();       
36       }
37       return total;
38    }
39
40    private ArrayList coins;
41 }
42


previous | start | next