task4/solution04/test/org/apidesign/apifest08/test/TestExchangeRateFinder.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 55 task3/solution04/test/org/apidesign/apifest08/test/TestExchangeRateFinder.java@14e78f48ac2b
permissions -rw-r--r--
Copying structure for task4
     1 package org.apidesign.apifest08.test;
     2 
     3 
     4 import java.math.BigDecimal;
     5 import java.util.Currency;
     6 import org.apidesign.apifest08.currency.ExchangeRate;
     7 import org.apidesign.apifest08.currency.ExchangeRateFinder;
     8 
     9 
    10 class TestExchangeRateFinder
    11     implements ExchangeRateFinder
    12 {
    13     private final BigDecimal min;
    14     private final BigDecimal max;
    15     private final BigDecimal stepUp;
    16     private final BigDecimal stepDown;
    17     private BigDecimal step;
    18     private BigDecimal rate;
    19     private boolean firstCall;
    20 
    21     TestExchangeRateFinder(final BigDecimal mn,
    22                            final BigDecimal mx,
    23                            final BigDecimal start,
    24                            final BigDecimal up,
    25                            final BigDecimal down)
    26     {
    27         min       = mn;
    28         max       = mx;
    29         rate      = start;
    30         stepUp    = up;
    31         stepDown  = down;
    32         firstCall = true;
    33     }
    34     
    35     public ExchangeRate findRate(Currency a, Currency b) 
    36     {
    37         final ExchangeRate value;
    38 
    39         if(rate.equals(max))
    40         {
    41             step = stepDown;
    42         }
    43         else if(rate.equals(min))
    44         {
    45             step = stepUp;
    46         }
    47 
    48         value = new ExchangeRate(a, b, BigDecimal.ONE, rate);
    49 
    50         if(firstCall)
    51         {
    52             firstCall = false;
    53         }
    54         else
    55         {
    56             rate = rate.add(step);
    57         }
    58 
    59         return (value);
    60     }
    61 }