UTS-RAKA HERLAMBANG

This commit is contained in:
202310715003 RAKA HERLAMBANG 2025-11-07 12:07:08 +07:00
parent 099c35f19a
commit 5478de266b
2 changed files with 44 additions and 11 deletions

View File

@ -1,4 +1,4 @@
Kalkulator BMI
Kalkulator BMI Oleh Raka Herlambang
===============
Silahkan kembangkan aplikasi ini untuk melakukan perhitungan BMI

View File

@ -185,12 +185,25 @@ fun RoundTheTipRow(
*
* Catatan: tambahkan unit test untuk kalkulasi BMI ini
*/
private fun calculateBMI(BmiHeight: Double, BmiWeight: Double = 15.0, roundUp: Boolean): String {
var bmi = BmiWeight / 100 * BmiHeight
if (roundUp) {
bmi = kotlin.math.ceil(bmi)
//private fun calculateBMI(BmiHeight: Double, BmiWeight: Double = 15.0, roundUp: Boolean): String {
// var bmi = BmiWeight / 100 * BmiHeight
// if (roundUp) {
// 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
@ -198,13 +211,33 @@ private fun calculateBMI(BmiHeight: Double, BmiWeight: Double = 15.0, roundUp: B
* Catatan: tambahkan unit test untuk kalkulasi BMI ini
*/
private fun calculateBMICategory(BmiHeight: Double, BmiWeight: Double = 15.0, roundUp: Boolean): String {
var bmi = BmiWeight / 100 * BmiHeight
if (roundUp) {
bmi = kotlin.math.ceil(bmi)
//private fun calculateBMICategory(BmiHeight: Double, BmiWeight: Double = 15.0, roundUp: Boolean): String {
// var bmi = BmiWeight / 100 * BmiHeight
// if (roundUp) {
// 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 when {
bmiValue < 18.5 -> "Underweight"
bmiValue < 25 -> "Normal"
bmiValue < 30 -> "Overweight"
else -> "Obesity"
}
return NumberFormat.getNumberInstance().format(bmi)
}
@Preview(showBackground = true)
@Composable
fun TipTimeLayoutPreview() {