Class CurrencyAmount
java.lang.Object
it.unimi.di.prog2.h12.CurrencyAmount
Immutable class that represents a (positive or negative) amount in a currency that has an integer
part and cents.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intThe amount expressed in cents.static final CurrencyAmountA constant representing the zero amount. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintcents()Returns the cents of the amount.booleaninthashCode()static CurrencyAmountnegative(int units, int cents) Produce a negative amount.static CurrencyAmountparseCurrencyAmount(String amount) Parses an amount from a string.static CurrencyAmountpositive(int units, int cents) Produce a positive amount.intsign()Returns the sign of the amount.somma(CurrencyAmount other) Returns a new amount representing the sum of this amount and another.sottrai(CurrencyAmount other) Returns a new amount representing the difference between this amount and another.toString()intunits()Returns the integer part of the amount.
-
Field Details
-
ZERO
A constant representing the zero amount. -
cents
private final int centsThe amount expressed in cents.
-
-
Constructor Details
-
CurrencyAmount
private CurrencyAmount(int cents) Private constructor.- Parameters:
cents- the amount in cents.
-
-
Method Details
-
positive
Produce a positive amount.- Parameters:
units- whole units of the amount.cents- cents of the amount.- Returns:
- a positive
CurrencyAmountrepresenting the specified amount. - Throws:
IllegalArgumentException- ifcentsis not between 0 and 99 orunitsis negative.
-
negative
Produce a negative amount.- Parameters:
units- whole units of the amount.cents- cents of the amount.- Returns:
- a negative
CurrencyAmountrepresenting the specified amount. - Throws:
IllegalArgumentException- ifcentsis not between 0 and 99 orunitsis negative.
-
parseCurrencyAmount
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- ifamountisnull.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
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- ifaltroisnull.
-
sottrai
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- ifotheris greater than this amount.NullPointerException- ifotherisnull.
-
toString
-
equals
-
hashCode
-