Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.amlxv.anotherclass
- import android.app.Activity
- import android.os.Bundle
- import android.widget.Button
- import android.widget.EditText
- import android.widget.TextView
- import android.widget.Toast
- import java.text.DecimalFormat
- class CarLoanActivity : Activity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- // Set the layout
- setContentView(R.layout.activity_car_loan)
- // Call the widgets
- val btnCalculate = findViewById<Button>(R.id.btn_calculate);
- val price = findViewById<EditText>(R.id.price);
- val deposit = findViewById<EditText>(R.id.deposit);
- val interest = findViewById<EditText>(R.id.interest);
- val year = findViewById<EditText>(R.id.year);
- val instalment = findViewById<TextView>(R.id.instalment);
- // Implement method
- btnCalculate.setOnClickListener {
- var instalmentTotal: String?;
- // Input checking
- val valid: Boolean =
- price.text.isNotEmpty() || deposit.text.isNotEmpty() || interest.text.isNotEmpty() || year.text.isNotEmpty()
- if (!valid) {
- Toast.makeText(
- this,
- "All data are required to perform this task!",
- Toast.LENGTH_SHORT
- ).show();
- } else {
- val decimalFormat = DecimalFormat("#.##");
- instalmentTotal =
- decimalFormat.format((((((getDouble(price) - getDouble(deposit)) * getDouble(interest)) / 100) * getInt(
- year
- )) + (getDouble(price) - getDouble(deposit))) / (getInt(year) * 12)).toString();
- instalment.text = "** The instalment for your car is: **\n\nRM " + instalmentTotal + "/month";
- }
- }
- }
- private fun getDouble(item: EditText): Double {
- return item.text.toString().toDouble()
- }
- private fun getInt(item: EditText): Int {
- return item.text.toString().toInt()
- }
- }
Add Comment
Please, Sign In to add comment