@@ -0,0 +1,85 @@ | |||
*.db | |||
############################## | |||
## Java | |||
############################## | |||
.mtj.tmp/ | |||
*.class | |||
*.jar | |||
*.war | |||
*.ear | |||
*.nar | |||
hs_err_pid* | |||
############################## | |||
## Maven | |||
############################## | |||
target/ | |||
pom.xml.tag | |||
pom.xml.releaseBackup | |||
pom.xml.versionsBackup | |||
pom.xml.next | |||
pom.xml.bak | |||
release.properties | |||
dependency-reduced-pom.xml | |||
buildNumber.properties | |||
.mvn/timing.properties | |||
.mvn/wrapper/maven-wrapper.jar | |||
############################## | |||
## Gradle | |||
############################## | |||
bin/ | |||
build/ | |||
.gradle | |||
.gradletasknamecache | |||
gradle-app.setting | |||
!gradle-wrapper.jar | |||
############################## | |||
## IntelliJ | |||
############################## | |||
out/ | |||
.idea/ | |||
.idea_modules/ | |||
*.iml | |||
*.ipr | |||
*.iws | |||
############################## | |||
## Eclipse | |||
############################## | |||
.settings/ | |||
bin/ | |||
tmp/ | |||
.metadata | |||
.classpath | |||
.project | |||
*.tmp | |||
*.bak | |||
*.swp | |||
*~.nib | |||
local.properties | |||
.loadpath | |||
.factorypath | |||
############################## | |||
## NetBeans | |||
############################## | |||
nbproject/private/ | |||
build/ | |||
nbbuild/ | |||
dist/ | |||
nbdist/ | |||
nbactions.xml | |||
nb-configuration.xml | |||
############################## | |||
## Visual Studio Code | |||
############################## | |||
.vscode/ | |||
.code-workspace | |||
############################## | |||
## OS X | |||
############################## | |||
.DS_Store |
@@ -0,0 +1,25 @@ | |||
<?xml version="1.0" encoding="UTF-8"?> | |||
<project xmlns="http://maven.apache.org/POM/4.0.0" | |||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |||
<modelVersion>4.0.0</modelVersion> | |||
<groupId>com.javafanatics</groupId> | |||
<artifactId>healty-coder</artifactId> | |||
<version>1.0-SNAPSHOT</version> | |||
<packaging>jar</packaging> | |||
<properties> | |||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||
<maven.compiler.source>11</maven.compiler.source> | |||
<maven.compiler.target>11</maven.compiler.target> | |||
</properties> | |||
<dependencies> | |||
<dependency> | |||
<groupId>org.junit.jupiter</groupId> | |||
<artifactId>junit-jupiter</artifactId> | |||
<version>5.7.1</version> | |||
<scope>test</scope> | |||
</dependency> | |||
</dependencies> | |||
</project> |
@@ -0,0 +1,40 @@ | |||
package com.javafanatics; | |||
import java.util.Comparator; | |||
import java.util.List; | |||
public class BMICalculator { | |||
private static final double BMI_THRESHOLD = 25.0; | |||
public static boolean isDietRecommended(double weight, double height) { | |||
if (height == 0.0) throw new ArithmeticException(); | |||
double bmi = weight / (height * height); | |||
if (bmi < BMI_THRESHOLD) | |||
return false; | |||
return true; | |||
} | |||
public static Coder findCoderWithWorstBMI(List<Coder> coders) { | |||
return coders.stream().sorted(Comparator.comparing(BMICalculator::calculateBMI)) | |||
.reduce((first, second) -> second).orElse(null); | |||
} | |||
public static double[] getBMIScores(List<Coder> coders) { | |||
double[] bmiScores = new double[coders.size()]; | |||
for (int i = 0; i < bmiScores.length; i++) { | |||
bmiScores[i] = BMICalculator.calculateBMI(coders.get(i)); | |||
} | |||
return bmiScores; | |||
} | |||
private static double calculateBMI(Coder coder) { | |||
double height = coder.getHeight(); | |||
double weight = coder.getWeight(); | |||
if (height == 0.0) | |||
throw new ArithmeticException(); | |||
double bmi = weight / (height * height); | |||
return Math.round(bmi * 100) / 100.0; | |||
} | |||
} |
@@ -0,0 +1,48 @@ | |||
package com.javafanatics; | |||
public class Coder { | |||
private double height; | |||
private double weight; | |||
private int age; | |||
private Gender gender; | |||
public Coder(double height, double weight) { | |||
super(); | |||
this.height = height; | |||
this.weight = weight; | |||
} | |||
public Coder(double height, double weight, int age, Gender gender) { | |||
super(); | |||
this.height = height; | |||
this.weight = weight; | |||
this.age = age; | |||
this.gender = gender; | |||
} | |||
public double getHeight() { | |||
return height; | |||
} | |||
public void setHeight(double height) { | |||
this.height = height; | |||
} | |||
public double getWeight() { | |||
return weight; | |||
} | |||
public void setWeight(double weight) { | |||
this.weight = weight; | |||
} | |||
public int getAge() { | |||
return age; | |||
} | |||
public void setAge(int age) { | |||
this.age = age; | |||
} | |||
public Gender getGender() { | |||
return gender; | |||
} | |||
public void setGender(Gender gender) { | |||
this.gender = gender; | |||
} | |||
} |
@@ -0,0 +1,47 @@ | |||
package com.javafanatics; | |||
public class DietPlan { | |||
private int calories; | |||
private int protein; | |||
private int fat; | |||
private int carbohydrate; | |||
public DietPlan(int calories, int protein, int fat, int carbohydrate) { | |||
super(); | |||
this.calories = calories; | |||
this.protein = protein; | |||
this.fat = fat; | |||
this.carbohydrate = carbohydrate; | |||
} | |||
@Override | |||
public String toString() { | |||
return "Diet [calories=" + calories + ", protein=" + protein + ", fat=" + fat + ", carbohydrate=" | |||
+ carbohydrate + "]"; | |||
} | |||
public int getCalories() { | |||
return calories; | |||
} | |||
public void setCalories(int calories) { | |||
this.calories = calories; | |||
} | |||
public int getProtein() { | |||
return protein; | |||
} | |||
public void setProtein(int protein) { | |||
this.protein = protein; | |||
} | |||
public int getFat() { | |||
return fat; | |||
} | |||
public void setFat(int fat) { | |||
this.fat = fat; | |||
} | |||
public int getCarbohydrate() { | |||
return carbohydrate; | |||
} | |||
public void setCarbohydrate(int carbohydrate) { | |||
this.carbohydrate = carbohydrate; | |||
} | |||
} |
@@ -0,0 +1,54 @@ | |||
package com.javafanatics; | |||
public class DietPlanner { | |||
private int proteinPercentage; | |||
private int fatPercentage; | |||
private int carbohydratePercentage; | |||
public DietPlanner(int proteinPercentage, int fatPercentage, int carbohydratePercentage) { | |||
super(); | |||
if (proteinPercentage + fatPercentage + carbohydratePercentage != 100) { | |||
throw new RuntimeException("protein, fat and carbohydrate percentages must add up to 100!"); | |||
} | |||
this.proteinPercentage = proteinPercentage; | |||
this.fatPercentage = fatPercentage; | |||
this.carbohydratePercentage = carbohydratePercentage; | |||
} | |||
public DietPlan calculateDiet(Coder coder) { | |||
int calories = this.calculateBMR(coder); | |||
int protein = this.calculateProtein(calories); | |||
int fat = this.calculateFat(calories); | |||
int carbohydrate = this.calculateCarbohydrate(calories); | |||
return new DietPlan(calories, protein, fat, carbohydrate); | |||
} | |||
private int calculateProtein(int bmr) { | |||
return (int) Math.round(bmr * proteinPercentage / 400.0); | |||
} | |||
private int calculateFat(int bmr) { | |||
return (int) Math.round(bmr * fatPercentage / 900.0); | |||
} | |||
private int calculateCarbohydrate(int bmr) { | |||
return (int) Math.round(bmr * carbohydratePercentage / 400.0); | |||
} | |||
private int calculateBMR(Coder coder) { | |||
if (coder.getGender() == Gender.MALE) { | |||
return (int) Math.round( | |||
(66.5 + 13.8 * coder.getWeight() | |||
+ 5.0 * coder.getHeight() * 100 | |||
- 6.8 * coder.getAge()) * 1.2 | |||
); | |||
} | |||
return (int) Math.round( | |||
(655.1 + 9.6 * coder.getWeight() | |||
+ 1.9 * coder.getHeight() * 100 | |||
- 4.7 * coder.getAge()) * 1.2 | |||
); | |||
} | |||
} |
@@ -0,0 +1,5 @@ | |||
package com.javafanatics; | |||
public enum Gender { | |||
MALE, FEMALE | |||
} |
@@ -0,0 +1,21 @@ | |||
package com.javafanatics; | |||
import static org.junit.jupiter.api.Assertions.assertTrue; | |||
import org.junit.jupiter.api.Test; | |||
/** | |||
* Unit test for simple App. | |||
*/ | |||
public class AppTest | |||
{ | |||
/** | |||
* Rigorous Test :-) | |||
*/ | |||
@Test | |||
public void shouldAnswerWithTrue() | |||
{ | |||
assertTrue( true ); | |||
} | |||
} |