Skip to content

Commit bba6ec1

Browse files
authored
feat: adds lindaquinoa Lesson 16 HW custom data type for custom Breakfast Food class (#586)
* feat: complete lesson 16 OOP assignment with breakfast food classes * fix: clean up redundant comments * chore: ran spotlessApply to correct build errors
1 parent cb0c0ed commit bba6ec1

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.codedifferently.lesson16.lindaquinoa;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/** A class representing a breakfast food item */
7+
public class BreakfastFood {
8+
9+
private String name;
10+
private double calories;
11+
private int preparationTimeMinutes;
12+
private boolean isHealthy;
13+
private List<String> ingredients;
14+
private BreakfastType type;
15+
16+
/** Constructor */
17+
public BreakfastFood(String name, double calories, int preparationTimeMinutes, BreakfastType type)
18+
throws InvalidBreakfastException {
19+
20+
if (name == null || name.trim().isEmpty()) {
21+
throw new InvalidBreakfastException("Breakfast food name cannot be null or empty");
22+
}
23+
if (calories < 0) {
24+
throw new InvalidBreakfastException("Calories cannot be negative");
25+
}
26+
if (preparationTimeMinutes < 0) {
27+
throw new InvalidBreakfastException("Preparation time cannot be negative");
28+
}
29+
if (type == null) {
30+
throw new InvalidBreakfastException("Breakfast type cannot be null");
31+
}
32+
33+
this.name = name;
34+
this.calories = calories;
35+
this.preparationTimeMinutes = preparationTimeMinutes;
36+
this.type = type;
37+
this.ingredients = new ArrayList<>();
38+
this.isHealthy = determineIfHealthy();
39+
}
40+
41+
public boolean determineIfHealthy() {
42+
if (calories < 300) {
43+
this.isHealthy = true;
44+
return true;
45+
} else {
46+
this.isHealthy = false;
47+
return false;
48+
}
49+
}
50+
51+
public void addIngredient(String ingredient) throws InvalidBreakfastException {
52+
if (ingredient == null || ingredient.trim().isEmpty()) {
53+
throw new InvalidBreakfastException("Ingredient cannot be null or empty");
54+
}
55+
ingredients.add(ingredient.toLowerCase());
56+
}
57+
58+
public String listIngredients() {
59+
if (ingredients.isEmpty()) {
60+
return "No ingredients added yet.";
61+
}
62+
63+
StringBuilder result = new StringBuilder("Ingredients: ");
64+
for (int i = 0; i < ingredients.size(); i++) {
65+
result.append(ingredients.get(i));
66+
if (i < ingredients.size() - 1) {
67+
result.append(", ");
68+
}
69+
}
70+
return result.toString();
71+
}
72+
73+
public String getName() {
74+
return name;
75+
}
76+
77+
public double getCalories() {
78+
return calories;
79+
}
80+
81+
public int getPreparationTimeMinutes() {
82+
return preparationTimeMinutes;
83+
}
84+
85+
public boolean isHealthy() {
86+
return isHealthy;
87+
}
88+
89+
public List<String> getIngredients() {
90+
return ingredients;
91+
}
92+
93+
public BreakfastType getType() {
94+
return type;
95+
}
96+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.codedifferently.lesson16.lindaquinoa;
2+
3+
public enum BreakfastType {
4+
PANCAKES,
5+
WAFFLES,
6+
CEREAL,
7+
OATMEAL,
8+
TOAST,
9+
EGGS,
10+
YOGURT,
11+
SMOOTHIE
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.codedifferently.lesson16.lindaquinoa;
2+
3+
/** Custom exception for invalid breakfast food operations */
4+
public class InvalidBreakfastException extends Exception {
5+
6+
public InvalidBreakfastException(String message) {
7+
super(message);
8+
}
9+
10+
public InvalidBreakfastException(String message, Throwable cause) {
11+
super(message, cause);
12+
}
13+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.codedifferently.lesson16.lindaquinoa;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
/** Test class for BreakfastFood */
9+
public class BreakfastFoodTest {
10+
11+
@Test
12+
void testBreakfastFoodCreationAndProperties() throws InvalidBreakfastException {
13+
BreakfastFood cereal = new BreakfastFood("Cheerios", 110, 2, BreakfastType.CEREAL);
14+
15+
assertThat(cereal.getName()).isEqualTo("Cheerios");
16+
assertThat(cereal.getCalories()).isEqualTo(110);
17+
assertThat(cereal.getPreparationTimeMinutes()).isEqualTo(2);
18+
assertThat(cereal.getType()).isEqualTo(BreakfastType.CEREAL);
19+
assertThat(cereal.getIngredients()).isEmpty();
20+
}
21+
22+
@Test
23+
void testInvalidBreakfastFoodCreation() {
24+
assertThatThrownBy(() -> new BreakfastFood(null, 100, 5, BreakfastType.TOAST))
25+
.isInstanceOf(InvalidBreakfastException.class)
26+
.hasMessage("Breakfast food name cannot be null or empty");
27+
28+
assertThatThrownBy(() -> new BreakfastFood("Toast", -10, 5, BreakfastType.TOAST))
29+
.isInstanceOf(InvalidBreakfastException.class)
30+
.hasMessage("Calories cannot be negative");
31+
}
32+
33+
@Test
34+
void testBreakfastHealthinessDetermination() throws InvalidBreakfastException {
35+
BreakfastFood healthyOatmeal =
36+
new BreakfastFood("Steel Cut Oats", 150, 15, BreakfastType.OATMEAL);
37+
BreakfastFood pancakes = new BreakfastFood("Fluffy Pancakes", 450, 20, BreakfastType.PANCAKES);
38+
39+
assertThat(healthyOatmeal.isHealthy()).isTrue();
40+
assertThat(pancakes.isHealthy()).isFalse();
41+
}
42+
43+
@Test
44+
void testBreakfastIngredientManagement() throws InvalidBreakfastException {
45+
BreakfastFood healthyOatmeal =
46+
new BreakfastFood("Steel Cut Oats", 150, 15, BreakfastType.OATMEAL);
47+
48+
healthyOatmeal.addIngredient("oats");
49+
healthyOatmeal.addIngredient("water");
50+
51+
assertThat(healthyOatmeal.getIngredients()).hasSize(2);
52+
assertThat(healthyOatmeal.getIngredients()).contains("oats", "water");
53+
}
54+
55+
@Test
56+
void testBreakfastIngredientListing() throws InvalidBreakfastException {
57+
BreakfastFood healthyOatmeal =
58+
new BreakfastFood("Steel Cut Oats", 150, 15, BreakfastType.OATMEAL);
59+
60+
healthyOatmeal.addIngredient("oats");
61+
healthyOatmeal.addIngredient("honey");
62+
63+
String result = healthyOatmeal.listIngredients();
64+
assertThat(result).contains("oats");
65+
assertThat(result).contains("honey");
66+
assertThat(result).startsWith("Ingredients:");
67+
}
68+
}

0 commit comments

Comments
 (0)