com.pcauto.util
Class Currency

java.lang.Object
  extended bycom.pcauto.util.Quantity
      extended bycom.pcauto.util.Currency
All Implemented Interfaces:
java.lang.Comparable, java.io.Serializable

public class Currency
extends Quantity

Currency objects used to hold currency information. Objects can only be instantiated using the CurrencyFormat class. A Currency object's concept of unit is hidden from class users, and instead inferred from the formatter class at instantiation.

The baseUnit is used to store the unit with which Currency objects will be printed using the overrided toString() method.

A set of arithmetic operations have been included to compare, add, subtract, divide and multiply currency objects. All arithmetic operations have been designed to be null safe.

See Also:
Serialized Form

Field Summary
protected static CurrencyUnits baseUnit
           
static Currency ZERO
          Instance of Currency with zero value.
 
Fields inherited from class com.pcauto.util.Quantity
quantity, unit
 
Constructor Summary
protected Currency(double quantity, CurrencyUnits unit)
          Creates new Currency with an initial value double value and int unit.
 
Method Summary
 Currency add(Currency quant)
          Adds this Currency to the supplied Currency.
static Currency add(Currency a, Currency b)
          Adds supplied Currency to other supplied Currency.
static boolean areEqual(Currency quant1, Currency quant2)
          Determines if two Currency objects passed to the fuction are equal.
 java.lang.Object clone()
          Creates new Currency using an instance of Currency.
 int compareTo(Currency quant)
          Compares this Currency with the specified Currency.
 Currency divide(java.math.BigDecimal divisor)
          Divides this Currency by supplied BigDecimal.
 java.math.BigDecimal divide(Currency quant)
          Divides this Currency by supplied Currency.
 boolean equals(Currency quant)
          Compares this Currency to the specified Currency.
static CurrencyUnits getBaseUnit()
          Gets base unit with which this currency is printed.
 boolean greaterThan(Currency quant)
          Compares this Currency to the specified Currency.
 boolean lessThan(Currency quant)
          Compares this Currency to the specified Currency.
 Currency multiply(java.math.BigDecimal multiplier)
          Multiplies this Currency by supplied BigDecimal.
static void setBaseUnit(CurrencyUnits unit)
          Sets the "toString" representation of any Currency objects to be of type unit.
 Currency subtract(Currency quant)
          Subtracts supplied Currency from this Currency.
 
Methods inherited from class com.pcauto.util.Quantity
add, add, areEqual, compareTo, compareTo, divide, equals, equals, getQuantity, getUnit, greaterThan, lessThan, myDivide, myMultiply, setQuantity, setUnit, setZERO, subtract
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

baseUnit

protected static CurrencyUnits baseUnit

ZERO

public static final Currency ZERO
Instance of Currency with zero value.

Constructor Detail

Currency

protected Currency(double quantity,
                   CurrencyUnits unit)
Creates new Currency with an initial value double value and int unit.

Parameters:
quantity - double currency & @param unit CurrencyUnits
Method Detail

getBaseUnit

public static CurrencyUnits getBaseUnit()
Gets base unit with which this currency is printed.

Returns:
CurrencyUnits unit with which currencies are printed.

setBaseUnit

public static void setBaseUnit(CurrencyUnits unit)
Sets the "toString" representation of any Currency objects to be of type unit.


clone

public java.lang.Object clone()
Creates new Currency using an instance of Currency.

Specified by:
clone in class Quantity
Returns:
new Quantity object

equals

public boolean equals(Currency quant)
Compares this Currency to the specified Currency. The result is true if and only if the argument is not null and is a Currency object that has the same value. null parameter returns false.

Parameters:
quant - Currency with which to compare
Returns:
true if Currencies are equivalent; false othewise.

compareTo

public int compareTo(Currency quant)
Compares this Currency with the specified Currency. null parameter returns 1.

Parameters:
quant - Currency object with which to compare.
Returns:
0 if they are equal. 1 if this Currency is larger. -1 if this Currency is smaller.

greaterThan

public boolean greaterThan(Currency quant)
Compares this Currency to the specified Currency.

Parameters:
quant - Currency object with which to compare.
Returns:
true if this is greater than quant; false otherwise

lessThan

public boolean lessThan(Currency quant)
Compares this Currency to the specified Currency.

Parameters:
quant - Currency object with which to compare.
Returns:
true if this is less than quant; false otherwise

areEqual

public static boolean areEqual(Currency quant1,
                               Currency quant2)
Determines if two Currency objects passed to the fuction are equal. Two null are considered equal.

Returns:
true if equal, false otherwise

add

public Currency add(Currency quant)
Adds this Currency to the supplied Currency. null parameter is considered equal to Currency with ZERO quantity.

Parameters:
quant - Currency object to add
Returns:
Currency object resulting from addition operation

add

public static Currency add(Currency a,
                           Currency b)
Adds supplied Currency to other supplied Currency. null parameter(s) is considered to be equal to ZERO quantity.

Parameters:
a - first Currency object
b - second Currency object
Returns:
Currency object resulting from addition of a and b in Currency a's unit

subtract

public Currency subtract(Currency quant)
Subtracts supplied Currency from this Currency. null parameter is considered equal to Currency with zero quantity

Parameters:
quant - Currency object to subtract
Returns:
Currency object resulting from subtraction operation in this unit

divide

public java.math.BigDecimal divide(Currency quant)
Divides this Currency by supplied Currency.

Parameters:
quant - Currency object to divide by
Returns:
BigDecimal object resulting from division operation
Throws:
ArithmeticException - on the attempt to divide by null or zero

multiply

public Currency multiply(java.math.BigDecimal multiplier)
Multiplies this Currency by supplied BigDecimal.

Returns:
Currency object resulting from multiplication operation
Throws:
ArithmeticException - on the attempt to multiply by null

divide

public Currency divide(java.math.BigDecimal divisor)
Divides this Currency by supplied BigDecimal.

Returns:
Currency object resulting from division operation
Throws:
ArithmeticException - on the attempt to divide by null or zero