author | japod@localhost |
Sun, 28 Sep 2008 14:12:38 +0200 | |
changeset 6 | 97662396c0fd |
permissions | -rw-r--r-- |
japod@6 | 1 |
package org.apidesign.apifest08.currency; |
japod@6 | 2 |
|
japod@6 | 3 |
import java.math.BigDecimal; |
japod@6 | 4 |
|
japod@6 | 5 |
|
japod@6 | 6 |
public class ConvertorFactory { |
japod@6 | 7 |
|
japod@6 | 8 |
public static Convertor getConvertor(CurrencyType from, CurrencyType to) { |
japod@6 | 9 |
if (from == CurrencyType.CZK && to == CurrencyType.USD) { |
japod@6 | 10 |
return new BasicConvertor(new BigDecimal(17)); |
japod@6 | 11 |
} else if (from == CurrencyType.SKK && to == CurrencyType.CZK) { |
japod@6 | 12 |
double rate = 0.8d; |
japod@6 | 13 |
return new BasicConvertor(new BigDecimal(rate)); |
japod@6 | 14 |
} |
japod@6 | 15 |
|
japod@6 | 16 |
|
japod@6 | 17 |
throw new UnsupportedOperationException("Conversion not supported now"); |
japod@6 | 18 |
} |
japod@6 | 19 |
|
japod@6 | 20 |
private static class BasicConvertor implements Convertor { |
japod@6 | 21 |
|
japod@6 | 22 |
private final BigDecimal conversionRate; |
japod@6 | 23 |
|
japod@6 | 24 |
BasicConvertor(BigDecimal conversionRate) { |
japod@6 | 25 |
this.conversionRate = conversionRate; |
japod@6 | 26 |
} |
japod@6 | 27 |
|
japod@6 | 28 |
@Override |
japod@6 | 29 |
public long convertTo(long amount) { |
japod@6 | 30 |
return (long) (conversionRate.doubleValue() * amount); |
japod@6 | 31 |
} |
japod@6 | 32 |
|
japod@6 | 33 |
@Override |
japod@6 | 34 |
public long convertFrom(long amount) { |
japod@6 | 35 |
return (long) (amount / conversionRate.doubleValue()); |
japod@6 | 36 |
} |
japod@6 | 37 |
} |
japod@6 | 38 |
} |