1- import java . util . Scanner ;
1+ // Handles the business logic for calorie calculations
22
33public class CalorieCalculator {
44
@@ -16,88 +16,27 @@ public class CalorieCalculator {
1616 private static final double MODERATE_MULTIPLIER = 1.55 ;
1717 private static final double ACTIVE_MULTIPLIER = 1.725 ;
1818
19- public static void main (String [] args ) {
20- Scanner scanner = new Scanner (System .in );
21-
22- // Prompt the user for information
23- System .out .println ("Calorie Calculator" );
24-
25- System .out .print ("Enter your gender (M/F): " );
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-
42- System .out .print ("Enter your activity level (sedentary/moderate/active): " );
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- }
49-
50- // 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 ) {
19+ public double calculateBMR (UserData userData ) {
8820 double bmr ;
89- if (gender .equals ("M" )) {
90- bmr = MALE_BMR_CONSTANT + (MALE_WEIGHT_COEFFICIENT * weight ) + (MALE_HEIGHT_COEFFICIENT * height ) - (MALE_AGE_COEFFICIENT * age );
91- } else {
92- bmr = FEMALE_BMR_CONSTANT + (FEMALE_WEIGHT_COEFFICIENT * weight ) + (FEMALE_HEIGHT_COEFFICIENT * height ) - (FEMALE_AGE_COEFFICIENT * age );
21+ if (userData .getGender ().equals ("M" )) {
22+ bmr = MALE_BMR_CONSTANT +
23+ (MALE_WEIGHT_COEFFICIENT * userData .getWeight ()) +
24+ (MALE_HEIGHT_COEFFICIENT * userData .getHeight ()) -
25+ (MALE_AGE_COEFFICIENT * userData .getAge ());
26+ } else { // Assumes "F" as gender has been validated by input handler
27+ bmr = FEMALE_BMR_CONSTANT +
28+ (FEMALE_WEIGHT_COEFFICIENT * userData .getWeight ()) +
29+ (FEMALE_HEIGHT_COEFFICIENT * userData .getHeight ()) -
30+ (FEMALE_AGE_COEFFICIENT * userData .getAge ());
9331 }
9432 return bmr ;
9533 }
9634
97- // Method to calculate calorie needs based on activity level
98- private static double calculateCalorieNeeds (double bmr , String activityLevel ) {
35+ public double calculateDailyCalorieNeeds (double bmr , String activityLevel ) {
36+ // Domain validation for activityLevel could be here if not handled before
37+ // For this example, assuming activityLevel is already validated
9938 double calorieNeeds ;
100- switch (activityLevel ) {
39+ switch (activityLevel . toLowerCase () ) {
10140 case "sedentary" :
10241 calorieNeeds = bmr * SEDENTARY_MULTIPLIER ;
10342 break ;
@@ -108,7 +47,8 @@ private static double calculateCalorieNeeds(double bmr, String activityLevel) {
10847 calorieNeeds = bmr * ACTIVE_MULTIPLIER ;
10948 break ;
11049 default :
111- throw new IllegalArgumentException ("Invalid activity level" );
50+ // This case should ideally not be reached if input is validated properly
51+ throw new IllegalArgumentException ("Invalid activity level: " + activityLevel );
11252 }
11353 return calorieNeeds ;
11454 }
0 commit comments