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