package cse1030.labtest1; /** * A class that represents an amount of Canadian money in dollars and cents. * The class also keeps track of the total amount of dollars and cents in * circulation. When money is created, the total amount of money in * circulation is increased by the value of the created money. When an * amount of money is modified, the total amount of money in circulation is * updated with the change in the value of the money. * *

* The Money class keeps track of two values: *

    *
  1. the number of dollars in circulation
  2. *
  3. the number of cents in circulation
  4. *
* *

* Each Money object keeps track of two values: *

    *
  1. the number of dollars in this money object
  2. *
  3. the number of cents in this money object
  4. *
* * @author CSE1030 * */ public class Money { private static int totalDollars = 0; private static int totalCents = 0; private int dollars; private int cents; /** * Construct a money object having 0 dollars and 0 cents. */ public Money() { this(0, 0); } /** * Construct a money object given the amount of dollars and amount of cents. * Updates the total of amount of money in circulation. * * @param dollars The amount of dollars. * @param cents The amount of cents. * @throws IllegalArgumentException if dollars < 0 or cents < 0. */ public Money(int dollars, int cents) { if( !this.setDollars(dollars) ) { throw new IllegalArgumentException("dollars is negative"); } else if( !this.setCents(cents) ) { throw new IllegalArgumentException("cents is negative"); } } /** * Set the amount of dollars. If dollars < 0 then the * state of the object is unchanged from its current value and false * is returned. If dollars >= 0 then the state of the object is * updated, the total amount of money in circulation is updated, * and true is returned. * *

The total amount of money in circulation is updated with the * change in the value of dollars for this money object. For example, * suppose the number of dollars for a money object m * is 10; then m.setDollars(15) will increase the total amount of * money in circulation by 5 dollars. * * @param dollars The amount of dollars. * @return true if dollars >= 0; false otherwise. */ public boolean setDollars(int dollars) { boolean ok = false; if (dollars >= 0) { Money.totalDollars -= this.getDollars(); Money.totalDollars += dollars; this.dollars = dollars; ok = true; } return ok; } /** * Set the amount of cents. If cents < 0 then the * state of the object is unchanged from its current value and false * is returned. If cents >= 0 then the state of the object is * updated, the total amount of money in circulation is updated, * and true is returned. * *

The total amount of money in circulation is updated with the * change in the value of cents for this money object. For example, * suppose the number of cents for a money object m * is 50; then m.setCents(30) will decrease the total amount of * money in circulation by 20 cents. * * @param cents The amount of cents. * @return true if cents >= 0; false otherwise. */ public boolean setCents(int cents) { boolean ok = false; if (cents >= 0) { Money.totalCents -= this.getCents(); Money.totalCents += cents; this.cents = cents; ok = true; } return ok; } /** * Get the amount of dollars represented by this object. * (ignoring the number of cents). For example, for a money * object representing 10 dollars and 55 cents this method * would return 10. * * @return The number of dollars. */ public int getDollars() { return this.dollars; } /** * Get the amount of cents represented by this object * (ignoring the number of dollars). For example, for a money * object representing 10 dollars and 55 cents this method * would return 55. * * @return The number of cents. */ public int getCents() { return this.cents; } /** * Get the total amount of dollars in circulation. * * @return The number of dollars in circulation. */ public static int getDollarsInCirculation() { return Money.totalDollars; } /** * Get the total amount of cents in circulation. * * @return The number of cents in circulation. */ public static int getCentsInCirculation() { return Money.totalCents; } /** * Creates a string representation of a money object. The string * is the number of dollars followed by " dollars and " followed by * the number of cents followed by " cents". * * @return The string representation of the object. */ @Override public String toString() { return this.getDollars() + " dollars and " + this.getCents() + " cents"; } }