Skip to content

Commit 5104ea7

Browse files
Update CalorieCalculator.java
1 parent ad69e73 commit 5104ea7

File tree

1 file changed

+88
-27
lines changed

1 file changed

+88
-27
lines changed
Lines changed: 88 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,115 @@
11
import java.util.Scanner;
22

33
public class CalorieCalculator {
4+
5+
// Constants for BMR formula coefficients and activity multipliers
6+
private static final double MALE_BMR_CONSTANT = 88.362;
7+
private static final double FEMALE_BMR_CONSTANT = 447.593;
8+
private static final double MALE_WEIGHT_COEFFICIENT = 13.397;
9+
private static final double FEMALE_WEIGHT_COEFFICIENT = 9.247;
10+
private static final double MALE_HEIGHT_COEFFICIENT = 4.799;
11+
private static final double FEMALE_HEIGHT_COEFFICIENT = 3.098;
12+
private static final double MALE_AGE_COEFFICIENT = 5.677;
13+
private static final double FEMALE_AGE_COEFFICIENT = 4.330;
14+
15+
private static final double SEDENTARY_MULTIPLIER = 1.2;
16+
private static final double MODERATE_MULTIPLIER = 1.55;
17+
private static final double ACTIVE_MULTIPLIER = 1.725;
18+
419
public static void main(String[] args) {
520
Scanner scanner = new Scanner(System.in);
621

722
// Prompt the user for information
823
System.out.println("Calorie Calculator");
24+
925
System.out.print("Enter your gender (M/F): ");
10-
String gender = scanner.nextLine();
11-
System.out.print("Enter your age: ");
12-
int age = scanner.nextInt();
13-
System.out.print("Enter your weight in kilograms: ");
14-
double weight = scanner.nextDouble();
15-
System.out.print("Enter your height in centimeters: ");
16-
double height = scanner.nextDouble();
26+
String gender = scanner.nextLine().trim().toUpperCase();
27+
28+
if (!gender.equals("M") && !gender.equals("F")) {
29+
System.out.println("Invalid gender input. Please enter 'M' or 'F'.");
30+
return;
31+
}
32+
33+
System.out.print("Enter your age (in years): ");
34+
int age = getValidIntInput(scanner);
35+
36+
System.out.print("Enter your weight (in kilograms): ");
37+
double weight = getValidDoubleInput(scanner);
38+
39+
System.out.print("Enter your height (in centimeters): ");
40+
double height = getValidDoubleInput(scanner);
41+
1742
System.out.print("Enter your activity level (sedentary/moderate/active): ");
18-
String activityLevel = scanner.next();
43+
String activityLevel = scanner.nextLine().trim().toLowerCase();
44+
45+
if (!isValidActivityLevel(activityLevel)) {
46+
System.out.println("Invalid activity level input. Please choose 'sedentary', 'moderate', or 'active'.");
47+
return;
48+
}
1949

2050
// Calculate BMR
51+
double bmr = calculateBMR(gender, age, weight, height);
52+
53+
// Calculate daily calorie needs based on activity level
54+
double calorieNeeds = calculateCalorieNeeds(bmr, activityLevel);
55+
56+
// Display the results
57+
System.out.printf("Your Basal Metabolic Rate (BMR) is: %.0f calories per day.\n", bmr);
58+
System.out.printf("Your estimated daily calorie needs are: %.0f calories per day.\n", calorieNeeds);
59+
60+
scanner.close();
61+
}
62+
63+
// Method to get a valid integer input from the user
64+
private static int getValidIntInput(Scanner scanner) {
65+
while (!scanner.hasNextInt()) {
66+
System.out.println("Invalid input. Please enter a valid integer.");
67+
scanner.next(); // Clear invalid input
68+
}
69+
return scanner.nextInt();
70+
}
71+
72+
// Method to get a valid double input from the user
73+
private static double getValidDoubleInput(Scanner scanner) {
74+
while (!scanner.hasNextDouble()) {
75+
System.out.println("Invalid input. Please enter a valid number.");
76+
scanner.next(); // Clear invalid input
77+
}
78+
return scanner.nextDouble();
79+
}
80+
81+
// Method to check if the activity level is valid
82+
private static boolean isValidActivityLevel(String activityLevel) {
83+
return activityLevel.equals("sedentary") || activityLevel.equals("moderate") || activityLevel.equals("active");
84+
}
85+
86+
// Method to calculate BMR
87+
private static double calculateBMR(String gender, int age, double weight, double height) {
2188
double bmr;
22-
if (gender.equalsIgnoreCase("M")) {
23-
bmr = Math.round(88.362 + (13.397 * weight) + (4.799 * height) - (5.677 * age));
24-
} else if (gender.equalsIgnoreCase("F")) {
25-
bmr = Math.round(447.593 + (9.247 * weight) + (3.098 * height) - (4.330 * age));
89+
if (gender.equals("M")) {
90+
bmr = MALE_BMR_CONSTANT + (MALE_WEIGHT_COEFFICIENT * weight) + (MALE_HEIGHT_COEFFICIENT * height) - (MALE_AGE_COEFFICIENT * age);
2691
} else {
27-
System.out.println("Invalid gender input.");
28-
return;
92+
bmr = FEMALE_BMR_CONSTANT + (FEMALE_WEIGHT_COEFFICIENT * weight) + (FEMALE_HEIGHT_COEFFICIENT * height) - (FEMALE_AGE_COEFFICIENT * age);
2993
}
94+
return bmr;
95+
}
3096

31-
// Calculate daily calorie needs based on activity level
97+
// Method to calculate calorie needs based on activity level
98+
private static double calculateCalorieNeeds(double bmr, String activityLevel) {
3299
double calorieNeeds;
33-
switch (activityLevel.toLowerCase()) {
100+
switch (activityLevel) {
34101
case "sedentary":
35-
calorieNeeds = Math.round(bmr * 1.2);
102+
calorieNeeds = bmr * SEDENTARY_MULTIPLIER;
36103
break;
37104
case "moderate":
38-
calorieNeeds = Math.round(bmr * 1.55);
105+
calorieNeeds = bmr * MODERATE_MULTIPLIER;
39106
break;
40107
case "active":
41-
calorieNeeds = Math.round(bmr * 1.725);
108+
calorieNeeds = bmr * ACTIVE_MULTIPLIER;
42109
break;
43110
default:
44-
System.out.println("Invalid activity level input.");
45-
return;
111+
throw new IllegalArgumentException("Invalid activity level");
46112
}
47-
48-
// Display the results
49-
System.out.println("Your Basal Metabolic Rate (BMR) is: " + (int) bmr + " calories per day.");
50-
System.out.println("Your estimated daily calorie needs are: " + (int) calorieNeeds + " calories per day.");
51-
52-
scanner.close();
113+
return calorieNeeds;
53114
}
54115
}

0 commit comments

Comments
 (0)