task4/solution13/test/org/apidesign/apifest08/test/BouncingExchangeRateEngine.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task4/solution13/test/org/apidesign/apifest08/test/BouncingExchangeRateEngine.java Sat Oct 11 23:38:46 2008 +0200
1.3 @@ -0,0 +1,79 @@
1.4 +package org.apidesign.apifest08.test;
1.5 +
1.6 +import java.math.BigDecimal;
1.7 +import org.apidesign.apifest08.currency.ConvertorCurrency;
1.8 +import org.apidesign.apifest08.currency.ExchangeRate;
1.9 +import org.apidesign.apifest08.currency.IExchangeRateEngine;
1.10 +
1.11 +/** Exchange rate engine which is periodicaly changing echange rate.
1.12 + * See <a href="http://wiki.apidesign.org/wiki/APIFest08:Task3">http://wiki.apidesign.org/wiki/APIFest08:Task3</a> for further description.
1.13 + *
1.14 + * @author arnostvalicek
1.15 + */
1.16 +public class BouncingExchangeRateEngine implements IExchangeRateEngine {
1.17 +
1.18 + ConvertorCurrency fromCurrency;
1.19 + BigDecimal fromValue;
1.20 + ConvertorCurrency toCurrency;
1.21 + BigDecimal toValueStart;
1.22 + BigDecimal toValueStop;
1.23 + BigDecimal step;
1.24 +
1.25 + BigDecimal toValue;
1.26 +
1.27 +
1.28 +
1.29 +
1.30 + private BouncingExchangeRateEngine(ConvertorCurrency fromCurrency, BigDecimal fromValue, ConvertorCurrency toCurrency, BigDecimal toValueStart, BigDecimal toValueStop, BigDecimal step) {
1.31 + this.fromCurrency = fromCurrency;
1.32 + this.fromValue = fromValue;
1.33 + this.toCurrency = toCurrency;
1.34 + this.toValueStart = toValueStart;
1.35 + this.toValueStop = toValueStop;
1.36 + this.step = step;
1.37 + this.toValue = toValueStart;
1.38 + }
1.39 +
1.40 +
1.41 + public static IExchangeRateEngine create(ConvertorCurrency fromCurrency, BigDecimal fromValue, ConvertorCurrency toCurrency, BigDecimal toValueStart, BigDecimal toValueStop, BigDecimal step) {
1.42 + return new BouncingExchangeRateEngine(fromCurrency, fromValue, toCurrency, toValueStart, toValueStop, step);
1.43 + }
1.44 +
1.45 + public ExchangeRate getExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
1.46 + if (!fromCurrency.equals(this.fromCurrency)) {
1.47 + return null;
1.48 + }
1.49 + if (!toCurrency.equals(this.toCurrency)) {
1.50 + return null;
1.51 + }
1.52 +
1.53 + ExchangeRate result = new ExchangeRate(fromValue, toValue);
1.54 +
1.55 + toValue = toValue.add(step);
1.56 +
1.57 +// if (step.signum()==1 && toValueStop.compareTo(toValue)==0) {
1.58 +// System.out.println("A");
1.59 +// toValue=toValueStop;
1.60 +// step = step.negate();
1.61 +// BigDecimal x = toValueStart;
1.62 +// toValueStart=toValueStop;
1.63 +// toValueStop=x;
1.64 +// } else if (step.signum()==-1 && toValueStop.compareTo(toValue)==0) {
1.65 +// System.out.println("B");
1.66 +// toValue=toValueStop;
1.67 +// step = step.negate();
1.68 +// BigDecimal x = toValueStart;
1.69 +// toValueStart=toValueStop;
1.70 +// toValueStop=x;
1.71 +// }
1.72 + if (toValueStop.compareTo(toValue)==0) {
1.73 + toValue=toValueStop;
1.74 + step = step.negate();
1.75 + BigDecimal x = toValueStart;
1.76 + toValueStart=toValueStop;
1.77 + toValueStop=x;
1.78 + }
1.79 + return result;
1.80 + }
1.81 +
1.82 +}