package com.simplecurrencyconverter;

import java.math.BigDecimal;
import java.math.RoundingMode;

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 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());
        String outputAmount = inputAmount.multiply(multiplier).setScale(2, RoundingMode.HALF_EVEN).toString();
        textToChange.setText(outputAmount.equals("0.00") ? "" : outputAmount);
    }

    private BigDecimal parseDecimal(String s) {
        try {
            BigDecimal decimal = new BigDecimal(s);
            return decimal;
        }
        catch (NumberFormatException e) {
            return new BigDecimal(0);
        }
    }

    @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;
    }
}

@@ -90,16 +90,112 @@
 ingMode;
+%0Aimport java.text.DecimalFormat;%0Aimport java.text.DecimalFormatSymbols;%0Aimport java.util.Locale;
 %0A%0Aimport
@@ -2123,22 +2123,26 @@
         
-String
+BigDecimal
  outputA
@@ -2221,86 +2221,116 @@
 VEN)
-.toString();%0A        textToChange.setText(outputAmount.equals(%220.00%22) ? %22%22 : o
+;%0A        String formattedOutputAmount = formatAmount(outputAmount);%0A        textToChange.setText(formattedO
 utpu
@@ -2578,16 +2578,16 @@
 mal(0);%0A
-
         
@@ -2591,24 +2591,419 @@
    %7D%0A    %7D%0A%0A
+    private String formatAmount(BigDecimal amount) %7B%0A        DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);%0A        symbols.setGroupingSeparator(' ');%0A        DecimalFormat formatter = new DecimalFormat(%22###,##0.00%22, symbols);%0A        String formattedValue = formatter.format(amount.doubleValue());%0A        return formattedValue.equals(%220.00%22) ? %22%22 : formattedValue;%0A    %7D%0A%0A
     @Overrid

Add thousands separators to the updated currency amount