1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task2/solution12/src/org/apidesign/apifest08/currency/Convertor.java Wed Oct 01 10:43:05 2008 +0200
1.3 @@ -0,0 +1,149 @@
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 + * (currently in Task1Test.java) file.
1.16 + * <p>
1.17 + * Feel free to create additional classes or rename this one, just keep all the API and its implementation in this
1.18 + * package. Do not spread it outside to other packages.
1.19 + */
1.20 +public class Convertor {
1.21 +
1.22 + private static Hashtable<String, ExchangeRate> exchangeRates;
1.23 +
1.24 + private ExchangeRate exchangeRate12;
1.25 + private ExchangeRate exchangeRate21;
1.26 +
1.27 + private Convertor(Currency currency1, Currency currency2) throws UnknownConvertorException {
1.28 + String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode();
1.29 + String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode();
1.30 +
1.31 + if (!(exchangeRates.containsKey(key12) && exchangeRates.containsKey(key21))) {
1.32 + throw new UnknownConvertorException("Selected convertor (" + currency1.getCurrencyCode() + "->"
1.33 + + currency2.getCurrencyCode() + ") has not defined any rates!!!");
1.34 + }
1.35 +
1.36 + this.exchangeRate12 = exchangeRates.get(key12);
1.37 + this.exchangeRate21 = exchangeRates.get(key21);
1.38 + }
1.39 +
1.40 + /**
1.41 + * Sets convertor rate for selected currencies.
1.42 + * @param currency1
1.43 + * one of the currencies we want to convert to/from
1.44 + * @param currency2
1.45 + * the other currency
1.46 + * @param rate
1.47 + * exchange rate from currency1 to currency2
1.48 + * @param unit
1.49 + * unit of exchangeRate (USD->CZK - unit=1, you exchange one dollar, SKK->CZK unit=100, exchange rate is for
1.50 + * 100SKK)
1.51 + */
1.52 + public static void setConvertorRates(Currency currency1, Currency currency2, double rate, double unit) {
1.53 + if (currency1 == null || currency2 == null) {
1.54 + throw new ConvertorException("None of the currencies should be null!!!");
1.55 + }
1.56 +
1.57 + if (rate <= 0 || unit <= 0) {
1.58 + throw new ConvertorException("Rate(" + rate + ") and unit(" + unit + ") has to be grater then zero!!!");
1.59 + }
1.60 +
1.61 + if (exchangeRates == null) {
1.62 + exchangeRates = new Hashtable<String, ExchangeRate>();
1.63 + }
1.64 +
1.65 + String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode();
1.66 + String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode();
1.67 + double recountedRate = (unit / rate) * unit;
1.68 +
1.69 + exchangeRates.put(key12, new ExchangeRate(currency1, currency2, rate, unit));
1.70 + exchangeRates.put(key21, new ExchangeRate(currency2, currency1, recountedRate, unit));
1.71 +
1.72 + }
1.73 +
1.74 + /**
1.75 + * Creates new instance of convertor.
1.76 + * @param currency1
1.77 + * one of the currencies we want to convert to/from
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) throws UnknownConvertorException {
1.85 + if (currency1 == null || currency2 == null) {
1.86 + throw new ConvertorException("None of the currencies should be null!!!");
1.87 + }
1.88 + return new Convertor(currency1, currency2);
1.89 + }
1.90 +
1.91 + /**
1.92 + * Converts selected amout of selected currency to other currency of this convertor instance.
1.93 + * @param amount
1.94 + * amount to convert
1.95 + * @param originalCurrency
1.96 + * currency of this amount
1.97 + * @param newCurrency
1.98 + * currency to which we want convert
1.99 + * @return converted amount
1.100 + * @throws InvalidCurrencyException
1.101 + * while one or both currencies doesn't fit for this convertor
1.102 + */
1.103 + public double convert(double amount, Currency originalCurrency, Currency newCurrency) throws InvalidCurrencyException {
1.104 + ExchangeRate actualyUsedExchangeRate = null;
1.105 +
1.106 + if (originalCurrency == null) {
1.107 + throw new ConvertorException("Original currency is null!!!");
1.108 + }
1.109 +
1.110 + if (newCurrency == null) {
1.111 + throw new ConvertorException("Destination currency is null!!!");
1.112 + }
1.113 +
1.114 + actualyUsedExchangeRate = getExchangeRate(originalCurrency, newCurrency);
1.115 +
1.116 + return countResult(actualyUsedExchangeRate, amount);
1.117 + }
1.118 +
1.119 + private double countResult(ExchangeRate actualyUsedExchangeRate, double amount) {
1.120 + return amount * actualyUsedExchangeRate.getRate() / actualyUsedExchangeRate.getUnit();
1.121 + }
1.122 +
1.123 + /**
1.124 + * Decides the direction of conversion and returns instance of actual exchange rate.
1.125 + * @param actualCurrency
1.126 + * actual currency we want to convert
1.127 + * @return actual exchange rate of this convertor for selected currency
1.128 + */
1.129 + private ExchangeRate getExchangeRate(Currency originalCurrency, Currency newCurrency) throws InvalidCurrencyException {
1.130 + ExchangeRate actualyUsedExchangeRate = null;
1.131 +
1.132 + if (originalCurrency.getCurrencyCode().equals(exchangeRate12.getOriginalCurrency().getCurrencyCode())
1.133 + && newCurrency.getCurrencyCode().equals(exchangeRate12.getNewCurrency().getCurrencyCode())) {
1.134 + actualyUsedExchangeRate = exchangeRate12;
1.135 + } else if (originalCurrency.getCurrencyCode().equals(exchangeRate21.getOriginalCurrency().getCurrencyCode())
1.136 + && newCurrency.getCurrencyCode().equals(exchangeRate21.getNewCurrency().getCurrencyCode())) {
1.137 + actualyUsedExchangeRate = exchangeRate21;
1.138 + } else {
1.139 + throw new InvalidCurrencyException("This convertor " + this
1.140 + + " could not be used for converting selected currencies (" + originalCurrency.getCurrencyCode() + "->"
1.141 + + newCurrency.getCurrencyCode() + ") !!!");
1.142 + }
1.143 +
1.144 + return actualyUsedExchangeRate;
1.145 + }
1.146 +
1.147 + public String toString() {
1.148 + String currency1Code = exchangeRate12.getOriginalCurrency().getCurrencyCode();
1.149 + String currency2Code = exchangeRate12.getNewCurrency().getCurrencyCode();
1.150 + return "Converter [" + currency1Code + "->" + currency2Code + ", " + currency2Code + "->" + currency1Code + "]";
1.151 + }
1.152 +}