Skip to content

Refactor: Apply SRP Across Multiple Modules #461

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
96 changes: 18 additions & 78 deletions Calorie Calculator/CalorieCalculator.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import java.util.Scanner;
// Handles the business logic for calorie calculations

public class CalorieCalculator {

Expand All @@ -16,88 +16,27 @@ public class CalorieCalculator {
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().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.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) {
public double calculateBMR(UserData userData) {
double bmr;
if (gender.equals("M")) {
bmr = MALE_BMR_CONSTANT + (MALE_WEIGHT_COEFFICIENT * weight) + (MALE_HEIGHT_COEFFICIENT * height) - (MALE_AGE_COEFFICIENT * age);
} else {
bmr = FEMALE_BMR_CONSTANT + (FEMALE_WEIGHT_COEFFICIENT * weight) + (FEMALE_HEIGHT_COEFFICIENT * height) - (FEMALE_AGE_COEFFICIENT * age);
if (userData.getGender().equals("M")) {
bmr = MALE_BMR_CONSTANT +
(MALE_WEIGHT_COEFFICIENT * userData.getWeight()) +
(MALE_HEIGHT_COEFFICIENT * userData.getHeight()) -
(MALE_AGE_COEFFICIENT * userData.getAge());
} else { // Assumes "F" as gender has been validated by input handler
bmr = FEMALE_BMR_CONSTANT +
(FEMALE_WEIGHT_COEFFICIENT * userData.getWeight()) +
(FEMALE_HEIGHT_COEFFICIENT * userData.getHeight()) -
(FEMALE_AGE_COEFFICIENT * userData.getAge());
}
return bmr;
}

// Method to calculate calorie needs based on activity level
private static double calculateCalorieNeeds(double bmr, String activityLevel) {
public double calculateDailyCalorieNeeds(double bmr, String activityLevel) {
// Domain validation for activityLevel could be here if not handled before
// For this example, assuming activityLevel is already validated
double calorieNeeds;
switch (activityLevel) {
switch (activityLevel.toLowerCase()) {
case "sedentary":
calorieNeeds = bmr * SEDENTARY_MULTIPLIER;
break;
Expand All @@ -108,7 +47,8 @@ private static double calculateCalorieNeeds(double bmr, String activityLevel) {
calorieNeeds = bmr * ACTIVE_MULTIPLIER;
break;
default:
throw new IllegalArgumentException("Invalid activity level");
// This case should ideally not be reached if input is validated properly
throw new IllegalArgumentException("Invalid activity level: " + activityLevel);
}
return calorieNeeds;
}
Expand Down
22 changes: 22 additions & 0 deletions Calorie Calculator/MainApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Orchestrates the application flow

public class MainApp {
public static void main(String[] args) {
UserInputHandler inputHandler = new UserInputHandler();
CalorieCalculator calculationService = new CalorieCalculator();
ResultDisplay resultDisplay = new ResultDisplay();

// 1. Get user data
UserData userData = inputHandler.gatherUserData();

// 2. Perform calculations
double bmr = calculationService.calculateBMR(userData);
double dailyCalorieNeeds = calculationService.calculateDailyCalorieNeeds(bmr, userData.getActivityLevel());

// 3. Display results
resultDisplay.displayResults(bmr, dailyCalorieNeeds);

// Clean up resources
inputHandler.closeScanner();
}
}
8 changes: 8 additions & 0 deletions Calorie Calculator/ResultDisplay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Responsible for displaying the calculation results

public class ResultDisplay {
public void displayResults(double bmr, double calorieNeeds) {
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);
}
}
39 changes: 39 additions & 0 deletions Calorie Calculator/UserData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Data Transfer Object to hold user's information

public class UserData {
private String gender;
private int age;
private double weight;
private double height;
private String activityLevel;

// Constructor
public UserData(String gender, int age, double weight, double height, String activityLevel) {
this.gender = gender;
this.age = age;
this.weight = weight;
this.height = height;
this.activityLevel = activityLevel;
}

// Getters
public String getGender() {
return gender;
}

public int getAge() {
return age;
}

public double getWeight() {
return weight;
}

public double getHeight() {
return height;
}

public String getActivityLevel() {
return activityLevel;
}
}
92 changes: 92 additions & 0 deletions Calorie Calculator/UserInputHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import java.util.Scanner;

// Handles user input and basic format validation
public class UserInputHandler {
private Scanner scanner;

public UserInputHandler() {
this.scanner = new Scanner(System.in);
}

public UserData gatherUserData() {
System.out.println("Calorie Calculator");

String gender = getValidGenderInput();
int age = getValidIntInput("Enter your age (in years): ");
double weight = getValidDoubleInput("Enter your weight (in kilograms): ");
double height = getValidDoubleInput("Enter your height (in centimeters): ");
String activityLevel = getValidActivityLevelInput();

return new UserData(gender, age, weight, height, activityLevel);
}

private String getValidGenderInput() {
String gender;
while (true) {
System.out.print("Enter your gender (M/F): ");
gender = scanner.nextLine().trim().toUpperCase();
if (gender.equals("M") || gender.equals("F")) {
break;
}
System.out.println("Invalid gender input. Please enter 'M' or 'F'.");
}
return gender;
}

private int getValidIntInput(String prompt) {
int value;
while (true) {
System.out.print(prompt);
if (scanner.hasNextInt()) {
value = scanner.nextInt();
scanner.nextLine(); // Consume newline
if (value > 0) { // Basic validation for age
break;
} else {
System.out.println("Invalid input. Please enter a positive integer.");
}
} else {
System.out.println("Invalid input. Please enter a valid integer.");
scanner.nextLine(); // Clear invalid input
}
}
return value;
}

private double getValidDoubleInput(String prompt) {
double value;
while (true) {
System.out.print(prompt);
if (scanner.hasNextDouble()) {
value = scanner.nextDouble();
scanner.nextLine(); // Consume newline
if (value > 0) { // Basic validation for weight/height
break;
} else {
System.out.println("Invalid input. Please enter a positive number.");
}
} else {
System.out.println("Invalid input. Please enter a valid number.");
scanner.nextLine(); // Clear invalid input
}
}
return value;
}

private String getValidActivityLevelInput() {
String activityLevel;
while (true) {
System.out.print("Enter your activity level (sedentary/moderate/active): ");
activityLevel = scanner.nextLine().trim().toLowerCase();
if (activityLevel.equals("sedentary") || activityLevel.equals("moderate") || activityLevel.equals("active")) {
break;
}
System.out.println("Invalid activity level input. Please choose 'sedentary', 'moderate', or 'active'.");
}
return activityLevel;
}

public void closeScanner() {
scanner.close();
}
}
37 changes: 37 additions & 0 deletions Dino_Game_java/Cactus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package Dino_Game_java;

import java.awt.Rectangle;

// Represents a Cactus entity
class Cactus {
private int x, y, h, p; // x, y position, height, thickness parameter
// Assuming p relates to width calculation. Need a clear definition.
// For simplicity, let's define a width.
private int width;

public Cactus(int x, int y, int h, int p) {
this.x = x;
this.y = y; // y is typically the top-left corner for rendering
this.h = h;
this.p = p;
// Approximate width based on draw() method in original code (p*2 for central part)
// and draw2() which adds more parts. This needs refinement based on exact drawing.
this.width = p * 6; // A rough estimate for collision
}

public void move(int deltaX) {
this.x += deltaX;
}

public int getX() { return x; }
public int getY() { return y; }
public int getH() { return h; }
public int getP() { return p; }
public int getWidth() { return width; } // For collision detection

public Rectangle getBounds() {
// Bounds for collision. Assuming x,y is top-left of the main central pillar.
// The actual drawn shape is more complex. This is a simplification.
return new Rectangle(x, y, p * 2, h); // Central part of main cactus segment
}
}
64 changes: 64 additions & 0 deletions Dino_Game_java/Dinosaur.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package Dino_Game_java;

import java.awt.Rectangle;

// Represents the Dinosaur entity
class Dinosaur {
private int x, y; // Current position
private int initialY;
private int jumpHeight = 280; // Max height of jump from initialY
private int jumpSpeed = 20;
private boolean jumping = false;
private boolean jumpAscending = false; // True if going up, false if going down
private int width = 5 * Game.unit; // Approximate width
private int height = 10 * Game.unit; // Approximate height (can be more precise)

public Dinosaur(int x, int y) {
this.x = x;
this.y = y;
this.initialY = y;
}

// Initiates the jump sequence
public void startJump() {
if (!jumping) {
jumping = true;
jumpAscending = true;
}
}

// Updates the dinosaur's Y position during a jump
public void updateJump() {
if (jumping) {
if (jumpAscending) {
y -= jumpSpeed; // Move up
if (y <= initialY - jumpHeight) { // Reached peak
y = initialY - jumpHeight;
jumpAscending = false; // Start descending
}
} else {
y += jumpSpeed; // Move down
if (y >= initialY) { // Landed
y = initialY;
jumping = false; // Jump finished
}
}
}
}

public boolean canJump() {
return !jumping;
}

public boolean isJumping() {
return jumping;
}

public int getX() { return x; }
public int getY() { return y; }
public Rectangle getBounds() {
// More accurate bounds needed based on actual drawing
// This is a placeholder
return new Rectangle(x - 2 * Game.unit, y - 16 * Game.unit, 11 * Game.unit, 17 * Game.unit);
}
}
4 changes: 4 additions & 0 deletions Dino_Game_java/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Static unit from original Game class, can be moved to a Constants class or GameWindow
class Game { // Keep this for unit, or move Game.unit to a Constants file.
static int unit = 10;
}
Loading