task4/solution04/src/org/apidesign/apifest08/currency/ConverterImpl.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 25 Oct 2008 20:53:00 +0200
changeset 84 2ae6e4aa7aef
parent 61 58ec6da75f6f
permissions -rw-r--r--
Solutions by Petr Smid
     1 package org.apidesign.apifest08.currency;
     2 
     3 
     4 import java.math.BigDecimal;
     5 import java.util.Currency;
     6 import java.util.Set;
     7 
     8 
     9 /**
    10  * Convert between two currencies.
    11  *
    12  * @author D'Arcy Smith
    13  * @version 1.1
    14  */
    15 class ConvertorImpl
    16     implements ExchangeRateConvertor
    17 {
    18     /**
    19      */
    20     private final ExchangeRate rate;
    21 
    22     /**
    23      * Constructs a convertor with the specified currencies.
    24      * 
    25      * @param a the currency to convert from.
    26      * @param aRate the exchage rage between from and to.
    27      * @param b the currency to convert to.
    28      * @param bRate the exchage rage between to and from.
    29      * @throws IllegalArgumentException if either any of the arguments are null or if either rate <= 0.
    30      */
    31     public ConvertorImpl(final ExchangeRate r)
    32     {
    33         if(r == null)
    34         {
    35             throw new IllegalArgumentException("r cannot be null");
    36         }
    37 
    38         rate = r;
    39     }
    40     
    41     /**
    42      * Convert an amount from one currency to another.
    43      * 
    44      * @param from the currency to convert from.
    45      * @param to the currency to convert to.
    46      * @param amount the amount to convert.
    47      * @return the converted amount.
    48      * @throws IllegalArgumentException if any of the arguments are null.
    49      * @throws InvalidConversionException if either from or to are not equal to the currencies passed to the constructor.
    50      */
    51     public BigDecimal convert(final Currency   from,
    52                               final Currency   to,
    53                               final BigDecimal amount)
    54         throws InvalidConversionException
    55     {
    56         return (rate.convert(from, to, amount));
    57     }
    58 
    59     /**
    60      * Check to see if converting between the two currencies is possible.
    61      * 
    62      * @param from the currency to convert from.
    63      * @param to the currency to convert to.
    64      * @return true if the conversion is possible.
    65      * @throws IllegalArgumentException if either from or to are null.
    66      */
    67     public boolean canConvert(final Currency from, final Currency to)
    68     {
    69         return (rate.canConvert(from, to));
    70     }
    71 
    72     /**
    73      * Get the currencies that the convertor supports.
    74      * 
    75      * @return the supported currencies.
    76      */
    77     public Set<Currency> getCurrencies()
    78     {
    79         return (rate.getCurrencies());
    80     }
    81 
    82     /**
    83      * Get the conversion rate between two currencies.
    84      * 
    85      * @param from the currency to convert from.
    86      * @param to the currency to convert to.
    87      * @return the conversion rate between the two currencies.
    88      * @throws InvalidConversionException if canConvert would return false.
    89      * @throws IllegalArgumentException if either from or to are null.
    90      */
    91     public BigDecimal getConversionRate(final Currency from, 
    92                                         final Currency to)
    93         throws InvalidConversionException
    94     {                
    95         return (rate.getConversionRate(from, to));
    96     }
    97 
    98     /**
    99      * Check to see if two ConvertorImpls are equal.
   100      *
   101      * @param obj the object to check
   102      * @return if the ConvertorImpls are not the same (cuyrrencies and rates).
   103      */
   104     @Override
   105     public boolean equals(Object obj)
   106     {
   107         if (obj == null)
   108         {
   109             return false;
   110         }
   111         
   112         if (getClass() != obj.getClass())
   113         {
   114             return false;
   115         }
   116 
   117         final ConvertorImpl other = (ConvertorImpl) obj;
   118 
   119         return (rate.equals(other.rate));
   120     }
   121 
   122     /**
   123      * Get the hashCode of the Convertor.
   124      *
   125      * @return the hashCode of the convertor.
   126      */
   127     @Override
   128     public int hashCode()
   129     {
   130         return (rate.hashCode());
   131     }
   132 
   133     /**
   134      * Get the currencyCode of both currencies.
   135      *
   136      * @return the currency codes of both currencies.
   137      */
   138     @Override
   139     public String toString()
   140     {
   141         return (rate.getCurrencyA().getCurrencyCode() + " to " + rate.getCurrencyB().getCurrencyCode());
   142     }
   143 
   144     public ExchangeRate getExchangeRate()
   145     {
   146         return (rate);
   147     }
   148 }