author | Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
Sat, 11 Oct 2008 23:38:46 +0200 | |
changeset 61 | 58ec6da75f6f |
parent 55 | task3/solution04/src/org/apidesign/apifest08/currency/ExchangeRate.java@14e78f48ac2b |
child 69 | 420baec87dc5 |
permissions | -rw-r--r-- |
japod@55 | 1 |
package org.apidesign.apifest08.currency; |
japod@55 | 2 |
|
japod@55 | 3 |
|
japod@55 | 4 |
import java.math.BigDecimal; |
japod@55 | 5 |
import java.util.Currency; |
japod@55 | 6 |
|
japod@55 | 7 |
|
japod@55 | 8 |
/** |
japod@55 | 9 |
* The exchange rate between two currencies. |
japod@55 | 10 |
* |
japod@55 | 11 |
* @author D'Arcy Smith |
japod@55 | 12 |
* @version 1.0 |
japod@55 | 13 |
*/ |
japod@55 | 14 |
public final class ExchangeRate |
japod@55 | 15 |
{ |
japod@55 | 16 |
/** |
japod@55 | 17 |
* |
japod@55 | 18 |
*/ |
japod@55 | 19 |
private final Currency currencyA; |
japod@55 | 20 |
|
japod@55 | 21 |
/** |
japod@55 | 22 |
* |
japod@55 | 23 |
*/ |
japod@55 | 24 |
private final Currency currencyB; |
japod@55 | 25 |
|
japod@55 | 26 |
/** |
japod@55 | 27 |
* |
japod@55 | 28 |
*/ |
japod@55 | 29 |
private final BigDecimal rateAtoB; |
japod@55 | 30 |
|
japod@55 | 31 |
/** |
japod@55 | 32 |
* |
japod@55 | 33 |
*/ |
japod@55 | 34 |
private final BigDecimal rateBtoA; |
japod@55 | 35 |
|
japod@55 | 36 |
/** |
japod@55 | 37 |
* Construct an ExchangeRate with the specified values. |
japod@55 | 38 |
* |
japod@55 | 39 |
* @param a the first currency |
japod@55 | 40 |
* @param b the second currency |
japod@55 | 41 |
* @param ra the rate to convert a to b |
japod@55 | 42 |
* @param rb the rate to covertt b to a |
japod@55 | 43 |
* @throws IllegalArgumentException if any parameter is null. |
japod@55 | 44 |
*/ |
japod@55 | 45 |
public ExchangeRate(final Currency a, |
japod@55 | 46 |
final Currency b, |
japod@55 | 47 |
final BigDecimal ra, |
japod@55 | 48 |
final BigDecimal rb) |
japod@55 | 49 |
{ |
japod@55 | 50 |
if(a == null) |
japod@55 | 51 |
{ |
japod@55 | 52 |
throw new IllegalArgumentException("a cannot be null"); |
japod@55 | 53 |
} |
japod@55 | 54 |
|
japod@55 | 55 |
if(b == null) |
japod@55 | 56 |
{ |
japod@55 | 57 |
throw new IllegalArgumentException("b cannot be null"); |
japod@55 | 58 |
} |
japod@55 | 59 |
|
japod@55 | 60 |
if(ra == null) |
japod@55 | 61 |
{ |
japod@55 | 62 |
throw new IllegalArgumentException("ra cannot be null"); |
japod@55 | 63 |
} |
japod@55 | 64 |
|
japod@55 | 65 |
if(rb == null) |
japod@55 | 66 |
{ |
japod@55 | 67 |
throw new IllegalArgumentException("rb cannot be null"); |
japod@55 | 68 |
} |
japod@55 | 69 |
|
japod@55 | 70 |
if(ra.compareTo(BigDecimal.ZERO) <= 0) |
japod@55 | 71 |
{ |
japod@55 | 72 |
throw new IllegalArgumentException("ra cannot be <= 0, was: " + ra); |
japod@55 | 73 |
} |
japod@55 | 74 |
|
japod@55 | 75 |
if(rb.compareTo(BigDecimal.ZERO) <= 0) |
japod@55 | 76 |
{ |
japod@55 | 77 |
throw new IllegalArgumentException("rb cannot be <= 0, was: " + ra); |
japod@55 | 78 |
} |
japod@55 | 79 |
|
japod@55 | 80 |
currencyA = a; |
japod@55 | 81 |
currencyB = b; |
japod@55 | 82 |
rateAtoB = ra; |
japod@55 | 83 |
rateBtoA = rb; |
japod@55 | 84 |
} |
japod@55 | 85 |
|
japod@55 | 86 |
/** |
japod@55 | 87 |
* Get the first currency. |
japod@55 | 88 |
* |
japod@55 | 89 |
* @return the first currency. |
japod@55 | 90 |
*/ |
japod@55 | 91 |
public Currency getCurrencyA() |
japod@55 | 92 |
{ |
japod@55 | 93 |
return currencyA; |
japod@55 | 94 |
} |
japod@55 | 95 |
|
japod@55 | 96 |
/** |
japod@55 | 97 |
* Get the second currency. |
japod@55 | 98 |
* |
japod@55 | 99 |
* @return the second currency. |
japod@55 | 100 |
*/ |
japod@55 | 101 |
public Currency getCurrencyB() |
japod@55 | 102 |
{ |
japod@55 | 103 |
return currencyB; |
japod@55 | 104 |
} |
japod@55 | 105 |
|
japod@55 | 106 |
/** |
japod@55 | 107 |
* Get the conversion rate from currencyA to currencyB. |
japod@55 | 108 |
* |
japod@55 | 109 |
* @return the conversion rate from currencyA to currencyB. |
japod@55 | 110 |
*/ |
japod@55 | 111 |
public BigDecimal getRateAtoB() |
japod@55 | 112 |
{ |
japod@55 | 113 |
return rateAtoB; |
japod@55 | 114 |
} |
japod@55 | 115 |
|
japod@55 | 116 |
/** |
japod@55 | 117 |
* Get the conversion rate from currencyB to currencyA. |
japod@55 | 118 |
* |
japod@55 | 119 |
* @return the conversion rate from currencyB to currencyA. |
japod@55 | 120 |
*/ |
japod@55 | 121 |
public BigDecimal getRateBtoA() |
japod@55 | 122 |
{ |
japod@55 | 123 |
return rateBtoA; |
japod@55 | 124 |
} |
japod@55 | 125 |
} |