/*
** Java currency library package.
** Copyright (c) 1999 by Bruno Antunes
**
** This program is free software.
**
** You may redistribute it and/or modify it under the terms of the GNU
** General Public License as published by the Free Software Foundation.
** Version 2 of the license should be included with this distribution in
** the file LICENSE, as well as License.html. If the license is not
** included with this distribution, you may find a copy at the FSF web
** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
**
** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
** REDISTRIBUTION OF THIS SOFTWARE.
**
*/
package org.gjt.currency;
import java.math.BigDecimal;
import java.io.Serializable;
/**
* A money representation. The money is allays represented by
* an amount (the amount of money) and its currency.
* <br>
* Moneys are constant; their values cannot be changed after they are
* created.
*
* @see Currency
*
* @author Bruno Antunes
* @version 1.0 7/05/1999
*/
public class Money implements Comparable, Cloneable, Serializable {
private double amount;
private Currency currency;
/**
* A zero amount of money in the specified currency
*
* @param aCurrency the currency for this amount of money
*/
public Money(Currency aCurrency) {
this(0.0,aCurrency);
}
/**
* A specified amount of money in the specified currency
*
* @param anAmount the amount of money
* @param aCurrency the currency for this amount of money
*/
public Money(double anAmount, Currency aCurrency) {
amount = anAmount;
currency = aCurrency;
}
/**
* Converts this money to the <code>Curency</code> specified.
*
* @param toCurrency the currency unit to convert the money
* @return a new Money object in the specified unit. If the unit specified is
* the same of this <code>Money</code>, the same <code>Money</code> object is
* returned.
*
* @see Currency
*/
public Money convertTo(Currency toCurrency) {
Money result = null;
if (currency.equals(toCurrency)) {
return this;
} else {
// TOSEE: Is it necessary to make rounds?
// TOSEE: Is it ok for non Euro currencys?
double newAmount = amount / currency.getExchangeRate();
newAmount = newAmount * toCurrency.getExchangeRate();
result = new Money(newAmount, toCurrency);
}
return result;
}
/**
* Adds the money specified to this money
*
* @param money the money to be added
* @return (this + money)
*/
public Money add(Money money) {
Money toAdd = money.convertTo(currency);
double newAmount = amount + toAdd.amount;
return new Money(newAmount,currency);
}
/**
* Subtracts the money specified to this money
*
* @param money the money to be subtracted
* @return (this - money)
*/
public Money subtract(Money money) {
Money toSubtract = money.convertTo(currency);
double newAmount = amount - toSubtract.amount;
return new Money(newAmount,currency);
}
/**
* Multiplies this money with a specified factor
*
* @param factor the value os the multiplier
* @return this * factor
*/
public Money multiply(double factor) {
return new Money(amount * factor, currency);
}
/**
* Compares this money with the specified money.
*
* @param money Money to which this Money is to be compared.
* @return -1, 0 or 1 as this Money is numerically less than, equal
* to, or greater than money.
*/
public int compareTo(Money money) {
money = money.convertTo(currency);
if (amount < money.amount) {
return -1;
} else if (amount == money.amount) {
return 0;
} else {
return 1;
}
}
/**
* Compares this money with the specified object. If the object is not a
* <code>Money</code>, then a <code>ClassCastException</code> is thronw.
*
* @param obj Money to which this Money is to be compared.
* @return -1, 0 or 1 as this Money is numerically less than, equal
* to, or greater than money.
*
* @throws ClassCastException if obj is not a BigMoney.
* @see Comparable
*/
public int compareTo(Object obj) {
Money money = (Money) obj;
money = money.convertTo(currency);
if (amount < money.amount) {
return -1;
} else if (amount == money.amount) {
return 0;
} else {
return 1;
}
}
/**
* Compares the specified Object with this Money for equality.
*
* @param o the Object to be compared for equality with this Money
* @return true if the specified Object is equal to this Money
*/
public boolean equals(Object obj) {
if (obj instanceof Money) {
Money money = (Money) obj;
return currency.equals(money.currency) &&
amount == money.amount;
} else {
return false;
}
}
public double getAmount() {
return amount;
}
public Currency getCurrency() {
return currency;
}
/**
* String representation of the amount of money formatted using the
* NumberFormatter registered in the <code>CurrenyFormatManager</code>
*
* @return the formatted money
*
* @see CurrenyFormatManager
*/
public String format() {
return CurrenyFormatManager.getCurrencyFormat(currency).format(amount);
}
/** @return a String representation of this money */
public String toString() {
return amount + ";" + currency.toString();
}
public Object clone() {
return this;
}
}