Skip to content

Commit a6a3547

Browse files
Merge pull request #461 from titan2903/main
Refactor: Apply SRP Across Multiple Modules
2 parents 7a40c5b + cc98bdf commit a6a3547

25 files changed

+1803
-889
lines changed

Calorie Calculator/CalorieCalculator.java

Lines changed: 18 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import java.util.Scanner;
1+
// Handles the business logic for calorie calculations
22

33
public 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
}

Calorie Calculator/MainApp.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Orchestrates the application flow
2+
3+
public class MainApp {
4+
public static void main(String[] args) {
5+
UserInputHandler inputHandler = new UserInputHandler();
6+
CalorieCalculator calculationService = new CalorieCalculator();
7+
ResultDisplay resultDisplay = new ResultDisplay();
8+
9+
// 1. Get user data
10+
UserData userData = inputHandler.gatherUserData();
11+
12+
// 2. Perform calculations
13+
double bmr = calculationService.calculateBMR(userData);
14+
double dailyCalorieNeeds = calculationService.calculateDailyCalorieNeeds(bmr, userData.getActivityLevel());
15+
16+
// 3. Display results
17+
resultDisplay.displayResults(bmr, dailyCalorieNeeds);
18+
19+
// Clean up resources
20+
inputHandler.closeScanner();
21+
}
22+
}

Calorie Calculator/ResultDisplay.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Responsible for displaying the calculation results
2+
3+
public class ResultDisplay {
4+
public void displayResults(double bmr, double calorieNeeds) {
5+
System.out.printf("Your Basal Metabolic Rate (BMR) is: %.0f calories per day.\n", bmr);
6+
System.out.printf("Your estimated daily calorie needs are: %.0f calories per day.\n", calorieNeeds);
7+
}
8+
}

