package com.simplecurrencyconverter;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;

public class MainActivity extends Activity {
    private static final BigDecimal ONE_EURO_IN_WONS = new BigDecimal(1452.62594);
    private static final BigDecimal ONE_WON_IN_EUROS = new BigDecimal(1).divide(ONE_EURO_IN_WONS, 5, RoundingMode.HALF_EVEN);

    private EditText getKrwEditText() { return (EditText) findViewById(R.id.krw_amount); }
    private EditText getEurEditText() { return (EditText) findViewById(R.id.eur_amount); }

    private final DecimalFormat amountFormatter = getAmountFormatter();
    private DecimalFormat getAmountFormatter() {
        DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
        symbols.setGroupingSeparator(' ');
        return new DecimalFormat("###,##0.00", symbols);
    }
    private final String emptyAmount = "0.00";

    private boolean ignoreAmountUpdate = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addAmountChangedListeners(getKrwEditText());
        addAmountChangedListeners(getEurEditText());
    }

    private void addAmountChangedListeners(EditText editText) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) { updateAmounts(s); }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
        });
    }

    private void updateAmounts(Editable changedText) {
        if (ignoreAmountUpdate) {
            ignoreAmountUpdate = false;
            return;
        }
        ignoreAmountUpdate = true;

        boolean hasKrwChanged = changedText.equals(getKrwEditText().getText());
        BigDecimal multiplier = hasKrwChanged ? ONE_WON_IN_EUROS : ONE_EURO_IN_WONS;
        EditText textToChange = hasKrwChanged ? getEurEditText() : getKrwEditText();

        BigDecimal inputAmount = parseDecimal(changedText.toString());
        BigDecimal outputAmount = inputAmount.multiply(multiplier).setScale(2, RoundingMode.HALF_EVEN);
        String formattedOutputAmount = formatAmount(outputAmount);
        textToChange.setText(formattedOutputAmount);
    }

    private BigDecimal parseDecimal(String s) {
        try {
            BigDecimal decimal = new BigDecimal(amountFormatter.parse(s).doubleValue());
            return decimal;
        }
        catch (NumberFormatException e) {
            return new BigDecimal(0);
        }
        catch (ParseException e) {
            return new BigDecimal(0);
        }
    }

    private String formatAmount(BigDecimal amount) {
        String formattedValue = amountFormatter.format(amount.doubleValue());
        return formattedValue.equals(emptyAmount) ? "" : formattedValue;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

@@ -365,16 +365,90 @@
 w.Menu;%0A
+import android.view.View;%0Aimport android.view.View.OnFocusChangeListener;%0A
 import a
@@ -471,16 +471,16 @@
 itText;%0A
-
 %0Apublic 
@@ -1575,24 +1575,127 @@
 EditText());
+%0A%0A        addFocusChangedListener(getKrwEditText());%0A        addFocusChangedListener(getEurEditText());
 %0A    %7D%0A%0A    
@@ -2041,32 +2041,32 @@
       @Override%0A
-
             publ
@@ -2151,32 +2151,658 @@
      %7D);%0A    %7D%0A%0A
+    private void addFocusChangedListener(EditText editText) %7B%0A        editText.setOnFocusChangeListener(new OnFocusChangeListener() %7B%0A            @Override%0A            public void onFocusChange(View v, boolean hasFocus) %7B%0A                if (!hasFocus) %7B%0A                    EditText editText = (EditText) v;%0A                    BigDecimal amount = parseDecimal(editText.getText().toString());%0A                    String formattedOutputAmount = formatAmount(amount);%0A                    ignoreAmountUpdate = true;%0A                    editText.setText(formattedOutputAmount);%0A                %7D%0A            %7D%0A        %7D);%0A    %7D%0A%0A
     private void

Format amount when the component loses focus