package com.example.ghomi.chapter3_1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import android.text.TextUtils;


public class MainActivity extends AppCompatActivity {

   EditText strNum1, strNum2;
   Button add, minus, multi, div;
   TextView result;
   String oper = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
             strNum1 = (EditText)findViewById(R.id.editNum1);
             strNum2 = (EditText)findViewById(R.id.editNum2) ;
             add = (Button)findViewById(R.id.addButton);
             minus = (Button)findViewById(R.id.minusButton);
             multi = (Button)findViewById(R.id.multiButton) ;
             div = (Button)findViewById(R.id.divButton) ;
             result = (TextView)findViewById(R.id.textResult);

    }
    public void onClickButton(View v) {
          float num1 = 0 ;
          float num2 = 0;
          float resultNum = 0;
         // check if the fields are empty
          if (TextUtils.isEmpty(strNum1.getText().toString())
                || TextUtils.isEmpty(strNum2.getText().toString())) {
               return;
          }
          num1 = Float.parseFloat(strNum1.getText().toString());
          num2 = Float.parseFloat(strNum2.getText().toString());
         // defines the button that has been clicked and performs the corresponding operation
        // write operation into oper, we will use it later for output
        switch (v.getId()) {
              case R.id.addButton:
                 oper = "+";
                 resultNum = num1 + num2;
                 break;
              case R.id.minusButton:
                 oper = "-";
                 resultNum = num1 - num2;
                 break;
              case R.id.multiButton:
                 oper = "*";
                 resultNum = num1 * num2;
                 break;
              case R.id.divButton:
                 oper = "/";
                 resultNum = num1 / num2;
                 break;
              default:
                 break;
        }
        // form the output line
        result.setText(num1 + " " + oper + " " + num2 + " = " + resultNum);
    } // End of onClickButton
}
