1 package org.apidesign.apifest08.currency;
3 import java.util.Currency;
4 import java.util.Hashtable;
6 import org.apidesign.apifest08.currency.exceptions.ConvertorException;
7 import org.apidesign.apifest08.currency.exceptions.InvalidCurrencyException;
8 import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
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.
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.
17 public class Convertor {
19 private static Hashtable<String, ExchangeRate> exchangeRates;
21 private ExchangeRate exchangeRate12;
22 private ExchangeRate exchangeRate21;
24 private Convertor(Currency currency1, Currency currency2) throws UnknownConvertorException {
25 String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode();
26 String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode();
28 if (!(exchangeRates.containsKey(key12) && exchangeRates.containsKey(key21))) {
29 throw new UnknownConvertorException("Selected convertor (" + currency1.getCurrencyCode() + "->"
30 + currency2.getCurrencyCode() + ") has not defined any rates!!!");
33 this.exchangeRate12 = exchangeRates.get(key12);
34 this.exchangeRate21 = exchangeRates.get(key21);
38 * Sets convertor rate for selected currencies.
40 * one of the currencies we want to convert to/from
44 * exchange rate from currency1 to currency2
46 * unit of exchangeRate (USD->CZK - unit=1, you exchange one dollar, SKK->CZK unit=100, exchange rate is for
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!!!");
54 if (rate <= 0 || unit <= 0) {
55 throw new ConvertorException("Rate(" + rate + ") and unit(" + unit + ") has to be grater then zero!!!");
58 if (exchangeRates == null) {
59 exchangeRates = new Hashtable<String, ExchangeRate>();
62 String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode();
63 String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode();
64 double recountedRate = (unit / rate) * unit;
66 exchangeRates.put(key12, new ExchangeRate(currency1, currency2, rate, unit));
67 exchangeRates.put(key21, new ExchangeRate(currency2, currency1, recountedRate, unit));
72 * Creates new instance of convertor.
74 * one of the currencies we want to convert to/from
77 * @return new instance of convertor
78 * @throws UnknownConvertorException
79 * thrown if convertor for selected currencies has not been defined
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!!!");
85 return new Convertor(currency1, currency2);
89 * Converts selected amout of selected currency to other currency of this convertor instance.
92 * @param originalCurrency
93 * currency of this amount
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
100 public double convert(double amount, Currency originalCurrency, Currency newCurrency) throws InvalidCurrencyException {
101 ExchangeRate actualyUsedExchangeRate = null;
103 if (originalCurrency == null) {
104 throw new ConvertorException("Original currency is null!!!");
107 if (newCurrency == null) {
108 throw new ConvertorException("Destination currency is null!!!");
111 actualyUsedExchangeRate = getExchangeRate(originalCurrency, newCurrency);
113 return countResult(actualyUsedExchangeRate, amount);
116 private double countResult(ExchangeRate actualyUsedExchangeRate, double amount) {
117 return amount * actualyUsedExchangeRate.getRate() / actualyUsedExchangeRate.getUnit();
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
126 private ExchangeRate getExchangeRate(Currency originalCurrency, Currency newCurrency) throws InvalidCurrencyException {
127 ExchangeRate actualyUsedExchangeRate = null;
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;
136 throw new InvalidCurrencyException("This convertor " + this
137 + " could not be used for converting selected currencies (" + originalCurrency.getCurrencyCode() + "->"
138 + newCurrency.getCurrencyCode() + ") !!!");
141 return actualyUsedExchangeRate;
144 public String toString() {
145 String currency1Code = exchangeRate12.getOriginalCurrency().getCurrencyCode();
146 String currency2Code = exchangeRate12.getNewCurrency().getCurrencyCode();
147 return "Converter [" + currency1Code + "->" + currency2Code + ", " + currency2Code + "->" + currency1Code + "]";