task1/solution12/src/org/apidesign/apifest08/currency/Convertor.java
changeset 20 7ca97f802b5a
parent 6 97662396c0fd
     1.1 --- a/task1/solution12/src/org/apidesign/apifest08/currency/Convertor.java	Sun Sep 28 14:12:38 2008 +0200
     1.2 +++ b/task1/solution12/src/org/apidesign/apifest08/currency/Convertor.java	Tue Sep 30 12:04:26 2008 +0200
     1.3 @@ -1,6 +1,11 @@
     1.4  package org.apidesign.apifest08.currency;
     1.5  
     1.6  import java.util.Currency;
     1.7 +import java.util.Hashtable;
     1.8 +
     1.9 +import org.apidesign.apifest08.currency.exceptions.ConvertorException;
    1.10 +import org.apidesign.apifest08.currency.exceptions.InvalidCurrencyException;
    1.11 +import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
    1.12  
    1.13  /**
    1.14   * This is the skeleton class for your API. You need to make it public, so it is accessible to your client code
    1.15 @@ -11,12 +16,56 @@
    1.16   */
    1.17  public class Convertor {
    1.18  
    1.19 -  private Currency currency1;
    1.20 -  private Currency currency2;
    1.21 +  private static Hashtable<String, ExchangeRate> exchangeRates;
    1.22  
    1.23 -  private Convertor(Currency currency1, Currency currency2) {
    1.24 -    this.currency1 = currency1;
    1.25 -    this.currency2 = currency2;
    1.26 +  private ExchangeRate exchangeRate12;
    1.27 +  private ExchangeRate exchangeRate21;
    1.28 +
    1.29 +  private Convertor(Currency currency1, Currency currency2) throws UnknownConvertorException {
    1.30 +    String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode();
    1.31 +    String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode();
    1.32 +
    1.33 +    if (!(exchangeRates.containsKey(key12) && exchangeRates.containsKey(key21))) {
    1.34 +      throw new UnknownConvertorException("Selected convertor (" + currency1.getCurrencyCode() + "->"
    1.35 +          + currency2.getCurrencyCode() + ") has not defined any rates!!!");
    1.36 +    }
    1.37 +
    1.38 +    this.exchangeRate12 = exchangeRates.get(key12);
    1.39 +    this.exchangeRate21 = exchangeRates.get(key21);
    1.40 +  }
    1.41 +
    1.42 +  /**
    1.43 +   * Sets convertor rate for selected currencies.
    1.44 +   * @param currency1
    1.45 +   *          one of the currencies we want to convert to/from
    1.46 +   * @param currency2
    1.47 +   *          the other currency
    1.48 +   * @param rate
    1.49 +   *          exchange rate from currency1 to currency2
    1.50 +   * @param unit
    1.51 +   *          unit of exchangeRate (USD->CZK - unit=1, you exchange one dollar, SKK->CZK unit=100, exchange rate is for
    1.52 +   *          100SKK)
    1.53 +   */
    1.54 +  public static void setConvertorRates(Currency currency1, Currency currency2, double rate, double unit) {
    1.55 +    if (currency1 == null || currency2 == null) {
    1.56 +      throw new ConvertorException("None of the currencies should be null!!!");
    1.57 +    }
    1.58 +
    1.59 +    if (rate <= 0 || unit <= 0) {
    1.60 +      throw new ConvertorException("Rate(" + rate + ") and unit(" + unit + ") has to be grater then zero!!!");
    1.61 +    }
    1.62 +
    1.63 +    if (exchangeRates == null) {
    1.64 +      exchangeRates = new Hashtable<String, ExchangeRate>();
    1.65 +    }
    1.66 +
    1.67 +    String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode();
    1.68 +    String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode();
    1.69 +    double recountedRate = (unit / rate) * unit;
    1.70 +
    1.71 +    exchangeRates.put(key12, new ExchangeRate(currency1, currency2, rate, unit));
    1.72 +    exchangeRates.put(key21, new ExchangeRate(currency2, currency1, recountedRate, unit));
    1.73 +
    1.74    }
    1.75  
    1.76    /**
    1.77 @@ -26,10 +75,13 @@
    1.78     * @param currency2
    1.79     *          the other currency
    1.80     * @return new instance of convertor
    1.81 +   * @throws UnknownConvertorException
    1.82 +   *           thrown if convertor for selected currencies has not been defined
    1.83     */
    1.84 -  public static Convertor getConvertorInstance(Currency currency1, Currency currency2) {
    1.85 -    if (currency1 == null || currency2 == null)
    1.86 +  public static Convertor getConvertorInstance(Currency currency1, Currency currency2) throws UnknownConvertorException {
    1.87 +    if (currency1 == null || currency2 == null) {
    1.88        throw new ConvertorException("None of the currencies should be null!!!");
    1.89 +    }
    1.90      return new Convertor(currency1, currency2);
    1.91    }
    1.92  
    1.93 @@ -37,30 +89,32 @@
    1.94     * Converts selected amout of selected currency to other currency of this convertor instance.
    1.95     * @param amount
    1.96     *          amount to convert
    1.97 -   * @param actualCurrency
    1.98 +   * @param originalCurrency
    1.99     *          currency of this amount
   1.100 +   * @param newCurrency
   1.101 +   *          currency to which we want convert
   1.102     * @return converted amount
   1.103 +   * @throws InvalidCurrencyException
   1.104 +   *           while one or both currencies doesn't fit for this convertor
   1.105     */
   1.106 -  public double convert(double amount, Currency actualCurrency) {
   1.107 -    ExchangeRate exchangeRate = null;
   1.108 +  public double convert(double amount, Currency originalCurrency, Currency newCurrency) throws InvalidCurrencyException {
   1.109 +    ExchangeRate actualyUsedExchangeRate = null;
   1.110  
   1.111 -    if (actualCurrency == null) {
   1.112 -      throw new ConvertorException("Selected currency is null!!!");
   1.113 +    if (originalCurrency == null) {
   1.114 +      throw new ConvertorException("Original currency is null!!!");
   1.115      }
   1.116  
   1.117 -    if (currency1.getSymbol().equals(actualCurrency.getSymbol())
   1.118 -        || currency2.getSymbol().equals(actualCurrency.getSymbol())) {
   1.119 -      exchangeRate = getExchangeRate(actualCurrency);
   1.120 -    } else {
   1.121 -      throw new ConvertorException("This convertor could not be used for selected currency("
   1.122 -          + actualCurrency.getCurrencyCode() + ") " + this + "!!!");
   1.123 +    if (newCurrency == null) {
   1.124 +      throw new ConvertorException("Destination currency is null!!!");
   1.125      }
   1.126  
   1.127 -    return countResult(exchangeRate, amount);
   1.128 +    actualyUsedExchangeRate = getExchangeRate(originalCurrency, newCurrency);
   1.129 +
   1.130 +    return countResult(actualyUsedExchangeRate, amount);
   1.131    }
   1.132  
   1.133 -  private double countResult(ExchangeRate exchangeRate, double amount) {
   1.134 -    return amount * exchangeRate.getRate() / exchangeRate.getUnit();
   1.135 +  private double countResult(ExchangeRate actualyUsedExchangeRate, double amount) {
   1.136 +    return amount * actualyUsedExchangeRate.getRate() / actualyUsedExchangeRate.getUnit();
   1.137    }
   1.138  
   1.139    /**
   1.140 @@ -69,42 +123,27 @@
   1.141     *          actual currency we want to convert
   1.142     * @return actual exchange rate of this convertor for selected currency
   1.143     */
   1.144 -  private ExchangeRate getExchangeRate(Currency actualCurrency) {
   1.145 -    ExchangeRate exchangeRate = null;
   1.146 +  private ExchangeRate getExchangeRate(Currency originalCurrency, Currency newCurrency) throws InvalidCurrencyException {
   1.147 +    ExchangeRate actualyUsedExchangeRate = null;
   1.148  
   1.149 -    if (actualCurrency.getCurrencyCode().equals(currency1.getCurrencyCode())) {
   1.150 -      exchangeRate = getExchangeRateInstance(actualCurrency, currency2);
   1.151 -    } else if (actualCurrency.getCurrencyCode().equals(currency2.getCurrencyCode())) {
   1.152 -      exchangeRate = getExchangeRateInstance(actualCurrency, currency1);
   1.153 +    if (originalCurrency.getCurrencyCode().equals(exchangeRate12.getOriginalCurrency().getCurrencyCode())
   1.154 +        && newCurrency.getCurrencyCode().equals(exchangeRate12.getNewCurrency().getCurrencyCode())) {
   1.155 +      actualyUsedExchangeRate = exchangeRate12;
   1.156 +    } else if (originalCurrency.getCurrencyCode().equals(exchangeRate21.getOriginalCurrency().getCurrencyCode())
   1.157 +        && newCurrency.getCurrencyCode().equals(exchangeRate21.getNewCurrency().getCurrencyCode())) {
   1.158 +      actualyUsedExchangeRate = exchangeRate21;
   1.159 +    } else {
   1.160 +      throw new InvalidCurrencyException("This convertor " + this
   1.161 +          + " could not be used for converting selected currencies (" + originalCurrency.getCurrencyCode() + "->"
   1.162 +          + newCurrency.getCurrencyCode() + ") !!!");
   1.163      }
   1.164  
   1.165 -    return exchangeRate;
   1.166 -  }
   1.167 -
   1.168 -  /**
   1.169 -   * Returns actual exchange rate between original and destination currency.
   1.170 -   * @param originalCurrency
   1.171 -   *          original currency, from which we want to convert
   1.172 -   * @param newCurrency
   1.173 -   *          destination currency, to which we want to convert
   1.174 -   * @return exchange rate between this two currencies
   1.175 -   */
   1.176 -  private ExchangeRate getExchangeRateInstance(Currency originalCurrency, Currency newCurrency) {
   1.177 -    if ("USD".equals(originalCurrency.getCurrencyCode()) && "CZK".equals(newCurrency.getCurrencyCode())) {
   1.178 -      return new ExchangeRate(originalCurrency, newCurrency, 17d, 1d);
   1.179 -    } else if ("CZK".equals(originalCurrency.getCurrencyCode()) && "USD".equals(newCurrency.getCurrencyCode())) {
   1.180 -      return new ExchangeRate(originalCurrency, newCurrency, 1d / 17d, 1d);
   1.181 -    } else if ("SKK".equals(originalCurrency.getCurrencyCode()) && "CZK".equals(newCurrency.getCurrencyCode())) {
   1.182 -      return new ExchangeRate(originalCurrency, newCurrency, 80d, 100d);
   1.183 -    } else if ("CZK".equals(originalCurrency.getCurrencyCode()) && "SKK".equals(newCurrency.getCurrencyCode())) {
   1.184 -      return new ExchangeRate(originalCurrency, newCurrency, 100d / (80d / 100d), 100d);
   1.185 -    } else {
   1.186 -      throw new ConvertorException("Defined currencies don't have secified rates ["
   1.187 -          + originalCurrency.getCurrencyCode() + ", " + newCurrency.getCurrencyCode() + "]!!!");
   1.188 -    }
   1.189 +    return actualyUsedExchangeRate;
   1.190    }
   1.191  
   1.192    public String toString() {
   1.193 -    return "Converter [" + currency1.getCurrencyCode() + ", " + currency2.getCurrencyCode() + "]";
   1.194 +    String currency1Code = exchangeRate12.getOriginalCurrency().getCurrencyCode();
   1.195 +    String currency2Code = exchangeRate12.getNewCurrency().getCurrencyCode();
   1.196 +    return "Converter [" + currency1Code + "->" + currency2Code + ", " + currency2Code + "->" + currency1Code + "]";
   1.197    }
   1.198  }