Class CurrencyAmount

java.lang.Object
it.unimi.di.prog2.h12.CurrencyAmount

public class CurrencyAmount extends Object
Immutable class that represents a (positive or negative) amount in a currency that has an integer part and cents.
  • Field Details

    • ZERO

      public static final CurrencyAmount ZERO
      A constant representing the zero amount.
    • cents

      private final int cents
      The amount expressed in cents.
  • Constructor Details

    • CurrencyAmount

      private CurrencyAmount(int cents)
      Private constructor.
      Parameters:
      cents - the amount in cents.
  • Method Details

    • positive

      public static CurrencyAmount positive(int units, int cents)
      Produce a positive amount.
      Parameters:
      units - whole units of the amount.
      cents - cents of the amount.
      Returns:
      a positive CurrencyAmount representing the specified amount.
      Throws:
      IllegalArgumentException - if cents is not between 0 and 99 or units is negative.
    • negative

      public static CurrencyAmount negative(int units, int cents)
      Produce a negative amount.
      Parameters:
      units - whole units of the amount.
      cents - cents of the amount.
      Returns:
      a negative CurrencyAmount representing the specified amount.
      Throws:
      IllegalArgumentException - if cents is not between 0 and 99 or units is negative.
    • parseCurrencyAmount

      public static CurrencyAmount parseCurrencyAmount(String amount)
      Parses an amount from a string.

      The string must have the format U.C or -U.C, where U and C are two integers corresponding to the units and cents and C has exactly two digits.

      Parameters:
      amount - the amount.
      Returns:
      the parsed CurrencyAmount.
      Throws:
      NullPointerException - if amount is null.
      IllegalArgumentException - if the string is not in the correct format.
    • sign

      public int sign()
      Returns the sign of the amount.
      Returns:
      1 if the amount is positive, 0 if it is zero, -1 otherwise.
    • cents

      public int cents()
      Returns the cents of the amount.
      Returns:
      the cents of the amount, it is always non negative.
    • units

      public int units()
      Returns the integer part of the amount.
      Returns:
      the integer part of the amount, it is always non negative.
    • somma

      public CurrencyAmount somma(CurrencyAmount other)
      Returns a new amount representing the sum of this amount and another.
      Parameters:
      other - the other amount.
      Returns:
      the sum of the two amounts.
      Throws:
      NullPointerException - if altro is null.
    • sottrai

      public CurrencyAmount sottrai(CurrencyAmount other)
      Returns a new amount representing the difference between this amount and another.
      Parameters:
      other - the other amount.
      Returns:
      the difference between the two amounts.
      Throws:
      IllegalArgumentException - if other is greater than this amount.
      NullPointerException - if other is null.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object