package com.example.converter;

import java.text.NumberFormat;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class TemperatureConverterActivity extends Activity {

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

        Button fahrButton = (Button) findViewById(R.id.fahrButton);
        Button celButton = (Button) findViewById(R.id.celsiusButton);

        fahrButton.setOnClickListener(new FahrToCelListener());
        celButton.setOnClickListener(new CelToFahrListener());
    }

    class FahrToCelListener implements View.OnClickListener {
        public void onClick(View view) {
            EditText temperature =
                    (EditText) findViewById(R.id.temperature);
            TextView result = (TextView) findViewById(R.id.result);

            String tempStr = temperature.getText().toString();
            double f = Double.parseDouble(tempStr);
            double c = (5.0 / 9.0) * (f - 32);
            String cStr = NumberFormat.getInstance().format(c);
            result.setText(tempStr + " degrees Fahrenheit is " + cStr
                    + " degrees Celsius");
        }
    }

    class CelToFahrListener implements View.OnClickListener {
        public void onClick(View view) {
            EditText temperature =
                    (EditText) findViewById(R.id.temperature);
            TextView result = (TextView) findViewById(R.id.result);

            String tempStr = temperature.getText().toString();
            double c = Double.parseDouble(tempStr);
            double f = (9.0 / 5.0) * c + 32;
            String fStr = NumberFormat.getInstance().format(f);
            result.setText(tempStr + " degrees Celsius is " + fStr
                    + " degrees Fahrenheit");
        }
    }
}