task4/solution13/test/org/apidesign/apifest08/test/BouncingExchangeRateEngine.java
1 package org.apidesign.apifest08.test;
3 import java.math.BigDecimal;
4 import org.apidesign.apifest08.currency.ConvertorCurrency;
5 import org.apidesign.apifest08.currency.ExchangeRate;
6 import org.apidesign.apifest08.currency.IExchangeRateEngine;
8 /** Exchange rate engine which is periodicaly changing echange rate.
9 * See <a href="http://wiki.apidesign.org/wiki/APIFest08:Task3">http://wiki.apidesign.org/wiki/APIFest08:Task3</a> for further description.
11 * @author arnostvalicek
13 public class BouncingExchangeRateEngine implements IExchangeRateEngine {
15 ConvertorCurrency fromCurrency;
17 ConvertorCurrency toCurrency;
18 BigDecimal toValueStart;
19 BigDecimal toValueStop;
27 private BouncingExchangeRateEngine(ConvertorCurrency fromCurrency, BigDecimal fromValue, ConvertorCurrency toCurrency, BigDecimal toValueStart, BigDecimal toValueStop, BigDecimal step) {
28 this.fromCurrency = fromCurrency;
29 this.fromValue = fromValue;
30 this.toCurrency = toCurrency;
31 this.toValueStart = toValueStart;
32 this.toValueStop = toValueStop;
34 this.toValue = toValueStart;
38 public static IExchangeRateEngine create(ConvertorCurrency fromCurrency, BigDecimal fromValue, ConvertorCurrency toCurrency, BigDecimal toValueStart, BigDecimal toValueStop, BigDecimal step) {
39 return new BouncingExchangeRateEngine(fromCurrency, fromValue, toCurrency, toValueStart, toValueStop, step);
42 public ExchangeRate getExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
43 if (!fromCurrency.equals(this.fromCurrency)) {
46 if (!toCurrency.equals(this.toCurrency)) {
50 ExchangeRate result = new ExchangeRate(fromValue, toValue);
52 toValue = toValue.add(step);
54 // if (step.signum()==1 && toValueStop.compareTo(toValue)==0) {
55 // System.out.println("A");
56 // toValue=toValueStop;
57 // step = step.negate();
58 // BigDecimal x = toValueStart;
59 // toValueStart=toValueStop;
61 // } else if (step.signum()==-1 && toValueStop.compareTo(toValue)==0) {
62 // System.out.println("B");
63 // toValue=toValueStop;
64 // step = step.negate();
65 // BigDecimal x = toValueStart;
66 // toValueStart=toValueStop;
69 if (toValueStop.compareTo(toValue)==0) {
72 BigDecimal x = toValueStart;
73 toValueStart=toValueStop;