task4/solution04/test/org/apidesign/apifest08/test/TestExchangeRateFinder.java
changeset 61 58ec6da75f6f
parent 55 14e78f48ac2b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution04/test/org/apidesign/apifest08/test/TestExchangeRateFinder.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,61 @@
     1.4 +package org.apidesign.apifest08.test;
     1.5 +
     1.6 +
     1.7 +import java.math.BigDecimal;
     1.8 +import java.util.Currency;
     1.9 +import org.apidesign.apifest08.currency.ExchangeRate;
    1.10 +import org.apidesign.apifest08.currency.ExchangeRateFinder;
    1.11 +
    1.12 +
    1.13 +class TestExchangeRateFinder
    1.14 +    implements ExchangeRateFinder
    1.15 +{
    1.16 +    private final BigDecimal min;
    1.17 +    private final BigDecimal max;
    1.18 +    private final BigDecimal stepUp;
    1.19 +    private final BigDecimal stepDown;
    1.20 +    private BigDecimal step;
    1.21 +    private BigDecimal rate;
    1.22 +    private boolean firstCall;
    1.23 +
    1.24 +    TestExchangeRateFinder(final BigDecimal mn,
    1.25 +                           final BigDecimal mx,
    1.26 +                           final BigDecimal start,
    1.27 +                           final BigDecimal up,
    1.28 +                           final BigDecimal down)
    1.29 +    {
    1.30 +        min       = mn;
    1.31 +        max       = mx;
    1.32 +        rate      = start;
    1.33 +        stepUp    = up;
    1.34 +        stepDown  = down;
    1.35 +        firstCall = true;
    1.36 +    }
    1.37 +    
    1.38 +    public ExchangeRate findRate(Currency a, Currency b) 
    1.39 +    {
    1.40 +        final ExchangeRate value;
    1.41 +
    1.42 +        if(rate.equals(max))
    1.43 +        {
    1.44 +            step = stepDown;
    1.45 +        }
    1.46 +        else if(rate.equals(min))
    1.47 +        {
    1.48 +            step = stepUp;
    1.49 +        }
    1.50 +
    1.51 +        value = new ExchangeRate(a, b, BigDecimal.ONE, rate);
    1.52 +
    1.53 +        if(firstCall)
    1.54 +        {
    1.55 +            firstCall = false;
    1.56 +        }
    1.57 +        else
    1.58 +        {
    1.59 +            rate = rate.add(step);
    1.60 +        }
    1.61 +
    1.62 +        return (value);
    1.63 +    }
    1.64 +}