1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task2/solution09/src/org/apidesign/apifest08/currency/ConvertorFactory.java Wed Oct 01 10:43:05 2008 +0200
1.3 @@ -0,0 +1,38 @@
1.4 +package org.apidesign.apifest08.currency;
1.5 +
1.6 +import java.math.BigDecimal;
1.7 +
1.8 +
1.9 +public class ConvertorFactory {
1.10 +
1.11 + public static Convertor getConvertor(CurrencyType from, CurrencyType to) {
1.12 + if (from == CurrencyType.CZK && to == CurrencyType.USD) {
1.13 + return new BasicConvertor(new BigDecimal(17));
1.14 + } else if (from == CurrencyType.SKK && to == CurrencyType.CZK) {
1.15 + double rate = 0.8d;
1.16 + return new BasicConvertor(new BigDecimal(rate));
1.17 + }
1.18 +
1.19 +
1.20 + throw new UnsupportedOperationException("Conversion not supported now");
1.21 + }
1.22 +
1.23 + private static class BasicConvertor implements Convertor {
1.24 +
1.25 + private final BigDecimal conversionRate;
1.26 +
1.27 + BasicConvertor(BigDecimal conversionRate) {
1.28 + this.conversionRate = conversionRate;
1.29 + }
1.30 +
1.31 + @Override
1.32 + public long convertTo(long amount) {
1.33 + return (long) (conversionRate.doubleValue() * amount);
1.34 + }
1.35 +
1.36 + @Override
1.37 + public long convertFrom(long amount) {
1.38 + return (long) (amount / conversionRate.doubleValue());
1.39 + }
1.40 + }
1.41 +}