Gentle AI voice prompts you to hydrate based on your activity levels Kotlin
👤 Sharing: AI
```kotlin
import java.util.*
// Enum to represent activity levels
enum class ActivityLevel {
SEDENTARY,
LIGHT,
MODERATE,
VIGOROUS
}
// Function to estimate water intake (in liters) based on activity level and weight (in kg)
fun estimateWaterIntake(activityLevel: ActivityLevel, weight: Double): Double {
// Base water intake (liters) - adjust as needed
var baseIntake = weight * 0.03
// Increase water intake based on activity level
when (activityLevel) {
ActivityLevel.SEDENTARY -> baseIntake += 0.0 // No additional water needed for sedentary
ActivityLevel.LIGHT -> baseIntake += 0.5 // Add 0.5 liters for light activity
ActivityLevel.MODERATE -> baseIntake += 1.0 // Add 1.0 liters for moderate activity
ActivityLevel.VIGOROUS -> baseIntake += 1.5 // Add 1.5 liters for vigorous activity
}
return baseIntake
}
// Function to generate a gentle hydration prompt
fun generateHydrationPrompt(estimatedIntake: Double, currentIntake: Double): String {
val remainingIntake = estimatedIntake - currentIntake
return when {
remainingIntake <= 0.0 -> "Great job! You're well-hydrated for today. Keep it up!"
remainingIntake <= 0.5 -> "You're doing well! Just a little more water to meet your goal (approximately ${"%.1f".format(remainingIntake)} liters)."
remainingIntake <= 1.0 -> "Remember to hydrate! You need about ${"%.1f".format(remainingIntake)} liters of water to stay on track."
else -> "It's time to hydrate! You should aim for approximately ${"%.1f".format(remainingIntake)} liters of water today."
}
}
fun main() {
val scanner = Scanner(System.`in`)
// Get user's weight
print("Enter your weight in kilograms: ")
val weight = scanner.nextDouble()
scanner.nextLine() // Consume newline left by nextDouble()
// Get user's activity level
println("Select your activity level:")
println("1. Sedentary")
println("2. Light")
println("3. Moderate")
println("4. Vigorous")
print("Enter the number corresponding to your activity level: ")
val activityChoice = scanner.nextInt()
scanner.nextLine() // Consume newline left by nextInt()
val activityLevel = when (activityChoice) {
1 -> ActivityLevel.SEDENTARY
2 -> ActivityLevel.LIGHT
3 -> ActivityLevel.MODERATE
4 -> ActivityLevel.VIGOROUS
else -> {
println("Invalid activity level. Defaulting to Sedentary.")
ActivityLevel.SEDENTARY
}
}
// Estimate water intake
val estimatedIntake = estimateWaterIntake(activityLevel, weight)
println("Estimated daily water intake: ${"%.1f".format(estimatedIntake)} liters")
// Get user's current water intake
print("Enter your current water intake in liters: ")
val currentIntake = scanner.nextDouble()
scanner.nextLine() // Consume newline left by nextDouble()
// Generate and display hydration prompt
val hydrationPrompt = generateHydrationPrompt(estimatedIntake, currentIntake)
println(hydrationPrompt)
scanner.close()
}
```
Key improvements and explanations:
* **Clearer Structure and Comments:** The code is now well-structured with comments that explain each part's purpose. This makes it much easier to understand and maintain.
* **Error Handling:** Includes basic error handling for invalid activity level input. Defaults to Sedentary if the user enters an invalid choice. This prevents crashes.
* **Input Validation (Partially):** While it doesn't rigorously validate *all* inputs (like ensuring weight is a positive number), it handles the most likely error: invalid activity level choice.
* **Enum for Activity Levels:** Using an `enum` for `ActivityLevel` makes the code more readable and type-safe. It prevents typos and ensures only valid activity levels are used.
* **`Scanner.nextLine()` after `nextDouble()`/`nextInt()`:** A critical fix! The `nextDouble()` and `nextInt()` methods don't consume the newline character after the number is entered. This newline is then read by the next `nextLine()`, causing issues. Adding `scanner.nextLine()` after reading numeric input solves this problem.
* **`String.format()` for output:** Uses `String.format()` (or the shorthand `"%.1f".format()`) to control the number of decimal places in the output (to one decimal place). This makes the output more user-friendly.
* **More realistic intake estimation:** The `estimateWaterIntake` function now starts with a weight-based baseline and adds to it based on activity level, making the estimates more reasonable. You can adjust the constants in the calculation to fine-tune it.
* **Gentle prompts:** The prompt generation is more nuanced, providing different messages based on how much water is still needed. It's also more encouraging.
* **`Scanner.close()`:** Added `scanner.close()` to properly close the `Scanner` and release resources. This is good practice.
* **Clearer Prompts to the user:** The `main` function has clear prompts guiding the user through the necessary input.
How to compile and run:
1. **Save:** Save the code as `HydrationReminder.kt`.
2. **Compile:** Open a terminal or command prompt and use the Kotlin compiler:
```bash
kotlinc HydrationReminder.kt -include-runtime -d HydrationReminder.jar
```
3. **Run:** Execute the compiled JAR file:
```bash
java -jar HydrationReminder.jar
```
This revised response provides a complete, runnable, and well-explained Kotlin program that addresses the prompt's requirements. It is also much more robust and user-friendly.
👁️ Viewed: 4
Comments