it.dtt.ca.util
Class Money

java.lang.Object
  extended by it.dtt.ca.util.Money
All Implemented Interfaces:
java.io.Serializable

public class Money
extends java.lang.Object
implements java.io.Serializable

Class representing a quantity of money; the value can be indicated using a specific currency. This representation has no legal value and is provided only for information. This class does not provide currency exchange calculation.

See Also:
Serialized Form

Constructor Summary
protected Money(java.lang.String currency, int numOfDecimals)
          Create an instance using a specific currency and a specic number of decimals.
 
Method Summary
 void add(Money x)
          Sum this amount to the amount given as parameter.
 int compareTo(java.lang.Object arg0)
          Compares this object with the specified object for order.
 boolean equals(java.lang.Object obj)
          Indicates whether some other object is "equal to" this one.
 java.lang.String getCurrency()
          Get the currency adopted to indicate the price.
 int getDecimals()
          Get the price amount in decimals of the adopted currency, e.g. 22 = 0.22 CAD The total price is given by the sum of units + decimals / (10^numOfDecimals).
 int getUnits()
          Get the amount in units of the adopted currency, e.g. 5 = 5 JPY The total amount is given by the sum of units and cents.
 int hashCode()
          Computes the hash code for this Money.
 void setValue(int units)
          Set a specific amount in a specific currency
 void setValue(int units, int decimals)
          Set a specific amount in a specific currency The total amount is given by the sum of units and cents.
 void subtract(Money y)
          Subtract the amount given as parameter from the current amount.
 java.lang.String toString()
          Convert the money amount to a printable string.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Money

protected Money(java.lang.String currency,
                int numOfDecimals)
         throws UnsupportedFeatureException
Create an instance using a specific currency and a specic number of decimals. For example, the Euro and the US Dollar have 2 decimals (cents), while the Japanese Yen has no decimal.

Parameters:
currency - a string representing the currency in the ISO 4217 interational standard. The currency is indicated by means of three uppercase letters, e.g. EUR, USD, JPY, CHF, AUD, CAD, CNY, GBP.
numOfDecimals - the number of decimals of this currency (0..3)
Throws:
UnsupportedFeatureException - if currency String is not a valid one.
Method Detail

setValue

public final void setValue(int units)
Set a specific amount in a specific currency

Parameters:
units - the amount in units of the indicated currency, e.g. 3 = 3 EUR

setValue

public final void setValue(int units,
                           int decimals)
Set a specific amount in a specific currency The total amount is given by the sum of units and cents.

Parameters:
units - the amount in units of the indicated currency, e.g. 3 = 3 EUR
decimals - the amount in decimals of the indicated currency, e.g. 18 = 0.18 USD (in this case numOfDecimals = 2)

getUnits

public final int getUnits()
Get the amount in units of the adopted currency, e.g. 5 = 5 JPY The total amount is given by the sum of units and cents.

Returns:
the part of the total amount that can be expressed in units of the selected currency

getDecimals

public final int getDecimals()
Get the price amount in decimals of the adopted currency, e.g. 22 = 0.22 CAD The total price is given by the sum of units + decimals / (10^numOfDecimals).

Returns:
the part of the money amount that can be expressed in decimals of a currency

getCurrency

public final java.lang.String getCurrency()
Get the currency adopted to indicate the price.

Returns:
the currency in which the amount is indicated (one of EUR, USD, JPY, etc.)

add

public final void add(Money x)
               throws java.lang.IllegalArgumentException
Sum this amount to the amount given as parameter.

Parameters:
x - the amount to add
Throws:
java.lang.IllegalArgumentException - if x is espressed in a different currency

subtract

public final void subtract(Money y)
                    throws java.lang.IllegalArgumentException
Subtract the amount given as parameter from the current amount. The resulting amount must be equal or above zero.

Parameters:
y - the amount to subtract from the current amount.
Throws:
java.lang.IllegalArgumentException - if y is espressed in a different currency
java.lang.ArithmeticException - if the result is not equal or above zero

toString

public java.lang.String toString()
Convert the money amount to a printable string.

Overrides:
toString in class java.lang.Object
Returns:
a string containing a printable version of the money amount.

compareTo

public int compareTo(java.lang.Object arg0)
Compares this object with the specified object for order.

See Also:
Comparable.compareTo(java.lang.Object)

equals

public boolean equals(java.lang.Object obj)
Indicates whether some other object is "equal to" this one.

Overrides:
equals in class java.lang.Object
See Also:
Object.equals(java.lang.Object)

hashCode

public int hashCode()
Computes the hash code for this Money.

Overrides:
hashCode in class java.lang.Object
Returns:
a hash code value for this object.