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-- |
japod@55 | 1 |
package org.apidesign.apifest08.test; |
japod@55 | 2 |
|
japod@55 | 3 |
|
japod@55 | 4 |
import java.math.BigDecimal; |
japod@55 | 5 |
import java.util.Currency; |
japod@55 | 6 |
import org.apidesign.apifest08.currency.ExchangeRate; |
japod@55 | 7 |
import org.apidesign.apifest08.currency.ExchangeRateFinder; |
japod@55 | 8 |
|
japod@55 | 9 |
|
japod@55 | 10 |
class TestExchangeRateFinder |
japod@55 | 11 |
implements ExchangeRateFinder |
japod@55 | 12 |
{ |
japod@55 | 13 |
private final BigDecimal min; |
japod@55 | 14 |
private final BigDecimal max; |
japod@55 | 15 |
private final BigDecimal stepUp; |
japod@55 | 16 |
private final BigDecimal stepDown; |
japod@55 | 17 |
private BigDecimal step; |
japod@55 | 18 |
private BigDecimal rate; |
japod@55 | 19 |
private boolean firstCall; |
japod@55 | 20 |
|
japod@55 | 21 |
TestExchangeRateFinder(final BigDecimal mn, |
japod@55 | 22 |
final BigDecimal mx, |
japod@55 | 23 |
final BigDecimal start, |
japod@55 | 24 |
final BigDecimal up, |
japod@55 | 25 |
final BigDecimal down) |
japod@55 | 26 |
{ |
japod@55 | 27 |
min = mn; |
japod@55 | 28 |
max = mx; |
japod@55 | 29 |
rate = start; |
japod@55 | 30 |
stepUp = up; |
japod@55 | 31 |
stepDown = down; |
japod@55 | 32 |
firstCall = true; |
japod@55 | 33 |
} |
japod@55 | 34 |
|
japod@55 | 35 |
public ExchangeRate findRate(Currency a, Currency b) |
japod@55 | 36 |
{ |
japod@55 | 37 |
final ExchangeRate value; |
japod@55 | 38 |
|
japod@55 | 39 |
if(rate.equals(max)) |
japod@55 | 40 |
{ |
japod@55 | 41 |
step = stepDown; |
japod@55 | 42 |
} |
japod@55 | 43 |
else if(rate.equals(min)) |
japod@55 | 44 |
{ |
japod@55 | 45 |
step = stepUp; |
japod@55 | 46 |
} |
japod@55 | 47 |
|
japod@55 | 48 |
value = new ExchangeRate(a, b, BigDecimal.ONE, rate); |
japod@55 | 49 |
|
japod@55 | 50 |
if(firstCall) |
japod@55 | 51 |
{ |
japod@55 | 52 |
firstCall = false; |
japod@55 | 53 |
} |
japod@55 | 54 |
else |
japod@55 | 55 |
{ |
japod@55 | 56 |
rate = rate.add(step); |
japod@55 | 57 |
} |
japod@55 | 58 |
|
japod@55 | 59 |
return (value); |
japod@55 | 60 |
} |
japod@55 | 61 |
} |