task1/solution12/src/org/apidesign/apifest08/currency/Convertor.java
author japod@localhost
Sun, 28 Sep 2008 14:12:38 +0200
changeset 6 97662396c0fd
child 20 7ca97f802b5a
permissions -rw-r--r--
Adding solutions received for task1
     1 package org.apidesign.apifest08.currency;
     2 
     3 import java.util.Currency;
     4 
     5 /**
     6  * This is the skeleton class for your API. You need to make it public, so it is accessible to your client code
     7  * (currently in Task1Test.java) file.
     8  * <p>
     9  * Feel free to create additional classes or rename this one, just keep all the API and its implementation in this
    10  * package. Do not spread it outside to other packages.
    11  */
    12 public class Convertor {
    13 
    14   private Currency currency1;
    15   private Currency currency2;
    16 
    17   private Convertor(Currency currency1, Currency currency2) {
    18     this.currency1 = currency1;
    19     this.currency2 = currency2;
    20   }
    21 
    22   /**
    23    * Creates new instance of convertor.
    24    * @param currency1
    25    *          one of the currencies we want to convert to/from
    26    * @param currency2
    27    *          the other currency
    28    * @return new instance of convertor
    29    */
    30   public static Convertor getConvertorInstance(Currency currency1, Currency currency2) {
    31     if (currency1 == null || currency2 == null)
    32       throw new ConvertorException("None of the currencies should be null!!!");
    33     return new Convertor(currency1, currency2);
    34   }
    35 
    36   /**
    37    * Converts selected amout of selected currency to other currency of this convertor instance.
    38    * @param amount
    39    *          amount to convert
    40    * @param actualCurrency
    41    *          currency of this amount
    42    * @return converted amount
    43    */
    44   public double convert(double amount, Currency actualCurrency) {
    45     ExchangeRate exchangeRate = null;
    46 
    47     if (actualCurrency == null) {
    48       throw new ConvertorException("Selected currency is null!!!");
    49     }
    50 
    51     if (currency1.getSymbol().equals(actualCurrency.getSymbol())
    52         || currency2.getSymbol().equals(actualCurrency.getSymbol())) {
    53       exchangeRate = getExchangeRate(actualCurrency);
    54     } else {
    55       throw new ConvertorException("This convertor could not be used for selected currency("
    56           + actualCurrency.getCurrencyCode() + ") " + this + "!!!");
    57     }
    58 
    59     return countResult(exchangeRate, amount);
    60   }
    61 
    62   private double countResult(ExchangeRate exchangeRate, double amount) {
    63     return amount * exchangeRate.getRate() / exchangeRate.getUnit();
    64   }
    65 
    66   /**
    67    * Decides the direction of conversion and returns instance of actual exchange rate.
    68    * @param actualCurrency
    69    *          actual currency we want to convert
    70    * @return actual exchange rate of this convertor for selected currency
    71    */
    72   private ExchangeRate getExchangeRate(Currency actualCurrency) {
    73     ExchangeRate exchangeRate = null;
    74 
    75     if (actualCurrency.getCurrencyCode().equals(currency1.getCurrencyCode())) {
    76       exchangeRate = getExchangeRateInstance(actualCurrency, currency2);
    77     } else if (actualCurrency.getCurrencyCode().equals(currency2.getCurrencyCode())) {
    78       exchangeRate = getExchangeRateInstance(actualCurrency, currency1);
    79     }
    80 
    81     return exchangeRate;
    82   }
    83 
    84   /**
    85    * Returns actual exchange rate between original and destination currency.
    86    * @param originalCurrency
    87    *          original currency, from which we want to convert
    88    * @param newCurrency
    89    *          destination currency, to which we want to convert
    90    * @return exchange rate between this two currencies
    91    */
    92   private ExchangeRate getExchangeRateInstance(Currency originalCurrency, Currency newCurrency) {
    93     if ("USD".equals(originalCurrency.getCurrencyCode()) && "CZK".equals(newCurrency.getCurrencyCode())) {
    94       return new ExchangeRate(originalCurrency, newCurrency, 17d, 1d);
    95     } else if ("CZK".equals(originalCurrency.getCurrencyCode()) && "USD".equals(newCurrency.getCurrencyCode())) {
    96       return new ExchangeRate(originalCurrency, newCurrency, 1d / 17d, 1d);
    97     } else if ("SKK".equals(originalCurrency.getCurrencyCode()) && "CZK".equals(newCurrency.getCurrencyCode())) {
    98       return new ExchangeRate(originalCurrency, newCurrency, 80d, 100d);
    99     } else if ("CZK".equals(originalCurrency.getCurrencyCode()) && "SKK".equals(newCurrency.getCurrencyCode())) {
   100       return new ExchangeRate(originalCurrency, newCurrency, 100d / (80d / 100d), 100d);
   101     } else {
   102       throw new ConvertorException("Defined currencies don't have secified rates ["
   103           + originalCurrency.getCurrencyCode() + ", " + newCurrency.getCurrencyCode() + "]!!!");
   104     }
   105   }
   106 
   107   public String toString() {
   108     return "Converter [" + currency1.getCurrencyCode() + ", " + currency2.getCurrencyCode() + "]";
   109   }
   110 }