Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dicoding.hidro.hydroponic;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import org.w3c.dom.Text;
- public abstract class MainActivity extends AppCompatActivity implements View.OnClickListener {
- private EditText edtWidth, edtHeight, edtLength;
- private Button btnCalculate;
- private TextView tvResult;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- edtWidth = findViewById(R.id.edt_width);
- edtHeight = findViewById(R.id.edt_height);
- edtLength = findViewById(R.id.edt_length);
- btnCalculate = findViewById(R.id.btn_calculate);
- tvResult = findViewById(R.id.tv_result);
- btnCalculate.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- if (v.getId() == R.id.btn_calculate) {
- String inputLength = edtLength.getText().toString().trim();
- String inputWidth = edtWidth.getText().toString().trim();
- String inputHeight = edtHeight.getText().toString().trim();
- boolean isEmptyFields = false;
- boolean isInvalidDouble = false;
- if (TextUtils.isEmpty(inputLength)) {
- isEmptyFields = true;
- edtLength.setError("Field Ini Tidak Boleh Kosong");
- }
- if (TextUtils.isEmpty(inputWidth)) {
- isEmptyFields = true;
- edtWidth.setError("Field ini tidak boleh kosong");
- }
- if (TextUtils.isEmpty(inputHeight)) {
- isEmptyFields = true;
- edtHeight.setError("field ini tidak boleh kosong");
- }
- Double length = toDouble(inputLength);
- Double width = toDouble(inputWidth);
- Double height = toDouble(inputHeight);
- if (length == null) {
- isInvalidDouble = true;
- edtLength.setError("field ini tidak boleh kosong");
- }
- if (width == null) {
- isInvalidDouble = true;
- edtWidth.setError("field ini tidak boleh kosong");
- }
- if (height == null) {
- isInvalidDouble = true;
- edtHeight.setError("field ini tidak boleh kosong");
- }
- if (!isEmptyFields && !isInvalidDouble) {
- double volume = length * width * height;
- tvResult.setText(String.valueOf(volume));
- }
- }
- }
- private Double toDouble(String str) {
- try {
- return Double.valueOf(str);
- } catch (NumberFormatException e) {
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement