Computer Science 210
Data Structures

Fall 2019, Siena College

PocketChange BlueJ Project

Click here to download a BlueJ project for PocketChange.


PocketChange Source Code

The Java source code for PocketChange is below. Click on a file name to download it.


PocketChange.java


/**
 * Keep track of a set of coins.
 *
 * @author Jim Teresco and Fall 2019 CSIS 210
 */

import java.util.ArrayList;

public class PocketChange
{
    
    // ArrayList to hold my coin values
    private ArrayList<Integer> coins;
    
    /**
     * construct an empty PocketChange
     */
    public PocketChange() {
        
        coins = new ArrayList<Integer>();
    }
    
    /**
     * add a new coin with the given value to the PocketChange
     * 
     * @param value the value of the coin to add
     */
    public void addCoin(int value) {
    
        coins.add(value);
    }
    
    /**
     * Return the number of coins in the object
     * 
     * @return the number of coins in the object
     */
    public int numCoins() {
     
        return coins.size();
    }
    
    /**
     * Return the total value of the coins in the pocket
     * 
     * @return the total value of the coins in the pocket
     */
    public int totalValue() {
     
        int sum = 0;
        for (int coin : coins) {
            sum += coin;
        }
        
        return sum;
    }
    
    /**
     * remove a coin with a given value from the pocket
     * 
     * @param value the value of the coin to remove
     */
    public void removeCoin(int value) {
     
        if (coins.contains(value)) {
            coins.remove(new Integer(value));
        }
        else {
            System.out.println("No coin of value " + value + " to remove!");
        }
    }
    
    /**
     * return a String representing a human-readable form of the
     * coins in the object.
     * 
     * @return a String representing a human-readable form of the
     * coins in the object.
     */
    public String toString() {
     
        return "Have " + coins.size() + " coins: " + coins; 
    }
    
    /**
     * main method with tests
     * 
     * @param args not used
     */
    public static void main(String args[]) {
     
        PocketChange p = new PocketChange();
        
        System.out.println("My empty PocketChange:");
        System.out.println(p);
        System.out.println("Total value: " + p.totalValue());
        
        p.addCoin(25);
        p.addCoin(1);
        p.addCoin(10);
        p.addCoin(25);
        
        System.out.println("Added some coins:");
        System.out.println(p);
        System.out.println("Total coins: " + p.numCoins());
        System.out.println("Total value: " + p.totalValue());
        
        p.removeCoin(5);
        p.removeCoin(10);
        
        System.out.println("Removed some coins:");
        System.out.println(p);
        System.out.println("Total coins: " + p.numCoins());
        System.out.println("Total value: " + p.totalValue());

    }
}