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