// Arup Guha
// 4/25/2006
// Written for COP 3330 Final Exam

// Edited for 2010 BHCSI on 7/19/2010

public class Money {

  	private int dollars;
  	private int cents;

  	// Creates a default Money object worth no value.
  	public Money() {
    	dollars = 0;
    	cents = 0;
  	}

  	// Creates a Money object equivalent to n cents.
  	public Money(int n) {
    	dollars = n/100;
    	cents = n%100;
  	}

  	// Returns the total number of cents the current
  	// object is worth.
  	private int numCents() {
    	return 100*dollars+cents;
  	}

  	// Returns a new money object worth the sum of the
  	// current object and other.
  	public Money addMoney(Money other) {

    	int total = this.numCents()+other.numCents();
    	return new Money(total);
  	}
  	
  	// Returns a new Money object equal to the current 
  	// object minus other. If other is larger, a Money
  	// object with value 0 is returned.
  	public Money subMoney(Money other) {
  		int total = this.numCents()-other.numCents();
  		
  		if (total < 0)
  			return new Money();
  		else
  			return new Money(total);
  	}
  	
  	// Transfers the value of other into this object.
  	// At the end of this operation other will be 0 and
  	// this object will be the sum of its old value and
  	// other's old value.
  	public void transferMoney(Money other) {
  		
  		// Figure out how many cents are in other.
  		int extra = other.numCents();
  		
  		// Reset other.
  		other.dollars = 0;
  		other.cents = 0;
  		
  		// Add in the cents from other.
  		this.cents += extra;
  		
  		// See if we get more dollars with these cents and
  		// adjust dollars and cents accordingly.
  		int extraDollars = this.cents/100;
  		this.dollars += extraDollars;
  		this.cents = this.cents%100;
  		
  	}

	// Returns a string representation of this Money object.
  	public String toString() {
    	if (cents > 10)
      		return "$"+dollars+"."+cents;
    	else 
      		return "$"+dollars+".0"+cents;
  	}

  	public static void main(String[] args) {

    	// Create a bunch of money objects.
    	Money test1 = new Money(134);
    	Money test2 = new Money(68);
    	Money test3 = new Money(32);
    	Money test4 = (test1).addMoney(test2);
    	Money test5 = (test2).addMoney(test3);
    	Money test6 = (test1).addMoney(test3);
    	Money test7 = (test3).addMoney(test2);
    	
    	// See what each are equal to.
    	System.out.println("Money 1: "+test1);
		System.out.println("Money 2: "+test2);
		System.out.println("Money 3: "+test3);
		System.out.println("Money 4: "+test4);
		System.out.println("Money 5: "+test5);
		System.out.println("Money 6: "+test6);
		System.out.println("Money 7: "+test7);
		
		// Test out the transfer and print the results.
		test6.transferMoney(test4);
		
    	System.out.println("After transfer Money4: "+test4);
    	System.out.println("After transfer Money6: "+test6);
  }
}