Calorie Calculator/UserData.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Data Transfer Object to hold user's information
2+
3+
public class UserData {
4+
private String gender;
5+
private int age;
6+
private double weight;
7+
private double height;
8+
private String activityLevel;
9+
10+
// Constructor
11+
public UserData(String gender, int age, double weight, double height, String activityLevel) {
12+
this.gender = gender;
13+
this.age = age;
14+
this.weight = weight;
15+
this.height = height;
16+
this.activityLevel = activityLevel;
17+
}
18+
19+
// Getters
20+
public String getGender() {
21+
return gender;
22+
}
23+
24+
public int getAge() {
25+
return age;
26+
}
27+
28+
public double getWeight() {
29+
return weight;
30+
}
31+
32+
public double getHeight() {
33+
return height;
34+
}
35+
36+
public String getActivityLevel() {
37+
return activityLevel;
38+
}
39+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import java.util.Scanner;
2+
3+
// Handles user input and basic format validation
4+
public class UserInputHandler {
5+
private Scanner scanner;
6+
7+
public UserInputHandler() {
8+
this.scanner = new Scanner(System.in);
9+
}
10+
11+
public UserData gatherUserData() {
12+
System.out.println("Calorie Calculator");
13+
14+
String gender = getValidGenderInput();
15+
int age = getValidIntInput("Enter your age (in years): ");
16+
double weight = getValidDoubleInput("Enter your weight (in kilograms): ");
17+
double height = getValidDoubleInput("Enter your height (in centimeters): ");
18+
String activityLevel = getValidActivityLevelInput();
19+
20+
return new UserData(gender, age, weight, height, activityLevel);
21+
}
22+
23+
private String getValidGenderInput() {
24+
String gender;
25+
while (true) {
26+
System.out.print("Enter your gender (M/F): ");
27+
gender = scanner.nextLine().trim().toUpperCase();
28+
if (gender.equals("M") || gender.equals("F")) {
29+
break;
30+
}
31+
System.out.println("Invalid gender input. Please enter 'M' or 'F'.");
32+
}
33+
return gender;
34+
}
35+
36+
private int getValidIntInput(String prompt) {
37+
int value;
38+
while (true) {
39+
System.out.print(prompt);
40+
if (scanner.hasNextInt()) {
41+
value = scanner.nextInt();
42+
scanner.nextLine(); // Consume newline
43+
if (value > 0) { // Basic validation for age
44+
break;
45+
} else {
46+
System.out.println("Invalid input. Please enter a positive integer.");
47+
}
48+
} else {
49+
System.out.println("Invalid input. Please enter a valid integer.");
50+
scanner.nextLine(); // Clear invalid input
51+
}
52+
}
53+
return value;
54+
}
55+
56+
private double getValidDoubleInput(String prompt) {
57+
double value;
58+
while (true) {
59+
System.out.print(prompt);
60+
if (scanner.hasNextDouble()) {
61+
value = scanner.nextDouble();
62+
scanner.nextLine(); // Consume newline
63+
if (value > 0) { // Basic validation for weight/height
64+
break;
65+
} else {
66+
System.out.println("Invalid input. Please enter a positive number.");
67+
}
68+
} else {
69+
System.out.println("Invalid input. Please enter a valid number.");
70+
scanner.nextLine(); // Clear invalid input
71+
}
72+
}
73+
return value;
74+
}
75+
76+
private String getValidActivityLevelInput() {
77+
String activityLevel;
78+
while (true) {
79+
System.out.print("Enter your activity level (sedentary/moderate/active): ");
80+
activityLevel = scanner.nextLine().trim().toLowerCase();
81+
if (activityLevel.equals("sedentary") || activityLevel.equals("moderate") || activityLevel.equals("active")) {
82+
break;
83+
}
84+
System.out.println("Invalid activity level input. Please choose 'sedentary', 'moderate', or 'active'.");
85+
}
86+
return activityLevel;
87+
}
88+
89+
public void closeScanner() {
90+
scanner.close();
91+
}
92+
}

Dino_Game_java/Cactus.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package Dino_Game_java;
2+
3+
import java.awt.Rectangle;
4+
5+
// Represents a Cactus entity
6+
class Cactus {
7+
private int x, y, h, p; // x, y position, height, thickness parameter
8+
// Assuming p relates to width calculation. Need a clear definition.
9+
// For simplicity, let's define a width.
10+
private int width;
11+
12+
public Cactus(int x, int y, int h, int p) {
13+
this.x = x;
14+
this.y = y; // y is typically the top-left corner for rendering
15+
this.h = h;
16+
this.p = p;
17+
// Approximate width based on draw() method in original code (p*2 for central part)
18+
// and draw2() which adds more parts. This needs refinement based on exact drawing.
19+
this.width = p * 6; // A rough estimate for collision
20+
}
21+
22+
public void move(int deltaX) {
23+
this.x += deltaX;
24+
}
25+
26+
public int getX() { return x; }
27+
public int getY() { return y; }
28+
public int getH() { return h; }
29+
public int getP() { return p; }
30+
public int getWidth() { return width; } // For collision detection
31+
32+
public Rectangle getBounds() {
33+
// Bounds for collision. Assuming x,y is top-left of the main central pillar.
34+
// The actual drawn shape is more complex. This is a simplification.
35+
return new Rectangle(x, y, p * 2, h); // Central part of main cactus segment
36+
}
37+
}

Dino_Game_java/Dinosaur.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package Dino_Game_java;
2+
3+
import java.awt.Rectangle;
4+
5+
// Represents the Dinosaur entity
6+
class Dinosaur {
7+
private int x, y; // Current position
8+
private int initialY;
9+
private int jumpHeight = 280; // Max height of jump from initialY
10+
private int jumpSpeed = 20;
11+
private boolean jumping = false;
12+
private boolean jumpAscending = false; // True if going up, false if going down
13+
private int width = 5 * Game.unit; // Approximate width
14+
private int height = 10 * Game.unit; // Approximate height (can be more precise)
15+
16+
public Dinosaur(int x, int y) {
17+
this.x = x;
18+
this.y = y;
19+
this.initialY = y;
20+
}
21+
22+
// Initiates the jump sequence
23+
public void startJump() {
24+
if (!jumping) {
25+
jumping = true;
26+
jumpAscending = true;
27+
}
28+
}
29+
30+
// Updates the dinosaur's Y position during a jump
31+
public void updateJump() {
32+
if (jumping) {
33+
if (jumpAscending) {
34+
y -= jumpSpeed; // Move up
35+
if (y <= initialY - jumpHeight) { // Reached peak
36+
y = initialY - jumpHeight;
37+
jumpAscending = false; // Start descending
38+
}
39+
} else {
40+
y += jumpSpeed; // Move down
41+
if (y >= initialY) { // Landed
42+
y = initialY;
43+
jumping = false; // Jump finished
44+
}
45+
}
46+
}
47+
}
48+
49+
public boolean canJump() {
50+
return !jumping;
51+
}
52+
53+
public boolean isJumping() {
54+
return jumping;
55+
}
56+
57+
public int getX() { return x; }
58+
public int getY() { return y; }
59+
public Rectangle getBounds() {
60+
// More accurate bounds needed based on actual drawing
61+
// This is a placeholder
62+
return new Rectangle(x - 2 * Game.unit, y - 16 * Game.unit, 11 * Game.unit, 17 * Game.unit);
63+
}
64+
}

Dino_Game_java/Game.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Static unit from original Game class, can be moved to a Constants class or GameWindow
2+
class Game { // Keep this for unit, or move Game.unit to a Constants file.
3+
static int unit = 10;
4+
}

0 commit comments

Comments
 (0)