Advertisement
sururuzzaman

MainActivity.java

Aug 24th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. package com.dicoding.hidro.hydroponic;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.os.Bundle;
  6. import android.text.TextUtils;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11.  
  12. import org.w3c.dom.Text;
  13.  
  14. public abstract class MainActivity extends AppCompatActivity implements View.OnClickListener {
  15.  
  16.     private EditText edtWidth, edtHeight, edtLength;
  17.     private Button btnCalculate;
  18.     private TextView tvResult;
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.activity_main);
  23.  
  24.         edtWidth = findViewById(R.id.edt_width);
  25.         edtHeight = findViewById(R.id.edt_height);
  26.         edtLength = findViewById(R.id.edt_length);
  27.         btnCalculate = findViewById(R.id.btn_calculate);
  28.         tvResult = findViewById(R.id.tv_result);
  29.  
  30.         btnCalculate.setOnClickListener(this);
  31.     }
  32.  
  33.     @Override
  34.     public void onClick(View v) {
  35.         if (v.getId() == R.id.btn_calculate) {
  36.             String inputLength = edtLength.getText().toString().trim();
  37.             String inputWidth = edtWidth.getText().toString().trim();
  38.             String inputHeight = edtHeight.getText().toString().trim();
  39.  
  40.             boolean isEmptyFields = false;
  41.             boolean isInvalidDouble = false;
  42.  
  43.             if (TextUtils.isEmpty(inputLength)) {
  44.                 isEmptyFields = true;
  45.                 edtLength.setError("Field Ini Tidak Boleh Kosong");
  46.             }
  47.  
  48.             if (TextUtils.isEmpty(inputWidth)) {
  49.                 isEmptyFields = true;
  50.                 edtWidth.setError("Field ini tidak boleh kosong");
  51.             }
  52.  
  53.             if (TextUtils.isEmpty(inputHeight)) {
  54.                 isEmptyFields = true;
  55.                 edtHeight.setError("field ini tidak boleh kosong");
  56.             }
  57.             Double length = toDouble(inputLength);
  58.             Double width = toDouble(inputWidth);
  59.             Double height = toDouble(inputHeight);
  60.  
  61.             if (length == null) {
  62.                 isInvalidDouble = true;
  63.                 edtLength.setError("field ini tidak boleh kosong");
  64.             }
  65.  
  66.             if (width == null) {
  67.                 isInvalidDouble = true;
  68.                 edtWidth.setError("field ini tidak boleh kosong");
  69.             }
  70.  
  71.             if (height == null) {
  72.                 isInvalidDouble = true;
  73.                 edtHeight.setError("field ini tidak boleh kosong");
  74.             }
  75.  
  76.             if (!isEmptyFields && !isInvalidDouble) {
  77.                 double volume = length * width * height;
  78.                 tvResult.setText(String.valueOf(volume));
  79.             }
  80.         }
  81.     }
  82.  
  83.     private Double toDouble(String str) {
  84.         try {
  85.             return Double.valueOf(str);
  86.         } catch (NumberFormatException e) {
  87.             return null;
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement