package hw02;

/**
 * @author Thomas
 * The Money class holds US currency in terms of dollars and cents.
 */
public class Money {
	private int dollars;
	private int cents;

	/**
	 * Null constructor creates $0.0
	 */
	public Money() {
		this.dollars=0;
		this.cents=0;
	}

	/**
	 * Two integer constructor for exact dollars and cents
	 * @param dollars an integer dollar amount
	 * @param cents an integer cents amount
	 */
	public Money(int dollars,int cents) {
		this.dollars=dollars;
		this.cents= cents;
	}

	/**
	 * Single floating point (double) constructor - rounds to the nearest cent, and prints a message if rounding is significant
	 * @param dollarsAndCents - dollars are the integer part, cents are the decimal part
	 */
	public Money(double dollarsAndCents) {
		dollars = (int)dollarsAndCents;
		double dCents = (dollarsAndCents-(double)dollars)*100.0;
		cents = (int) Math.round(dCents);
		double diff =  Math.abs(((double)cents)-dCents);
//System.out.println("DEBUG: cents=" + cents + " dCents=" + dCents + " difference=" +	diff);
		if (diff>0.0001 ){
			System.out.println("Fractional cents in " + dollarsAndCents + " rounded to " + this);
		}
	}

	/**
	 * Cloning constructor - creates a new Money object with the same values as the paramter
	 * @param from the Money object to clone from
	 */
	public Money(Money from) {
		this.dollars = from.dollars;
		this.cents =  from.cents;
	}

	/**
	 * Dollars getter
	 * @return integer dollars
	 */
	public int getDollars() { return dollars; }

	/**
	 * Cents getter
	 * @return integer cents
	 */
	public int getCents() { return cents; }

	/**
	 * Add a money object to this money object 
	 * @param adder Money object to add to this
	 * @return a new Money object that contains the sum of this + adder
	 */
	public Money add(Money adder) {
		Money result = new Money();
		result.dollars = adder.dollars + this.dollars;
		result.cents = adder.cents + this.cents;
		if(result.cents>99) {
			result.cents= result.cents-100;
			result.dollars= result.dollars + 1;
		}
    	if (result.cents<0) {
			result.dollars = result.dollars - 1;
			result.cents = result.cents +100;
		}
		return result;
	}

	
	/**
	 * Multiply a money object by a floating point number
	 * @param multiplier a double multiplier to multiply by
	 * @return a new Money object that contains the result of the multiplication
	 */
	public Money mult(double multiplier) {
		double dMoney=(dollars + (cents/100.0))*multiplier;
		return new Money(dMoney);
	}

	/**
	 * The compareTo method compares this money object to another
	 * @param that the other Money object to compare to
	 * @return an integer less than 0 if this less than that, equal to 0 if this equals that, greater than 0 if this greater than that
	 */
	int compareTo(Money that) {
		int ddollars=this.dollars-that.dollars;
		if (ddollars != 0) return ddollars;
		return this.cents-that.cents;
	}

	/**
	 * Return the floating point representation of this Money, where dollars 
	 * are whole numbers, and cents are decimals
	 * @return double representation of this
	 */
	public double toDouble() { return (dollars + (cents/100.0)); }

	
	/**
	 * Returns the string representation of this money
	 */
	public String toString() {
		return String.format("$%d.%02d",dollars,cents);
	}

}
