1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task1/solution12/src/org/apidesign/apifest08/currency/Convertor.java Sun Sep 28 14:12:38 2008 +0200
1.3 @@ -0,0 +1,110 @@
1.4 +package org.apidesign.apifest08.currency;
1.5 +
1.6 +import java.util.Currency;
1.7 +
1.8 +/**
1.9 + * This is the skeleton class for your API. You need to make it public, so it is accessible to your client code
1.10 + * (currently in Task1Test.java) file.
1.11 + * <p>
1.12 + * Feel free to create additional classes or rename this one, just keep all the API and its implementation in this
1.13 + * package. Do not spread it outside to other packages.
1.14 + */
1.15 +public class Convertor {
1.16 +
1.17 + private Currency currency1;
1.18 + private Currency currency2;
1.19 +
1.20 + private Convertor(Currency currency1, Currency currency2) {
1.21 + this.currency1 = currency1;
1.22 + this.currency2 = currency2;
1.23 + }
1.24 +
1.25 + /**
1.26 + * Creates new instance of convertor.
1.27 + * @param currency1
1.28 + * one of the currencies we want to convert to/from
1.29 + * @param currency2
1.30 + * the other currency
1.31 + * @return new instance of convertor
1.32 + */
1.33 + public static Convertor getConvertorInstance(Currency currency1, Currency currency2) {
1.34 + if (currency1 == null || currency2 == null)
1.35 + throw new ConvertorException("None of the currencies should be null!!!");
1.36 + return new Convertor(currency1, currency2);
1.37 + }
1.38 +
1.39 + /**
1.40 + * Converts selected amout of selected currency to other currency of this convertor instance.
1.41 + * @param amount
1.42 + * amount to convert
1.43 + * @param actualCurrency
1.44 + * currency of this amount
1.45 + * @return converted amount
1.46 + */
1.47 + public double convert(double amount, Currency actualCurrency) {
1.48 + ExchangeRate exchangeRate = null;
1.49 +
1.50 + if (actualCurrency == null) {
1.51 + throw new ConvertorException("Selected currency is null!!!");
1.52 + }
1.53 +
1.54 + if (currency1.getSymbol().equals(actualCurrency.getSymbol())
1.55 + || currency2.getSymbol().equals(actualCurrency.getSymbol())) {
1.56 + exchangeRate = getExchangeRate(actualCurrency);
1.57 + } else {
1.58 + throw new ConvertorException("This convertor could not be used for selected currency("
1.59 + + actualCurrency.getCurrencyCode() + ") " + this + "!!!");
1.60 + }
1.61 +
1.62 + return countResult(exchangeRate, amount);
1.63 + }
1.64 +
1.65 + private double countResult(ExchangeRate exchangeRate, double amount) {
1.66 + return amount * exchangeRate.getRate() / exchangeRate.getUnit();
1.67 + }
1.68 +
1.69 + /**
1.70 + * Decides the direction of conversion and returns instance of actual exchange rate.
1.71 + * @param actualCurrency
1.72 + * actual currency we want to convert
1.73 + * @return actual exchange rate of this convertor for selected currency
1.74 + */
1.75 + private ExchangeRate getExchangeRate(Currency actualCurrency) {
1.76 + ExchangeRate exchangeRate = null;
1.77 +
1.78 + if (actualCurrency.getCurrencyCode().equals(currency1.getCurrencyCode())) {
1.79 + exchangeRate = getExchangeRateInstance(actualCurrency, currency2);
1.80 + } else if (actualCurrency.getCurrencyCode().equals(currency2.getCurrencyCode())) {
1.81 + exchangeRate = getExchangeRateInstance(actualCurrency, currency1);
1.82 + }
1.83 +
1.84 + return exchangeRate;
1.85 + }
1.86 +
1.87 + /**
1.88 + * Returns actual exchange rate between original and destination currency.
1.89 + * @param originalCurrency
1.90 + * original currency, from which we want to convert
1.91 + * @param newCurrency
1.92 + * destination currency, to which we want to convert
1.93 + * @return exchange rate between this two currencies
1.94 + */
1.95 + private ExchangeRate getExchangeRateInstance(Currency originalCurrency, Currency newCurrency) {
1.96 + if ("USD".equals(originalCurrency.getCurrencyCode()) && "CZK".equals(newCurrency.getCurrencyCode())) {
1.97 + return new ExchangeRate(originalCurrency, newCurrency, 17d, 1d);
1.98 + } else if ("CZK".equals(originalCurrency.getCurrencyCode()) && "USD".equals(newCurrency.getCurrencyCode())) {
1.99 + return new ExchangeRate(originalCurrency, newCurrency, 1d / 17d, 1d);
1.100 + } else if ("SKK".equals(originalCurrency.getCurrencyCode()) && "CZK".equals(newCurrency.getCurrencyCode())) {
1.101 + return new ExchangeRate(originalCurrency, newCurrency, 80d, 100d);
1.102 + } else if ("CZK".equals(originalCurrency.getCurrencyCode()) && "SKK".equals(newCurrency.getCurrencyCode())) {
1.103 + return new ExchangeRate(originalCurrency, newCurrency, 100d / (80d / 100d), 100d);
1.104 + } else {
1.105 + throw new ConvertorException("Defined currencies don't have secified rates ["
1.106 + + originalCurrency.getCurrencyCode() + ", " + newCurrency.getCurrencyCode() + "]!!!");
1.107 + }
1.108 + }
1.109 +
1.110 + public String toString() {
1.111 + return "Converter [" + currency1.getCurrencyCode() + ", " + currency2.getCurrencyCode() + "]";
1.112 + }
1.113 +}