Last active
July 23, 2020 17:35
-
-
Save TheDancerCodes/f02761fdff9f2ed876c5dd3820fcf72c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun computeTipAndTotal() { | |
// 10 - Conditional check to fix java.lang.NumberFormatException: empty String crash, | |
// when edit text is empty. | |
if (billEditText.text.isEmpty()) { | |
// 10b) Update values of tip and total to empty & return here | |
tvTipAmount.text = "$INITIAL_EMPTY_VALUE" | |
tvTotalAmount.text = "$INITIAL_EMPTY_VALUE" | |
return // This ensures the logic in 7b isn't run. 👍 | |
} | |
//7b) Get the value of the bill and tip percent | |
// Once we have these values, we can calculate the amount for the tip, (tip% x bill) | |
// We can also calculate the value for the total (tip + bill) | |
// Bill value - value inside edit text | |
// Call to string to change int to string. toDouble so that we can do math. | |
val billAmount = billEditText.text.toString().toDouble() | |
// Tip %age - The progress of the SeekBar | |
val tipPercent = tipSeekBar.progress | |
// Calculate the tip amount | |
val tipAmount = billAmount * tipPercent / 100 | |
// Calculate total amount | |
val totalAmount = billAmount + tipAmount | |
// 8) NOTE: After the calculation, place the tipAmount and totalAmount, into | |
// the respective textViews. | |
tvTipAmount.text = "%.2f".format(tipAmount) | |
tvTotalAmount.text = "%.2f".format(totalAmount) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment