Skip to content

Commit 21f47d5

Browse files
jjcapparelljjcapparell
andauthored
Feat: Add Lunch Class, an exception Class, and a test Class in lesson_16 folder - James Capparell (#517)
* Feat: add Lunch Method and tests for lesson_16 folder * chore: add more tests for Lunch class with new InvalidCalorieException file * chore: spotlessApply edits * fix: edit test to not fail with exception * chore: add test to get 81% test coverage * chore: edit test to reach 100% test coverage * chore: update folder names to be standard --------- Co-authored-by: jjcapparell <“[email protected]>
1 parent 7a1827e commit 21f47d5

File tree

3 files changed

+212
-0
lines changed

3 files changed

+212
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.lunch;
2+
3+
public class InvalidCalorieException extends Exception {
4+
public InvalidCalorieException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.codedifferently.lesson16.lunch;
2+
3+
import java.util.ArrayList;
4+
5+
public class Lunch {
6+
7+
public enum LunchType {
8+
VEGETARIAN,
9+
NON_VEGETARIAN,
10+
VEGAN,
11+
GLUTEN_FREE
12+
}
13+
14+
// Member variables
15+
private final String mainDish; // String type
16+
private final String sideDish; // String type
17+
private final int calories; // Integer type
18+
private final LunchType lunchType; // Enum type
19+
private final ArrayList<String> drinks; // Collection type (ArrayList)
20+
21+
// Constructor
22+
public Lunch(String mainDish, String sideDish, int calories, LunchType lunchType)
23+
throws InvalidCalorieException {
24+
if (calories <= 0) {
25+
throw new InvalidCalorieException("Calories cannot be zero or negative.");
26+
}
27+
this.mainDish = mainDish;
28+
this.sideDish = sideDish;
29+
this.calories = calories;
30+
this.lunchType = lunchType;
31+
this.drinks = new ArrayList<>();
32+
}
33+
34+
// Member function to add a drink
35+
public void addDrink(String drink) {
36+
drinks.add(drink);
37+
}
38+
39+
// Member function to get a healthy message based on calories
40+
public String getHealthMessage() {
41+
return (calories < 500) ? "This lunch is healthy!" : "This lunch is high in calories.";
42+
}
43+
44+
// Member function to display all drinks
45+
public void displayDrinks() {
46+
47+
System.out.println("Available drinks:");
48+
for (String drink : drinks) {
49+
System.out.println("- " + drink);
50+
}
51+
}
52+
53+
// Getters for member variables (optional)
54+
public String getMainDish() {
55+
return mainDish;
56+
}
57+
58+
public String getSideDish() {
59+
return sideDish;
60+
}
61+
62+
public int getCalories() {
63+
return calories;
64+
}
65+
66+
public LunchType getLunchType() {
67+
return lunchType;
68+
}
69+
70+
public ArrayList<String> getDrinks() {
71+
return drinks;
72+
}
73+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.codedifferently.lesson16.lunch;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
import static org.junit.jupiter.api.Assertions.fail;
6+
7+
import java.io.ByteArrayOutputStream;
8+
import java.io.PrintStream;
9+
import java.util.ArrayList;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
13+
public class LunchTest {
14+
15+
private Lunch lunch;
16+
private final PrintStream originalOut = System.out; // Store the original System.out
17+
18+
@BeforeEach
19+
public void setUp() throws InvalidCalorieException {
20+
lunch = new Lunch("Grilled Chicken", "Caesar Salad", 450, Lunch.LunchType.NON_VEGETARIAN);
21+
}
22+
23+
@Test
24+
public void testAddDrink() {
25+
lunch.addDrink("Iced Tea");
26+
lunch.addDrink("Lemonade");
27+
28+
// Verify that drinks were added correctly
29+
ArrayList<String> expectedDrinks = new ArrayList<>();
30+
expectedDrinks.add("Iced Tea");
31+
expectedDrinks.add("Lemonade");
32+
33+
// Use reflection or a method to get the drinks if you have one
34+
assertEquals(expectedDrinks.size(), lunch.getDrinks().size());
35+
assertTrue(lunch.getDrinks().containsAll(expectedDrinks));
36+
}
37+
38+
@Test
39+
public void testGetHealthMessageHealthy() throws InvalidCalorieException {
40+
lunch = new Lunch("Salad", "Fruit", 300, Lunch.LunchType.VEGAN);
41+
assertEquals("This lunch is healthy!", lunch.getHealthMessage());
42+
}
43+
44+
@Test
45+
public void testGetHealthMessageUnhealthy() throws InvalidCalorieException {
46+
lunch = new Lunch("Cheeseburger", "Fries", 800, Lunch.LunchType.NON_VEGETARIAN);
47+
assertEquals("This lunch is high in calories.", lunch.getHealthMessage());
48+
}
49+
50+
@Test
51+
public void testDisplayDrinks() {
52+
// Redirect output to capture printed text
53+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
54+
System.setOut(new PrintStream(outputStream));
55+
56+
lunch.addDrink("Water");
57+
lunch.addDrink("Soda");
58+
lunch.displayDrinks();
59+
60+
// Reset System.out to original
61+
System.setOut(originalOut);
62+
63+
// Prepare the expected output
64+
String expectedOutput = "Available drinks:\n- Water\n- Soda\n";
65+
assertEquals(expectedOutput, outputStream.toString());
66+
}
67+
68+
// Helper method to access the drinks list for testing
69+
public ArrayList<String> getDrinks() {
70+
return lunch.getDrinks();
71+
}
72+
73+
@Test
74+
public void testGetSideDish() throws InvalidCalorieException {
75+
assertEquals("Caesar Salad", lunch.getSideDish());
76+
}
77+
78+
@Test
79+
public void testLunchType() throws InvalidCalorieException {
80+
lunch = new Lunch("Tofu Stir Fry", "Brown Rice", 350, Lunch.LunchType.VEGAN);
81+
assertEquals(Lunch.LunchType.VEGAN, lunch.getLunchType());
82+
}
83+
84+
@Test
85+
public void testAddMultipleDrinks() {
86+
lunch.addDrink("Soda");
87+
lunch.addDrink("Juice");
88+
lunch.addDrink("Coffee");
89+
90+
ArrayList<String> expectedDrinks = new ArrayList<>();
91+
expectedDrinks.add("Soda");
92+
expectedDrinks.add("Juice");
93+
expectedDrinks.add("Coffee");
94+
95+
assertEquals(expectedDrinks.size(), lunch.getDrinks().size());
96+
assertTrue(lunch.getDrinks().containsAll(expectedDrinks));
97+
}
98+
99+
@Test
100+
public void testHighCalories() throws InvalidCalorieException {
101+
lunch = new Lunch("Double Cheeseburger", "Loaded Fries", 2000, Lunch.LunchType.NON_VEGETARIAN);
102+
assertEquals(2000, lunch.getCalories());
103+
}
104+
105+
@Test
106+
public void testEmptyMainDish() throws InvalidCalorieException {
107+
lunch = new Lunch("", "Salad", 300, Lunch.LunchType.VEGETARIAN);
108+
assertEquals("", lunch.getMainDish());
109+
}
110+
111+
// Expected to fail with exception message: Calories cannot be zero or negative
112+
@Test
113+
public void testZeroCaloriesThrowsException() {
114+
try {
115+
lunch = new Lunch("Grilled Chicken", "Caesar Salad", 0, Lunch.LunchType.NON_VEGETARIAN);
116+
fail("Expected InvalidCalorieException to be thrown");
117+
} catch (InvalidCalorieException e) {
118+
assertEquals("Calories cannot be zero or negative.", e.getMessage());
119+
}
120+
}
121+
122+
// Expected to fail with exception message: Calories cannot be zero or negative.
123+
@Test
124+
public void testNegativeCaloriesThrowsException() {
125+
try {
126+
lunch = new Lunch("Grilled Chicken", "Caesar Salad", -100, Lunch.LunchType.NON_VEGETARIAN);
127+
fail("Expected InvalidCalorieException to be thrown");
128+
} catch (InvalidCalorieException e) {
129+
assertEquals("Calories cannot be zero or negative.", e.getMessage());
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)