Skip to content

Commit

Permalink
Commenting the code
Browse files Browse the repository at this point in the history
  • Loading branch information
theyashgrover authored Aug 25, 2021
1 parent 1b57dad commit 9a7924f
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions TipTime/app/src/main/java/com/example/tiptime/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.text.NumberFormat

class MainActivity : AppCompatActivity() {

//setting binding (the latest way of accessing views and items from resources)
private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -17,21 +18,21 @@ class MainActivity : AppCompatActivity() {

binding.calculateButton.setOnClickListener { calculateTip() }
}

//function that calculates the tip amount
private fun calculateTip() {
val stringInTextField = binding.costOfService.text.toString()
val cost = stringInTextField.toDoubleOrNull()
if (cost == null) {
binding.tipResult.text = ""
return
}

//when keyword is used here to set the value of the tip percent as per the user rates through UI
val tipPercentage = when (binding.tipOptions.checkedRadioButtonId) {
R.id.option_twenty_percent -> 0.20
R.id.option_eighteen_percent -> 0.18
else -> 0.15
}

//final calculation of tip (along with rounding it off)
var tip = tipPercentage * cost
if (binding.roundUpSwitch.isChecked) {
tip = kotlin.math.ceil(tip)
Expand All @@ -40,4 +41,4 @@ class MainActivity : AppCompatActivity() {
val formattedTip = NumberFormat.getCurrencyInstance().format(tip)
binding.tipResult.text = getString(R.string.tip_amount, formattedTip)
}
}
}

0 comments on commit 9a7924f

Please sign in to comment.