Advertisement
erwinprasetyo97

dontknow

Apr 4th, 2018
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. package com.example.er.latihan1;
  2.  
  3. import android.os.Bundle;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.text.TextUtils;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10.  
  11. import com.example.er.latihan1.R;
  12.  
  13. public class MainActivity extends AppCompatActivity
  14.     implements View.OnClickListener {
  15.     private EditText edtWidth, edtHeight, edtLength;
  16.     private Button btnCalculate;
  17.     private TextView tvResult;
  18.  
  19.     private static String STATE_HASIL = "state_hasil";
  20.     private String str;
  21.  
  22.     @Override
  23.     protected void onCreate(Bundle saveInstanceState){
  24.         super.onCreate(saveInstanceState);
  25.         setContentView(R.layout.activity_main);
  26.  
  27.         edtWidth =(EditText)findViewById(R.id.edt_width);
  28.         edtHeight = (EditText)findViewById(R.id.edt_height);
  29.         edtLength = (EditText)findViewById(R.id.edt_length);
  30.         btnCalculate = (Button)findViewById(R.id.btn_calculate);
  31.         tvResult = (TextView)findViewById(R.id.tv_result);
  32.  
  33.         btnCalculate.setOnClickListener(this);
  34.     }
  35.  
  36.  
  37.     @Override
  38.     public void onClick(View v) {
  39.         if (v.getId()== R.id.btn_calculate) {
  40.             String length = edtLength.getText().toString().trim();
  41.             String width = edtWidth.getText().toString().trim();
  42.             String height = edtHeight.getText().toString().trim();
  43.  
  44.             boolean isEmptyFields = false;
  45.             boolean isInvalidDouble = false;
  46.  
  47.             if (TextUtils.isEmpty(length)) {
  48.                 isEmptyFields = true;
  49.                 edtLength.setError("Field ini tidak boleh kosong");
  50.             }else if (!isDouble(length)) {
  51.                 isInvalidDouble = true;
  52.                 edtLength.setError("Field ini harus berupa nomor yang valid");
  53.             }
  54.  
  55.  
  56.             if (TextUtils.isEmpty(width)) {
  57.                 isEmptyFields = true;
  58.                 edtLength.setError("Field ini tidak boleh kosong");
  59.             }else if (!isDouble(width)) {
  60.                 isInvalidDouble = true;
  61.                 edtLength.setError("Field ini harus berupa nomor yang valid");
  62.             }
  63.  
  64.  
  65.             if (TextUtils.isEmpty(height)) {
  66.                 isEmptyFields = true;
  67.                 edtLength.setError("Field ini tidak boleh kosong");
  68.             }else if (!isDouble(height)) {
  69.                 isInvalidDouble = true;
  70.                 edtLength.setError("Field ini harus berupa nomor yang valid");
  71.             }
  72.            
  73.             if (!isEmptyFields&&!isInvalidDouble) {
  74.                 double l = Double.parseDouble(length);
  75.                 double w = Double.parseDouble(width);
  76.                 double h = Double.parseDouble(height);
  77.                
  78.                 double volume = l*w*h;
  79.                 tvResult.setText(String.valueOf(volume));
  80.             }
  81.            
  82.             boolean isDouble(String str) {
  83.                 try {
  84.                     Double.parseDouble(str);
  85.                     return true;
  86.                 } catch (NumberFormatException e){
  87.                     return false;
  88.                 }
  89.             }
  90.            
  91.            
  92.            
  93.            
  94.  
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement