UTS-DENZEL CHANDRA ADHAM-202310715037

This commit is contained in:
202310715037 DENZEL CHANDRA ADHAM ATHALLAH 2025-11-07 09:42:54 +07:00
parent 099c35f19a
commit 4b8fead43c

View File

@ -185,12 +185,25 @@ fun RoundTheTipRow(
* *
* Catatan: tambahkan unit test untuk kalkulasi BMI ini * Catatan: tambahkan unit test untuk kalkulasi BMI ini
*/ */
private fun calculateBMI(BmiHeight: Double, BmiWeight: Double = 15.0, roundUp: Boolean): String { //private fun calculateBMI(BmiHeight: Double, BmiWeight: Double = 15.0, roundUp: Boolean): String {
var bmi = BmiWeight / 100 * BmiHeight // var bmi = BmiWeight / 100 * BmiHeight
if (roundUp) { // if (roundUp) {
bmi = kotlin.math.ceil(bmi) // bmi = kotlin.math.ceil(bmi)
// }
// return NumberFormat.getNumberInstance().format(bmi)
//}
private fun calculateBMI(BmiHeight: Double, BmiWeight: Double, useUSC: Boolean): String {
if (BmiHeight <= 0 || BmiWeight <= 0) return "0.0"
val bmi = if (useUSC) {
// Rumus USC: 703 * weight(lb) / height(in)^2
703 * BmiWeight / (BmiHeight * BmiHeight)
} else {
// Rumus SI: weight(kg) / (height(m))^2
BmiWeight / ((BmiHeight / 100) * (BmiHeight / 100))
} }
return NumberFormat.getNumberInstance().format(bmi)
return String.format("%.2f", bmi)
} }
/** /**
* Calculates the BMI Category * Calculates the BMI Category
@ -198,13 +211,33 @@ private fun calculateBMI(BmiHeight: Double, BmiWeight: Double = 15.0, roundUp: B
* Catatan: tambahkan unit test untuk kalkulasi BMI ini * Catatan: tambahkan unit test untuk kalkulasi BMI ini
*/ */
private fun calculateBMICategory(BmiHeight: Double, BmiWeight: Double = 15.0, roundUp: Boolean): String { //private fun calculateBMICategory(BmiHeight: Double, BmiWeight: Double = 15.0, roundUp: Boolean): String {
var bmi = BmiWeight / 100 * BmiHeight // var bmi = BmiWeight / 100 * BmiHeight
if (roundUp) { // if (roundUp) {
bmi = kotlin.math.ceil(bmi) // bmi = kotlin.math.ceil(bmi)
// }
// return NumberFormat.getNumberInstance().format(bmi)
//}
/**
* Menentukan kategori BMI berdasarkan nilai BMI
*/
private fun calculateBMICategory(BmiHeight: Double, BmiWeight: Double, useUSC: Boolean): String {
if (BmiHeight <= 0 || BmiWeight <= 0) return "Input tidak valid"
val bmiValue = if (useUSC) {
703 * BmiWeight / (BmiHeight * BmiHeight)
} else {
BmiWeight / ((BmiHeight / 100) * (BmiHeight / 100))
} }
return NumberFormat.getNumberInstance().format(bmi)
return when {
bmiValue < 18.5 -> "Underweight"
bmiValue < 25 -> "Normal"
bmiValue < 30 -> "Overweight"
else -> "Obesity"
} }
}
@Preview(showBackground = true) @Preview(showBackground = true)
@Composable @Composable
fun TipTimeLayoutPreview() { fun TipTimeLayoutPreview() {