package com.example.fitness;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class imcActivity extends AppCompatActivity {
private EditText editHeight;
private EditText editWeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imc);
editHeight = findViewById(R.id.edit_imc_height);
editWeight = findViewById(R.id.edit_imc_weight);
Button btnSend = findViewById(R.id.btn_imc_send);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!validate()) {
Toast.makeText(imcActivity.this,R.string.fields_messages, Toast.LENGTH_LONG).show();
return;
}
String sHeight = editHeight.getText().toString();
String sWeight = editWeight.getText().toString();
int height = Integer.parseInt(sHeight);
int weight = Integer.parseInt(sWeight);
double result = calculateImc(height, weight);
Log.d("TESTE", "resultado " + result);
}
});
}
private double calculateImc(int height, int weight){
// peso/(altura * altura)
return weight / ((double) height / 100) * ((double) height / 100);
}
private boolean validate() {
return (!editHeight.getText().toString().startsWith("0")
&& !editWeight.getText().toString().startsWith("0")
&& !editHeight.getText().toString().isEmpty()
&& !editWeight.getText().toString().isEmpty()) ;