/**
 * Implements a cash register that uses only $10, $5, and $1 bills.
 * The store only sells items in whole  dollar amounts, and 
 * customers only pay in whole dollar amounts.
 * 
 * <p>Bugs: does not check if the cash register runs out of bills
 */

public class CashRegister {

	/*
	 * Instance variables
	 */
	private int num10s;
	private int num5s;
	private int num1s;
	
	/*
	 * Methods
	 */
	
	// Constructor
	public CashRegister() {
		// CashRegister starts with 100 of each type of bill
		num10s = 100;
		num5s = 100;
		num1s = 100;
	}
	
	/** 
	 * Displays the current contents of the register 
	 */
	public void printRegisterContents() {
		System.out.println(num10s + "\t$10 bills");
		System.out.println(num5s + "\t$5 bills");
		System.out.println(num1s + "\t$1 bills");
	}
	
	/** 
	 * Returns the total value of the cash in the register 
	 */
	public int getBalance() {
		return 10*num10s + 5*num5s + num1s;
	}
	
	/** 
	 * Determines how many bills are used to make change
	 * (change = amountPaid - amountOwed), 
	 * updates the data fields accordingly, 
	 * and returns the change amount.
	 */
	public int getChange(int amount, int paid) {
		// Figure out what bills the customer used 
		// (assume they used the largest bills possible)
		
		// Use integer division get the largest number of $10 bills possible
		int tens = paid/10;
		// Accumulate 10s in the cash register
		num10s += tens;
		// Figure out the amount left that the customer provided
		int remaining = paid - 10*tens;
		// Repeat for $5 and $1 bills
		int fives = remaining/5;
		num5s += fives;
		remaining = remaining - 5*fives;
		int ones = remaining;
		num1s += ones;
		
		// Calculate change due to the customer
		int change = paid - amount;
		
		// Figure out how to hand back the change 
		// (i.e., what bills to use)
		// Same technique as above
		tens = change/10;
		num10s -= tens;
		remaining = change - 10*tens;
		fives = remaining/5;
		num5s -= fives;
		remaining = remaining - 5*fives;
		ones = remaining;
		num1s -= ones;
		
		return change;
	}
	
}
