Skip to content

Update CalorieCalculator.java #452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 88 additions & 27 deletions Calorie Calculator/CalorieCalculator.java
Original file line number Diff line number Diff line change
@@ -1,54 +1,115 @@
import java.util.Scanner;

public class CalorieCalculator {

// Constants for BMR formula coefficients and activity multipliers
private static final double MALE_BMR_CONSTANT = 88.362;
private static final double FEMALE_BMR_CONSTANT = 447.593;
private static final double MALE_WEIGHT_COEFFICIENT = 13.397;
private static final double FEMALE_WEIGHT_COEFFICIENT = 9.247;
private static final double MALE_HEIGHT_COEFFICIENT = 4.799;
private static final double FEMALE_HEIGHT_COEFFICIENT = 3.098;
private static final double MALE_AGE_COEFFICIENT = 5.677;
private static final double FEMALE_AGE_COEFFICIENT = 4.330;

private static final double SEDENTARY_MULTIPLIER = 1.2;
private static final double MODERATE_MULTIPLIER = 1.55;
private static final double ACTIVE_MULTIPLIER = 1.725;

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Prompt the user for information
System.out.println("Calorie Calculator");

System.out.print("Enter your gender (M/F): ");
String gender = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.print("Enter your weight in kilograms: ");
double weight = scanner.nextDouble();
System.out.print("Enter your height in centimeters: ");
double height = scanner.nextDouble();
String gender = scanner.nextLine().trim().toUpperCase();

if (!gender.equals("M") && !gender.equals("F")) {
System.out.println("Invalid gender input. Please enter 'M' or 'F'.");
return;
}

System.out.print("Enter your age (in years): ");
int age = getValidIntInput(scanner);

System.out.print("Enter your weight (in kilograms): ");
double weight = getValidDoubleInput(scanner);

System.out.print("Enter your height (in centimeters): ");
double height = getValidDoubleInput(scanner);

System.out.print("Enter your activity level (sedentary/moderate/active): ");
String activityLevel = scanner.next();
String activityLevel = scanner.nextLine().trim().toLowerCase();

if (!isValidActivityLevel(activityLevel)) {
System.out.println("Invalid activity level input. Please choose 'sedentary', 'moderate', or 'active'.");
return;
}

// Calculate BMR
double bmr = calculateBMR(gender, age, weight, height);

// Calculate daily calorie needs based on activity level
double calorieNeeds = calculateCalorieNeeds(bmr, activityLevel);

// Display the results
System.out.printf("Your Basal Metabolic Rate (BMR) is: %.0f calories per day.\n", bmr);
System.out.printf("Your estimated daily calorie needs are: %.0f calories per day.\n", calorieNeeds);

scanner.close();
}

// Method to get a valid integer input from the user
private static int getValidIntInput(Scanner scanner) {
while (!scanner.hasNextInt()) {
System.out.println("Invalid input. Please enter a valid integer.");
scanner.next(); // Clear invalid input
}
return scanner.nextInt();
}

// Method to get a valid double input from the user
private static double getValidDoubleInput(Scanner scanner) {
while (!scanner.hasNextDouble()) {
System.out.println("Invalid input. Please enter a valid number.");
scanner.next(); // Clear invalid input
}
return scanner.nextDouble();
}

// Method to check if the activity level is valid
private static boolean isValidActivityLevel(String activityLevel) {
return activityLevel.equals("sedentary") || activityLevel.equals("moderate") || activityLevel.equals("active");
}

// Method to calculate BMR
private static double calculateBMR(String gender, int age, double weight, double height) {
double bmr;
if (gender.equalsIgnoreCase("M")) {
bmr = Math.round(88.362 + (13.397 * weight) + (4.799 * height) - (5.677 * age));
} else if (gender.equalsIgnoreCase("F")) {
bmr = Math.round(447.593 + (9.247 * weight) + (3.098 * height) - (4.330 * age));
if (gender.equals("M")) {
bmr = MALE_BMR_CONSTANT + (MALE_WEIGHT_COEFFICIENT * weight) + (MALE_HEIGHT_COEFFICIENT * height) - (MALE_AGE_COEFFICIENT * age);
} else {
System.out.println("Invalid gender input.");
return;
bmr = FEMALE_BMR_CONSTANT + (FEMALE_WEIGHT_COEFFICIENT * weight) + (FEMALE_HEIGHT_COEFFICIENT * height) - (FEMALE_AGE_COEFFICIENT * age);
}
return bmr;
}

// Calculate daily calorie needs based on activity level
// Method to calculate calorie needs based on activity level
private static double calculateCalorieNeeds(double bmr, String activityLevel) {
double calorieNeeds;
switch (activityLevel.toLowerCase()) {
switch (activityLevel) {
case "sedentary":
calorieNeeds = Math.round(bmr * 1.2);
calorieNeeds = bmr * SEDENTARY_MULTIPLIER;
break;
case "moderate":
calorieNeeds = Math.round(bmr * 1.55);
calorieNeeds = bmr * MODERATE_MULTIPLIER;
break;
case "active":
calorieNeeds = Math.round(bmr * 1.725);
calorieNeeds = bmr * ACTIVE_MULTIPLIER;
break;
default:
System.out.println("Invalid activity level input.");
return;
throw new IllegalArgumentException("Invalid activity level");
}

// Display the results
System.out.println("Your Basal Metabolic Rate (BMR) is: " + (int) bmr + " calories per day.");
System.out.println("Your estimated daily calorie needs are: " + (int) calorieNeeds + " calories per day.");

scanner.close();
return calorieNeeds;
}
}