task2/solution12/src/org/apidesign/apifest08/currency/Convertor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 01 Oct 2008 10:43:05 +0200
changeset 29 f6073056b9fe
parent 20 task1/solution12/src/org/apidesign/apifest08/currency/Convertor.java@7ca97f802b5a
child 33 80f2d8751f35
permissions -rw-r--r--
Getting ready for task2: copying all solutions to new locations
     1 package org.apidesign.apifest08.currency;
     2 
     3 import java.util.Currency;
     4 import java.util.Hashtable;
     5 
     6 import org.apidesign.apifest08.currency.exceptions.ConvertorException;
     7 import org.apidesign.apifest08.currency.exceptions.InvalidCurrencyException;
     8 import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
     9 
    10 /**
    11  * This is the skeleton class for your API. You need to make it public, so it is accessible to your client code
    12  * (currently in Task1Test.java) file.
    13  * <p>
    14  * Feel free to create additional classes or rename this one, just keep all the API and its implementation in this
    15  * package. Do not spread it outside to other packages.
    16  */
    17 public class Convertor {
    18 
    19   private static Hashtable<String, ExchangeRate> exchangeRates;
    20 
    21   private ExchangeRate exchangeRate12;
    22   private ExchangeRate exchangeRate21;
    23 
    24   private Convertor(Currency currency1, Currency currency2) throws UnknownConvertorException {
    25     String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode();
    26     String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode();
    27 
    28     if (!(exchangeRates.containsKey(key12) && exchangeRates.containsKey(key21))) {
    29       throw new UnknownConvertorException("Selected convertor (" + currency1.getCurrencyCode() + "->"
    30           + currency2.getCurrencyCode() + ") has not defined any rates!!!");
    31     }
    32 
    33     this.exchangeRate12 = exchangeRates.get(key12);
    34     this.exchangeRate21 = exchangeRates.get(key21);
    35   }
    36 
    37   /**
    38    * Sets convertor rate for selected currencies.
    39    * @param currency1
    40    *          one of the currencies we want to convert to/from
    41    * @param currency2
    42    *          the other currency
    43    * @param rate
    44    *          exchange rate from currency1 to currency2
    45    * @param unit
    46    *          unit of exchangeRate (USD->CZK - unit=1, you exchange one dollar, SKK->CZK unit=100, exchange rate is for
    47    *          100SKK)
    48    */
    49   public static void setConvertorRates(Currency currency1, Currency currency2, double rate, double unit) {
    50     if (currency1 == null || currency2 == null) {
    51       throw new ConvertorException("None of the currencies should be null!!!");
    52     }
    53 
    54     if (rate <= 0 || unit <= 0) {
    55       throw new ConvertorException("Rate(" + rate + ") and unit(" + unit + ") has to be grater then zero!!!");
    56     }
    57 
    58     if (exchangeRates == null) {
    59       exchangeRates = new Hashtable<String, ExchangeRate>();
    60     }
    61 
    62     String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode();
    63     String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode();
    64     double recountedRate = (unit / rate) * unit;
    65 
    66     exchangeRates.put(key12, new ExchangeRate(currency1, currency2, rate, unit));
    67     exchangeRates.put(key21, new ExchangeRate(currency2, currency1, recountedRate, unit));
    68 
    69   }
    70 
    71   /**
    72    * Creates new instance of convertor.
    73    * @param currency1
    74    *          one of the currencies we want to convert to/from
    75    * @param currency2
    76    *          the other currency
    77    * @return new instance of convertor
    78    * @throws UnknownConvertorException
    79    *           thrown if convertor for selected currencies has not been defined
    80    */
    81   public static Convertor getConvertorInstance(Currency currency1, Currency currency2) throws UnknownConvertorException {
    82     if (currency1 == null || currency2 == null) {
    83       throw new ConvertorException("None of the currencies should be null!!!");
    84     }
    85     return new Convertor(currency1, currency2);
    86   }
    87 
    88   /**
    89    * Converts selected amout of selected currency to other currency of this convertor instance.
    90    * @param amount
    91    *          amount to convert
    92    * @param originalCurrency
    93    *          currency of this amount
    94    * @param newCurrency
    95    *          currency to which we want convert
    96    * @return converted amount
    97    * @throws InvalidCurrencyException
    98    *           while one or both currencies doesn't fit for this convertor
    99    */
   100   public double convert(double amount, Currency originalCurrency, Currency newCurrency) throws InvalidCurrencyException {
   101     ExchangeRate actualyUsedExchangeRate = null;
   102 
   103     if (originalCurrency == null) {
   104       throw new ConvertorException("Original currency is null!!!");
   105     }
   106 
   107     if (newCurrency == null) {
   108       throw new ConvertorException("Destination currency is null!!!");
   109     }
   110 
   111     actualyUsedExchangeRate = getExchangeRate(originalCurrency, newCurrency);
   112 
   113     return countResult(actualyUsedExchangeRate, amount);
   114   }
   115 
   116   private double countResult(ExchangeRate actualyUsedExchangeRate, double amount) {
   117     return amount * actualyUsedExchangeRate.getRate() / actualyUsedExchangeRate.getUnit();
   118   }
   119 
   120   /**
   121    * Decides the direction of conversion and returns instance of actual exchange rate.
   122    * @param actualCurrency
   123    *          actual currency we want to convert
   124    * @return actual exchange rate of this convertor for selected currency
   125    */
   126   private ExchangeRate getExchangeRate(Currency originalCurrency, Currency newCurrency) throws InvalidCurrencyException {
   127     ExchangeRate actualyUsedExchangeRate = null;
   128 
   129     if (originalCurrency.getCurrencyCode().equals(exchangeRate12.getOriginalCurrency().getCurrencyCode())
   130         && newCurrency.getCurrencyCode().equals(exchangeRate12.getNewCurrency().getCurrencyCode())) {
   131       actualyUsedExchangeRate = exchangeRate12;
   132     } else if (originalCurrency.getCurrencyCode().equals(exchangeRate21.getOriginalCurrency().getCurrencyCode())
   133         && newCurrency.getCurrencyCode().equals(exchangeRate21.getNewCurrency().getCurrencyCode())) {
   134       actualyUsedExchangeRate = exchangeRate21;
   135     } else {
   136       throw new InvalidCurrencyException("This convertor " + this
   137           + " could not be used for converting selected currencies (" + originalCurrency.getCurrencyCode() + "->"
   138           + newCurrency.getCurrencyCode() + ") !!!");
   139     }
   140 
   141     return actualyUsedExchangeRate;
   142   }
   143 
   144   public String toString() {
   145     String currency1Code = exchangeRate12.getOriginalCurrency().getCurrencyCode();
   146     String currency2Code = exchangeRate12.getNewCurrency().getCurrencyCode();
   147     return "Converter [" + currency1Code + "->" + currency2Code + ", " + currency2Code + "->" + currency1Code + "]";
   148   }
   149 }