1 package org.apidesign.apifest08.currency;
3 import java.util.Currency;
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.
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.
12 public class Convertor {
14 private Currency currency1;
15 private Currency currency2;
17 private Convertor(Currency currency1, Currency currency2) {
18 this.currency1 = currency1;
19 this.currency2 = currency2;
23 * Creates new instance of convertor.
25 * one of the currencies we want to convert to/from
28 * @return new instance of convertor
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);
37 * Converts selected amout of selected currency to other currency of this convertor instance.
40 * @param actualCurrency
41 * currency of this amount
42 * @return converted amount
44 public double convert(double amount, Currency actualCurrency) {
45 ExchangeRate exchangeRate = null;
47 if (actualCurrency == null) {
48 throw new ConvertorException("Selected currency is null!!!");
51 if (currency1.getSymbol().equals(actualCurrency.getSymbol())
52 || currency2.getSymbol().equals(actualCurrency.getSymbol())) {
53 exchangeRate = getExchangeRate(actualCurrency);
55 throw new ConvertorException("This convertor could not be used for selected currency("
56 + actualCurrency.getCurrencyCode() + ") " + this + "!!!");
59 return countResult(exchangeRate, amount);
62 private double countResult(ExchangeRate exchangeRate, double amount) {
63 return amount * exchangeRate.getRate() / exchangeRate.getUnit();
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
72 private ExchangeRate getExchangeRate(Currency actualCurrency) {
73 ExchangeRate exchangeRate = null;
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);
85 * Returns actual exchange rate between original and destination currency.
86 * @param originalCurrency
87 * original currency, from which we want to convert
89 * destination currency, to which we want to convert
90 * @return exchange rate between this two currencies
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);
102 throw new ConvertorException("Defined currencies don't have secified rates ["
103 + originalCurrency.getCurrencyCode() + ", " + newCurrency.getCurrencyCode() + "]!!!");
107 public String toString() {
108 return "Converter [" + currency1.getCurrencyCode() + ", " + currency2.getCurrencyCode() + "]";