package com.andela.currencycalculator.calculatorbrain;

import com.andela.currencycalculator.model.currency.Currency;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Spykins on 26/02/2016.
 */
public class CurrencyConverter{

    HashMap<String, Double>currencyAndExchangeRate;

    public  CurrencyConverter(){
        currencyAndExchangeRate = new HashMap<>();
    }

    public String exchangeFromCurrencyTo(String convertFrom, String convertTo, String amount){

        double answer = 0;
        DecimalFormat decimalFormat = new DecimalFormat(".##");
        if(currencyAndExchangeRate.containsKey(convertFrom) &&
                currencyAndExchangeRate.containsKey(convertTo)){
            answer = performCalculation(convertFrom,convertTo,amount);
        }
        if(answer == 0){
            return "0";
        }else if(answer < 1){
           return  0 + String.valueOf(decimalFormat.format(answer));
        }

        return String.valueOf(decimalFormat.format(answer));
    }

    private double performCalculation(String convertFrom, String convertTo, String amount){

        double answer;
        if(amount.equals("0") || amount.equals("")){
            return 0;
        }
        double inputExchangeRateToDollar = currencyAndExchangeRate.get(convertFrom);
        double amountConverting = Double.parseDouble(amount);
        double inputInDollar = amountConverting/inputExchangeRateToDollar;

        double outputExchangeRateToDollar = currencyAndExchangeRate.get(convertTo);
        answer = inputInDollar * outputExchangeRateToDollar;
        return answer;
    }

    public void setAllCurrencyInDb(ArrayList<Currency> currencies){
        for (Currency currency : currencies){
            currencyAndExchangeRate.put(currency.getCurrency(),currency.getExchangeRate());
        }
    }

}

@@ -115,40 +115,77 @@
 cy;%0A
-%0A
 import 
-java.text.DecimalFormat;
+com.andela.currencycalculator.model.helper.StringManipulator;%0A
 %0Aimp
@@ -311,16 +311,17 @@
 onverter
+ 
 %7B%0A%0A    H
@@ -342,16 +342,17 @@
  Double%3E
+ 
 currency
@@ -380,17 +380,16 @@
  public 
- 
 Currency
@@ -399,16 +399,17 @@
 verter()
+ 
 %7B%0A      
@@ -545,32 +545,33 @@
 , String amount)
+ 
 %7B%0A%0A        doubl
@@ -596,74 +596,11 @@
     
-DecimalFormat decimalFormat = new DecimalFormat(%22.##%22);%0A        
 if
+ 
 (cur
@@ -711,16 +711,17 @@
 vertTo))
+ 
 %7B%0A      
@@ -766,16 +766,17 @@
 ertFrom,
+ 
 convertT
@@ -777,16 +777,17 @@
 nvertTo,
+ 
 amount);
@@ -801,42 +801,17 @@
   %7D%0A
-        if(answer == 0)%7B%0A 
+%0A
         
    r
@@ -810,191 +810,65 @@
     
-   
 return 
-%220%22;%0A        %7Delse if(answer %3C 1)%7B%0A           return  0 + String.valueOf(decimalFormat.format(answer));%0A        %7D%0A%0A        return String.valueOf(decimalFormat.format
+StringManipulator.formatToTwoDecimalPlaces
 (answer)
 );%0A 
@@ -863,17 +863,16 @@
 (answer)
-)
 ;%0A    %7D%0A
@@ -962,16 +962,17 @@
  amount)
+ 
 %7B%0A%0A     
@@ -999,16 +999,17 @@
       if
+ 
 (amount.
@@ -1041,16 +1041,17 @@
 als(%22%22))
+ 
 %7B%0A      
@@ -1270,17 +1270,19 @@
 nverting
-/
+ / 
 inputExc
@@ -1534,32 +1534,33 @@
 ncy%3E currencies)
+ 
 %7B%0A        for (C
@@ -1589,16 +1589,17 @@
 rencies)
+ 
 %7B%0A      
@@ -1655,16 +1655,17 @@
 rency(),
+ 
 currency

Refactor currency converter