japod@58: package org.apidesign.apifest08.test; japod@58: japod@58: import java.math.BigDecimal; japod@58: import org.apidesign.apifest08.currency.ConvertorCurrency; japod@58: import org.apidesign.apifest08.currency.ExchangeRate; japod@58: import org.apidesign.apifest08.currency.IExchangeRateEngine; japod@58: japod@58: /** Exchange rate engine which is periodicaly changing echange rate. japod@58: * See http://wiki.apidesign.org/wiki/APIFest08:Task3 for further description. japod@58: * japod@58: * @author arnostvalicek japod@58: */ japod@58: public class BouncingExchangeRateEngine implements IExchangeRateEngine { japod@58: japod@58: ConvertorCurrency fromCurrency; japod@58: BigDecimal fromValue; japod@58: ConvertorCurrency toCurrency; japod@58: BigDecimal toValueStart; japod@58: BigDecimal toValueStop; japod@58: BigDecimal step; japod@58: japod@58: BigDecimal toValue; japod@58: japod@58: japod@58: japod@58: japod@58: private BouncingExchangeRateEngine(ConvertorCurrency fromCurrency, BigDecimal fromValue, ConvertorCurrency toCurrency, BigDecimal toValueStart, BigDecimal toValueStop, BigDecimal step) { japod@58: this.fromCurrency = fromCurrency; japod@58: this.fromValue = fromValue; japod@58: this.toCurrency = toCurrency; japod@58: this.toValueStart = toValueStart; japod@58: this.toValueStop = toValueStop; japod@58: this.step = step; japod@58: this.toValue = toValueStart; japod@58: } japod@58: japod@58: japod@58: public static IExchangeRateEngine create(ConvertorCurrency fromCurrency, BigDecimal fromValue, ConvertorCurrency toCurrency, BigDecimal toValueStart, BigDecimal toValueStop, BigDecimal step) { japod@58: return new BouncingExchangeRateEngine(fromCurrency, fromValue, toCurrency, toValueStart, toValueStop, step); japod@58: } japod@58: japod@58: public ExchangeRate getExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) { japod@58: if (!fromCurrency.equals(this.fromCurrency)) { japod@58: return null; japod@58: } japod@58: if (!toCurrency.equals(this.toCurrency)) { japod@58: return null; japod@58: } japod@58: japod@58: ExchangeRate result = new ExchangeRate(fromValue, toValue); japod@58: japod@58: toValue = toValue.add(step); japod@58: japod@58: // if (step.signum()==1 && toValueStop.compareTo(toValue)==0) { japod@58: // System.out.println("A"); japod@58: // toValue=toValueStop; japod@58: // step = step.negate(); japod@58: // BigDecimal x = toValueStart; japod@58: // toValueStart=toValueStop; japod@58: // toValueStop=x; japod@58: // } else if (step.signum()==-1 && toValueStop.compareTo(toValue)==0) { japod@58: // System.out.println("B"); japod@58: // toValue=toValueStop; japod@58: // step = step.negate(); japod@58: // BigDecimal x = toValueStart; japod@58: // toValueStart=toValueStop; japod@58: // toValueStop=x; japod@58: // } japod@58: if (toValueStop.compareTo(toValue)==0) { japod@58: toValue=toValueStop; japod@58: step = step.negate(); japod@58: BigDecimal x = toValueStart; japod@58: toValueStart=toValueStop; japod@58: toValueStop=x; japod@58: } japod@58: return result; japod@58: } japod@58: japod@58: }