task1/solution11/src/org/apidesign/apifest08/currency/Convertor.java
changeset 19 45e6a2d1ffd1
parent 6 97662396c0fd
     1.1 --- a/task1/solution11/src/org/apidesign/apifest08/currency/Convertor.java	Sun Sep 28 14:12:38 2008 +0200
     1.2 +++ b/task1/solution11/src/org/apidesign/apifest08/currency/Convertor.java	Tue Sep 30 12:01:18 2008 +0200
     1.3 @@ -22,6 +22,11 @@
     1.4              Computer<AmountType> computer,
     1.5              CurrencyValue<AmountType, IdentifierType> firstCurrencyExchangeRate,
     1.6              CurrencyValue<AmountType, IdentifierType> secondCurrencyExchangeRate) {
     1.7 +        if (firstCurrencyExchangeRate.getIdentifier() == null ||
     1.8 +            secondCurrencyExchangeRate.getIdentifier() == null ||
     1.9 +            firstCurrencyExchangeRate.getIdentifier().equals(secondCurrencyExchangeRate.getIdentifier())) {
    1.10 +                throw new IllegalArgumentException("Inappropriate exchange rates' identifiers!");
    1.11 +        }
    1.12          this.computer = computer;
    1.13          this.firstCurrencyExchangeRate = firstCurrencyExchangeRate;
    1.14          this.secondCurrencyExchangeRate = secondCurrencyExchangeRate;
    1.15 @@ -31,11 +36,26 @@
    1.16       * Convert an amount of the one currency to an amount of the another one currency
    1.17       * with respect to previously specified exchange rate.
    1.18       * 
    1.19 -     * @param currencyValue an amount of the one currency
    1.20 -     * @return an amount of the another one currency
    1.21 +     * @param targetCurrency an identifier of the requested currency
    1.22 +     * @param currencyValue an amount of the another one currency
    1.23 +     * @return an amount of the requested currency
    1.24       */
    1.25 -    public CurrencyValue<AmountType, IdentifierType> convert(CurrencyValue<AmountType, IdentifierType> currencyValue) {
    1.26 -        if (firstCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
    1.27 +    public CurrencyValue<AmountType, IdentifierType> convert(
    1.28 +            IdentifierType targetCurrency,
    1.29 +            CurrencyValue<AmountType, IdentifierType> currencyValue) {
    1.30 +        if (firstCurrencyExchangeRate.getIdentifier().equals(targetCurrency) &&
    1.31 +            secondCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
    1.32 +            ComputerRequest<AmountType> computerRequest = new ComputerRequest<AmountType>();
    1.33 +            computerRequest.setInput(currencyValue.getAmount());
    1.34 +            computerRequest.setInputCurrencyRatio(secondCurrencyExchangeRate.getAmount());
    1.35 +            computerRequest.setOutputCurrencyRatio(firstCurrencyExchangeRate.getAmount());
    1.36 +            ComputerResponse<AmountType> computerResponse = computer.compute(computerRequest);
    1.37 +
    1.38 +            return CurrencyValue.getCurrencyValue(
    1.39 +                    computerResponse.getResult(),
    1.40 +                    firstCurrencyExchangeRate.getIdentifier());
    1.41 +        } else if (secondCurrencyExchangeRate.getIdentifier().equals(targetCurrency) &&
    1.42 +                   firstCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
    1.43              ComputerRequest<AmountType> computerRequest = new ComputerRequest<AmountType>();
    1.44              computerRequest.setInput(currencyValue.getAmount());
    1.45              computerRequest.setInputCurrencyRatio(firstCurrencyExchangeRate.getAmount());
    1.46 @@ -45,18 +65,8 @@
    1.47              return CurrencyValue.getCurrencyValue(
    1.48                      computerResponse.getResult(),
    1.49                      secondCurrencyExchangeRate.getIdentifier());
    1.50 -        } else if (secondCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
    1.51 -            ComputerRequest<AmountType> computerRequest = new ComputerRequest<AmountType>();
    1.52 -            computerRequest.setInput(currencyValue.getAmount());
    1.53 -            computerRequest.setInputCurrencyRatio(secondCurrencyExchangeRate.getAmount());
    1.54 -            computerRequest.setOutputCurrencyRatio(firstCurrencyExchangeRate.getAmount());
    1.55 -            ComputerResponse<AmountType> computerResponse = computer.compute(computerRequest);
    1.56 -
    1.57 -            return CurrencyValue.getCurrencyValue(
    1.58 -                    computerResponse.getResult(),
    1.59 -                    firstCurrencyExchangeRate.getIdentifier());
    1.60          } else {
    1.61 -            throw new IllegalArgumentException("Inappropriate currency to convert!");
    1.62 +            throw new IllegalArgumentException("Inappropriate currencies to convert!");
    1.63          }
    1.64      }
    1.65