amlxv

queue

Mar 8th, 2022 (edited)
1,158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.09 KB | None | 0 0
  1. package com.amlxv.anotherclass
  2.  
  3. import android.app.Activity
  4. import android.os.Bundle
  5. import android.widget.Button
  6. import android.widget.EditText
  7. import android.widget.TextView
  8. import android.widget.Toast
  9. import java.text.DecimalFormat
  10.  
  11. class CarLoanActivity : Activity() {
  12.     override fun onCreate(savedInstanceState: Bundle?) {
  13.         super.onCreate(savedInstanceState)
  14.  
  15.         // Set the layout
  16.         setContentView(R.layout.activity_car_loan)
  17.  
  18.         // Call the widgets
  19.         val btnCalculate = findViewById<Button>(R.id.btn_calculate);
  20.         val price = findViewById<EditText>(R.id.price);
  21.         val deposit = findViewById<EditText>(R.id.deposit);
  22.         val interest = findViewById<EditText>(R.id.interest);
  23.         val year = findViewById<EditText>(R.id.year);
  24.         val instalment = findViewById<TextView>(R.id.instalment);
  25.  
  26.         // Implement method
  27.         btnCalculate.setOnClickListener {
  28.  
  29.             var instalmentTotal: String?;
  30.  
  31.             // Input checking
  32.             val valid: Boolean =
  33.                 price.text.isNotEmpty() || deposit.text.isNotEmpty() || interest.text.isNotEmpty() || year.text.isNotEmpty()
  34.  
  35.             if (!valid) {
  36.                 Toast.makeText(
  37.                     this,
  38.                     "All data are required to perform this task!",
  39.                     Toast.LENGTH_SHORT
  40.                 ).show();
  41.             } else {
  42.                 val decimalFormat = DecimalFormat("#.##");
  43.  
  44.                 instalmentTotal =
  45.                     decimalFormat.format((((((getDouble(price) - getDouble(deposit)) * getDouble(interest)) / 100) * getInt(
  46.                         year
  47.                     )) + (getDouble(price) - getDouble(deposit))) / (getInt(year) * 12)).toString();
  48.  
  49.                 instalment.text = "** The instalment for your car is: **\n\nRM " + instalmentTotal + "/month";
  50.  
  51.             }
  52.         }
  53.     }
  54.  
  55.     private fun getDouble(item: EditText): Double {
  56.         return item.text.toString().toDouble()
  57.     }
  58.  
  59.     private fun getInt(item: EditText): Int {
  60.         return item.text.toString().toInt()
  61.     }
  62. }
Add Comment
Please, Sign In to add comment