japod@6
|
1 |
package org.apidesign.apifest08.currency;
|
japod@6
|
2 |
|
japod@6
|
3 |
import java.util.Currency;
|
japod@20
|
4 |
import java.util.Hashtable;
|
japod@20
|
5 |
|
japod@20
|
6 |
import org.apidesign.apifest08.currency.exceptions.ConvertorException;
|
japod@20
|
7 |
import org.apidesign.apifest08.currency.exceptions.InvalidCurrencyException;
|
japod@20
|
8 |
import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
|
japod@6
|
9 |
|
japod@6
|
10 |
/**
|
japod@6
|
11 |
* This is the skeleton class for your API. You need to make it public, so it is accessible to your client code
|
japod@6
|
12 |
* (currently in Task1Test.java) file.
|
japod@6
|
13 |
* <p>
|
japod@6
|
14 |
* Feel free to create additional classes or rename this one, just keep all the API and its implementation in this
|
japod@6
|
15 |
* package. Do not spread it outside to other packages.
|
japod@6
|
16 |
*/
|
japod@6
|
17 |
public class Convertor {
|
japod@6
|
18 |
|
japod@20
|
19 |
private static Hashtable<String, ExchangeRate> exchangeRates;
|
japod@6
|
20 |
|
japod@20
|
21 |
private ExchangeRate exchangeRate12;
|
japod@20
|
22 |
private ExchangeRate exchangeRate21;
|
japod@20
|
23 |
|
japod@20
|
24 |
private Convertor(Currency currency1, Currency currency2) throws UnknownConvertorException {
|
japod@20
|
25 |
String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode();
|
japod@20
|
26 |
String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode();
|
japod@20
|
27 |
|
japod@20
|
28 |
if (!(exchangeRates.containsKey(key12) && exchangeRates.containsKey(key21))) {
|
japod@20
|
29 |
throw new UnknownConvertorException("Selected convertor (" + currency1.getCurrencyCode() + "->"
|
japod@20
|
30 |
+ currency2.getCurrencyCode() + ") has not defined any rates!!!");
|
japod@20
|
31 |
}
|
japod@20
|
32 |
|
japod@20
|
33 |
this.exchangeRate12 = exchangeRates.get(key12);
|
japod@20
|
34 |
this.exchangeRate21 = exchangeRates.get(key21);
|
japod@20
|
35 |
}
|
japod@20
|
36 |
|
japod@20
|
37 |
/**
|
japod@20
|
38 |
* Sets convertor rate for selected currencies.
|
japod@20
|
39 |
* @param currency1
|
japod@20
|
40 |
* one of the currencies we want to convert to/from
|
japod@20
|
41 |
* @param currency2
|
japod@20
|
42 |
* the other currency
|
japod@20
|
43 |
* @param rate
|
japod@20
|
44 |
* exchange rate from currency1 to currency2
|
japod@20
|
45 |
* @param unit
|
japod@20
|
46 |
* unit of exchangeRate (USD->CZK - unit=1, you exchange one dollar, SKK->CZK unit=100, exchange rate is for
|
japod@20
|
47 |
* 100SKK)
|
japod@20
|
48 |
*/
|
japod@20
|
49 |
public static void setConvertorRates(Currency currency1, Currency currency2, double rate, double unit) {
|
japod@20
|
50 |
if (currency1 == null || currency2 == null) {
|
japod@20
|
51 |
throw new ConvertorException("None of the currencies should be null!!!");
|
japod@20
|
52 |
}
|
japod@20
|
53 |
|
japod@20
|
54 |
if (rate <= 0 || unit <= 0) {
|
japod@20
|
55 |
throw new ConvertorException("Rate(" + rate + ") and unit(" + unit + ") has to be grater then zero!!!");
|
japod@20
|
56 |
}
|
japod@20
|
57 |
|
japod@20
|
58 |
if (exchangeRates == null) {
|
japod@20
|
59 |
exchangeRates = new Hashtable<String, ExchangeRate>();
|
japod@20
|
60 |
}
|
japod@20
|
61 |
|
japod@20
|
62 |
String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode();
|
japod@20
|
63 |
String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode();
|
japod@20
|
64 |
double recountedRate = (unit / rate) * unit;
|
japod@20
|
65 |
|
japod@20
|
66 |
exchangeRates.put(key12, new ExchangeRate(currency1, currency2, rate, unit));
|
japod@20
|
67 |
exchangeRates.put(key21, new ExchangeRate(currency2, currency1, recountedRate, unit));
|
japod@20
|
68 |
|
japod@6
|
69 |
}
|
japod@6
|
70 |
|
japod@6
|
71 |
/**
|
japod@6
|
72 |
* Creates new instance of convertor.
|
japod@6
|
73 |
* @param currency1
|
japod@6
|
74 |
* one of the currencies we want to convert to/from
|
japod@6
|
75 |
* @param currency2
|
japod@6
|
76 |
* the other currency
|
japod@6
|
77 |
* @return new instance of convertor
|
japod@20
|
78 |
* @throws UnknownConvertorException
|
japod@20
|
79 |
* thrown if convertor for selected currencies has not been defined
|
japod@6
|
80 |
*/
|
japod@20
|
81 |
public static Convertor getConvertorInstance(Currency currency1, Currency currency2) throws UnknownConvertorException {
|
japod@20
|
82 |
if (currency1 == null || currency2 == null) {
|
japod@6
|
83 |
throw new ConvertorException("None of the currencies should be null!!!");
|
japod@20
|
84 |
}
|
japod@6
|
85 |
return new Convertor(currency1, currency2);
|
japod@6
|
86 |
}
|
japod@6
|
87 |
|
japod@6
|
88 |
/**
|
japod@6
|
89 |
* Converts selected amout of selected currency to other currency of this convertor instance.
|
japod@6
|
90 |
* @param amount
|
japod@6
|
91 |
* amount to convert
|
japod@20
|
92 |
* @param originalCurrency
|
japod@6
|
93 |
* currency of this amount
|
japod@20
|
94 |
* @param newCurrency
|
japod@20
|
95 |
* currency to which we want convert
|
japod@6
|
96 |
* @return converted amount
|
japod@20
|
97 |
* @throws InvalidCurrencyException
|
japod@20
|
98 |
* while one or both currencies doesn't fit for this convertor
|
japod@6
|
99 |
*/
|
japod@20
|
100 |
public double convert(double amount, Currency originalCurrency, Currency newCurrency) throws InvalidCurrencyException {
|
japod@20
|
101 |
ExchangeRate actualyUsedExchangeRate = null;
|
japod@6
|
102 |
|
japod@20
|
103 |
if (originalCurrency == null) {
|
japod@20
|
104 |
throw new ConvertorException("Original currency is null!!!");
|
japod@6
|
105 |
}
|
japod@6
|
106 |
|
japod@20
|
107 |
if (newCurrency == null) {
|
japod@20
|
108 |
throw new ConvertorException("Destination currency is null!!!");
|
japod@6
|
109 |
}
|
japod@6
|
110 |
|
japod@20
|
111 |
actualyUsedExchangeRate = getExchangeRate(originalCurrency, newCurrency);
|
japod@20
|
112 |
|
japod@20
|
113 |
return countResult(actualyUsedExchangeRate, amount);
|
japod@6
|
114 |
}
|
japod@6
|
115 |
|
japod@20
|
116 |
private double countResult(ExchangeRate actualyUsedExchangeRate, double amount) {
|
japod@20
|
117 |
return amount * actualyUsedExchangeRate.getRate() / actualyUsedExchangeRate.getUnit();
|
japod@6
|
118 |
}
|
japod@6
|
119 |
|
japod@6
|
120 |
/**
|
japod@6
|
121 |
* Decides the direction of conversion and returns instance of actual exchange rate.
|
japod@6
|
122 |
* @param actualCurrency
|
japod@6
|
123 |
* actual currency we want to convert
|
japod@6
|
124 |
* @return actual exchange rate of this convertor for selected currency
|
japod@6
|
125 |
*/
|
japod@20
|
126 |
private ExchangeRate getExchangeRate(Currency originalCurrency, Currency newCurrency) throws InvalidCurrencyException {
|
japod@20
|
127 |
ExchangeRate actualyUsedExchangeRate = null;
|
japod@6
|
128 |
|
japod@20
|
129 |
if (originalCurrency.getCurrencyCode().equals(exchangeRate12.getOriginalCurrency().getCurrencyCode())
|
japod@20
|
130 |
&& newCurrency.getCurrencyCode().equals(exchangeRate12.getNewCurrency().getCurrencyCode())) {
|
japod@20
|
131 |
actualyUsedExchangeRate = exchangeRate12;
|
japod@20
|
132 |
} else if (originalCurrency.getCurrencyCode().equals(exchangeRate21.getOriginalCurrency().getCurrencyCode())
|
japod@20
|
133 |
&& newCurrency.getCurrencyCode().equals(exchangeRate21.getNewCurrency().getCurrencyCode())) {
|
japod@20
|
134 |
actualyUsedExchangeRate = exchangeRate21;
|
japod@20
|
135 |
} else {
|
japod@20
|
136 |
throw new InvalidCurrencyException("This convertor " + this
|
japod@20
|
137 |
+ " could not be used for converting selected currencies (" + originalCurrency.getCurrencyCode() + "->"
|
japod@20
|
138 |
+ newCurrency.getCurrencyCode() + ") !!!");
|
japod@6
|
139 |
}
|
japod@6
|
140 |
|
japod@20
|
141 |
return actualyUsedExchangeRate;
|
japod@6
|
142 |
}
|
japod@6
|
143 |
|
japod@6
|
144 |
public String toString() {
|
japod@20
|
145 |
String currency1Code = exchangeRate12.getOriginalCurrency().getCurrencyCode();
|
japod@20
|
146 |
String currency2Code = exchangeRate12.getNewCurrency().getCurrencyCode();
|
japod@20
|
147 |
return "Converter [" + currency1Code + "->" + currency2Code + ", " + currency2Code + "->" + currency1Code + "]";
|
japod@6
|
148 |
}
|
japod@6
|
149 |
}
|