task4/solution13/test/org/apidesign/apifest08/test/BouncingExchangeRateEngine.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 58 task3/solution13/test/org/apidesign/apifest08/test/BouncingExchangeRateEngine.java@07c16ec15a25
permissions -rw-r--r--
Copying structure for task4
     1 package org.apidesign.apifest08.test;
     2 
     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;
     7 
     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.
    10  *
    11  * @author arnostvalicek
    12  */
    13 public class BouncingExchangeRateEngine implements IExchangeRateEngine {
    14     
    15     ConvertorCurrency fromCurrency;
    16     BigDecimal fromValue;
    17     ConvertorCurrency toCurrency;
    18     BigDecimal toValueStart;
    19     BigDecimal toValueStop;
    20     BigDecimal step;
    21     
    22     BigDecimal toValue;
    23 
    24     
    25     
    26     
    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;
    33         this.step = step;
    34         this.toValue = toValueStart;
    35     }
    36 
    37 
    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);
    40     }
    41 
    42     public ExchangeRate getExchangeRate(ConvertorCurrency fromCurrency, ConvertorCurrency toCurrency) {
    43         if (!fromCurrency.equals(this.fromCurrency)) {
    44             return null;
    45         }
    46         if (!toCurrency.equals(this.toCurrency)) {
    47             return null;
    48         }
    49         
    50         ExchangeRate result = new ExchangeRate(fromValue, toValue);
    51         
    52         toValue = toValue.add(step);
    53         
    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;
    60 //            toValueStop=x;
    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;
    67 //            toValueStop=x;
    68 //        }
    69         if (toValueStop.compareTo(toValue)==0) {
    70             toValue=toValueStop;
    71             step = step.negate();
    72             BigDecimal x = toValueStart;
    73             toValueStart=toValueStop;
    74             toValueStop=x;
    75         }        
    76         return result;
    77     }
    78 
    79 }