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