Skip to content

Commit e970116

Browse files
author
jjcapparell
committed
Feat: added Lunch Method and tests for lesson_16 folder
1 parent 2e32759 commit e970116

File tree

2 files changed

+127
-0
lines changed
  • lesson_16/objects/objects_app/src

2 files changed

+127
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
this.mainDish = mainDish;
24+
this.sideDish = sideDish;
25+
this.calories = calories;
26+
this.lunchType = lunchType;
27+
this.drinks = new ArrayList<>();
28+
}
29+
30+
// Member function to add a drink
31+
public void addDrink(String drink) {
32+
drinks.add(drink);
33+
}
34+
35+
// Member function to get a healthy message based on calories
36+
public String getHealthMessage() {
37+
return (calories < 500) ? "This lunch is healthy!" : "This lunch is high in calories.";
38+
}
39+
40+
// Member function to display all drinks
41+
public void displayDrinks() {
42+
System.out.println("Available drinks:");
43+
for (String drink : drinks) {
44+
System.out.println("- " + drink);
45+
}
46+
}
47+
48+
// Getters for member variables (optional)
49+
public String getMainDish() {
50+
return mainDish;
51+
}
52+
53+
public String getSideDish() {
54+
return sideDish;
55+
}
56+
57+
public int getCalories() {
58+
return calories;
59+
}
60+
61+
public LunchType getLunchType() {
62+
return lunchType;
63+
}
64+
65+
public ArrayList<String> getDrinks() {
66+
return drinks;
67+
}
68+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.codedifferently.lesson16.LunchTest;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import com.codedifferently.lesson16.Lunch.Lunch;
7+
import java.util.ArrayList;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class LunchTest {
12+
13+
private Lunch lunch;
14+
15+
@BeforeEach
16+
public void setUp() {
17+
lunch = new Lunch("Grilled Chicken", "Caesar Salad", 450, Lunch.LunchType.NON_VEGETARIAN);
18+
}
19+
20+
@Test
21+
public void testAddDrink() {
22+
lunch.addDrink("Iced Tea");
23+
lunch.addDrink("Lemonade");
24+
25+
// Verify that drinks were added correctly
26+
ArrayList<String> expectedDrinks = new ArrayList<>();
27+
expectedDrinks.add("Iced Tea");
28+
expectedDrinks.add("Lemonade");
29+
30+
// Use reflection or a method to get the drinks if you have one
31+
assertEquals(expectedDrinks.size(), lunch.getDrinks().size());
32+
assertTrue(lunch.getDrinks().containsAll(expectedDrinks));
33+
}
34+
35+
@Test
36+
public void testGetHealthMessageHealthy() {
37+
lunch = new Lunch("Salad", "Fruit", 300, Lunch.LunchType.VEGAN);
38+
assertEquals("This lunch is healthy!", lunch.getHealthMessage());
39+
}
40+
41+
@Test
42+
public void testGetHealthMessageUnhealthy() {
43+
lunch = new Lunch("Cheeseburger", "Fries", 800, Lunch.LunchType.NON_VEGETARIAN);
44+
assertEquals("This lunch is high in calories.", lunch.getHealthMessage());
45+
}
46+
47+
@Test
48+
public void testDisplayDrinks() {
49+
lunch.addDrink("Water");
50+
// Capture output or verify by using a mocked System.out or similar approach
51+
// For simplicity, you could check if a drink was added and assume display works
52+
assertTrue(lunch.getDrinks().contains("Water"));
53+
}
54+
55+
// Helper method to access the drinks list for testing
56+
public ArrayList<String> getDrinks() {
57+
return lunch.getDrinks();
58+
}
59+
}

0 commit comments

Comments
 (0)