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