package com.example.tips;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TipCalculatorActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button calculate = (Button) findViewById(R.id.calculate);
        calculate.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                EditText billAmountEditText =
                        (EditText) findViewById(R.id.billAmount);
                EditText percentEditText =
                        (EditText) findViewById(R.id.percent);
                TextView tipAmountTextView =
                        (TextView) findViewById(R.id.tipAmount);
                TextView totalBillTextView =
                        (TextView) findViewById(R.id.totalBill);

                try {
                    float billAmount =
                            Float.parseFloat(billAmountEditText
                                    .getText().toString());
                    float tipPercent =
                            Float.parseFloat(percentEditText
                                    .getText().toString());
                    tipPercent = tipPercent / 100;
                    float tip = billAmount * tipPercent;
                    float total = billAmount + tip;

                    tipAmountTextView.setText("Tip Amount: $" + tip);
                    totalBillTextView.setText("Total Bill: $" + total);
                }
                catch (NumberFormatException e) {
                    tipAmountTextView.setText("Tip Amount: ");
                    totalBillTextView.setText("Total Bill: ");
                }
            }
        });
    }
}