author | japod@localhost |
Sun, 28 Sep 2008 14:12:38 +0200 | |
changeset 6 | 97662396c0fd |
child 17 | 37c9921c653e |
permissions | -rw-r--r-- |
japod@6 | 1 |
package org.apidesign.apifest08.currency; |
japod@6 | 2 |
|
japod@6 | 3 |
|
japod@6 | 4 |
import java.math.BigDecimal; |
japod@6 | 5 |
import java.math.MathContext; |
japod@6 | 6 |
import java.math.RoundingMode; |
japod@6 | 7 |
import java.util.Currency; |
japod@6 | 8 |
|
japod@6 | 9 |
|
japod@6 | 10 |
/** |
japod@6 | 11 |
* Convert between two currencies. |
japod@6 | 12 |
* |
japod@6 | 13 |
* @author D'Arcy Smith |
japod@6 | 14 |
* @version 1.0 |
japod@6 | 15 |
*/ |
japod@6 | 16 |
public final class Convertor |
japod@6 | 17 |
{ |
japod@6 | 18 |
/** |
japod@6 | 19 |
* The currency to cvonvert from. |
japod@6 | 20 |
*/ |
japod@6 | 21 |
private final Currency currencyA; |
japod@6 | 22 |
|
japod@6 | 23 |
/** |
japod@6 | 24 |
* The currency to cvonvert from. |
japod@6 | 25 |
*/ |
japod@6 | 26 |
private final Currency currencyB; |
japod@6 | 27 |
|
japod@6 | 28 |
/** |
japod@6 | 29 |
* Constructs a convertor with the specified currencies. |
japod@6 | 30 |
* |
japod@6 | 31 |
* @param a the currency to convert from. |
japod@6 | 32 |
* @param b the currency to convert to. |
japod@6 | 33 |
* @throws IllegalArgumentException if either a or b are null. |
japod@6 | 34 |
*/ |
japod@6 | 35 |
public Convertor(final Currency a, |
japod@6 | 36 |
final Currency b) |
japod@6 | 37 |
{ |
japod@6 | 38 |
if(a == null) |
japod@6 | 39 |
{ |
japod@6 | 40 |
throw new IllegalArgumentException("a cannot be null"); |
japod@6 | 41 |
} |
japod@6 | 42 |
|
japod@6 | 43 |
if(b == null) |
japod@6 | 44 |
{ |
japod@6 | 45 |
throw new IllegalArgumentException("a cannot be null"); |
japod@6 | 46 |
} |
japod@6 | 47 |
|
japod@6 | 48 |
currencyA = a; |
japod@6 | 49 |
currencyB = b; |
japod@6 | 50 |
} |
japod@6 | 51 |
|
japod@6 | 52 |
/** |
japod@6 | 53 |
* Convert from currency "b" to currency "a". |
japod@6 | 54 |
* |
japod@6 | 55 |
* @param amount the amount to convert. |
japod@6 | 56 |
* @return the converted amount. |
japod@6 | 57 |
* @throws IllegalArgumentException if amount is null. |
japod@6 | 58 |
*/ |
japod@6 | 59 |
public BigDecimal convertFrom(final BigDecimal amount) |
japod@6 | 60 |
{ |
japod@6 | 61 |
final BigDecimal aInUSD; |
japod@6 | 62 |
final BigDecimal bInUSD; |
japod@6 | 63 |
final BigDecimal temp; |
japod@6 | 64 |
final BigDecimal result; |
japod@6 | 65 |
|
japod@6 | 66 |
if(amount == null) |
japod@6 | 67 |
{ |
japod@6 | 68 |
throw new IllegalArgumentException("amount cannot be null"); |
japod@6 | 69 |
} |
japod@6 | 70 |
|
japod@6 | 71 |
aInUSD = CurrencyValues.getValue(currencyA); |
japod@6 | 72 |
bInUSD = CurrencyValues.getValue(currencyB); |
japod@6 | 73 |
temp = amount.divide(bInUSD, MathContext.DECIMAL32); |
japod@6 | 74 |
result = temp.multiply(aInUSD); |
japod@6 | 75 |
|
japod@6 | 76 |
return (result.setScale(2, RoundingMode.HALF_DOWN)); |
japod@6 | 77 |
} |
japod@6 | 78 |
|
japod@6 | 79 |
/** |
japod@6 | 80 |
* Convert from currency "a" to currency "b". |
japod@6 | 81 |
* |
japod@6 | 82 |
* @param amount the amount to convert. |
japod@6 | 83 |
* @return the converted amount. |
japod@6 | 84 |
* @throws IllegalArgumentException if amount is null. |
japod@6 | 85 |
*/ |
japod@6 | 86 |
public BigDecimal convertTo(final BigDecimal amount) |
japod@6 | 87 |
{ |
japod@6 | 88 |
final BigDecimal aInUSD; |
japod@6 | 89 |
final BigDecimal bInUSD; |
japod@6 | 90 |
final BigDecimal temp; |
japod@6 | 91 |
final BigDecimal result; |
japod@6 | 92 |
|
japod@6 | 93 |
if(amount == null) |
japod@6 | 94 |
{ |
japod@6 | 95 |
throw new IllegalArgumentException("amount cannot be null"); |
japod@6 | 96 |
} |
japod@6 | 97 |
|
japod@6 | 98 |
aInUSD = CurrencyValues.getValue(currencyA); |
japod@6 | 99 |
bInUSD = CurrencyValues.getValue(currencyB); |
japod@6 | 100 |
temp = amount.divide(aInUSD, MathContext.DECIMAL32); |
japod@6 | 101 |
result = temp.multiply(bInUSD); |
japod@6 | 102 |
|
japod@6 | 103 |
return (result.setScale(2, RoundingMode.HALF_DOWN)); |
japod@6 | 104 |
} |
japod@6 | 105 |
} |