-
Notifications
You must be signed in to change notification settings - Fork 25
Feat: added Lunch Class, an exception Class, and a test Class in lesson_16 folder - James Capparell #517
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
Merged
VicenteVigueras
merged 7 commits into
code-differently:main
from
jjcapparell:feat/lesson16
Nov 5, 2024
Merged
Feat: added Lunch Class, an exception Class, and a test Class in lesson_16 folder - James Capparell #517
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e970116
Feat: added Lunch Method and tests for lesson_16 folder
463923b
chore: added more tests for Lunch class with new InvalidCalorieExcept…
0e5546f
chore: spotlessApply edits
0d55f9c
fix: edit test to not fail with exception
b062ca2
chore: added test to get 81% test coverage
74bbcfa
chore: edited test to reach 100% test coverage
2c5fe9e
chore: updated folder names to be standard
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
...objects_app/src/main/java/com/codedifferently/lesson16/Lunch/InvalidCalorieException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.codedifferently.lesson16.lunch; | ||
|
||
public class InvalidCalorieException extends Exception { | ||
public InvalidCalorieException(String message) { | ||
super(message); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Lunch/Lunch.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.codedifferently.lesson16.lunch; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class Lunch { | ||
|
||
public enum LunchType { | ||
VEGETARIAN, | ||
NON_VEGETARIAN, | ||
VEGAN, | ||
GLUTEN_FREE | ||
} | ||
|
||
// Member variables | ||
private final String mainDish; // String type | ||
private final String sideDish; // String type | ||
private final int calories; // Integer type | ||
private final LunchType lunchType; // Enum type | ||
private final ArrayList<String> drinks; // Collection type (ArrayList) | ||
|
||
// Constructor | ||
public Lunch(String mainDish, String sideDish, int calories, LunchType lunchType) | ||
throws InvalidCalorieException { | ||
if (calories <= 0) { | ||
throw new InvalidCalorieException("Calories cannot be zero or negative."); | ||
} | ||
this.mainDish = mainDish; | ||
this.sideDish = sideDish; | ||
this.calories = calories; | ||
this.lunchType = lunchType; | ||
this.drinks = new ArrayList<>(); | ||
} | ||
|
||
// Member function to add a drink | ||
public void addDrink(String drink) { | ||
drinks.add(drink); | ||
} | ||
|
||
// Member function to get a healthy message based on calories | ||
public String getHealthMessage() { | ||
return (calories < 500) ? "This lunch is healthy!" : "This lunch is high in calories."; | ||
} | ||
|
||
// Member function to display all drinks | ||
public void displayDrinks() { | ||
|
||
System.out.println("Available drinks:"); | ||
for (String drink : drinks) { | ||
System.out.println("- " + drink); | ||
} | ||
} | ||
|
||
// Getters for member variables (optional) | ||
public String getMainDish() { | ||
return mainDish; | ||
} | ||
|
||
public String getSideDish() { | ||
return sideDish; | ||
} | ||
|
||
public int getCalories() { | ||
return calories; | ||
} | ||
|
||
public LunchType getLunchType() { | ||
return lunchType; | ||
} | ||
|
||
public ArrayList<String> getDrinks() { | ||
return drinks; | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
...on_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/lunch/LunchTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package com.codedifferently.lesson16.lunch; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.util.ArrayList; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class LunchTest { | ||
|
||
private Lunch lunch; | ||
private final PrintStream originalOut = System.out; // Store the original System.out | ||
|
||
@BeforeEach | ||
public void setUp() throws InvalidCalorieException { | ||
lunch = new Lunch("Grilled Chicken", "Caesar Salad", 450, Lunch.LunchType.NON_VEGETARIAN); | ||
} | ||
|
||
@Test | ||
public void testAddDrink() { | ||
lunch.addDrink("Iced Tea"); | ||
lunch.addDrink("Lemonade"); | ||
|
||
// Verify that drinks were added correctly | ||
ArrayList<String> expectedDrinks = new ArrayList<>(); | ||
expectedDrinks.add("Iced Tea"); | ||
expectedDrinks.add("Lemonade"); | ||
|
||
// Use reflection or a method to get the drinks if you have one | ||
assertEquals(expectedDrinks.size(), lunch.getDrinks().size()); | ||
assertTrue(lunch.getDrinks().containsAll(expectedDrinks)); | ||
} | ||
|
||
@Test | ||
public void testGetHealthMessageHealthy() throws InvalidCalorieException { | ||
lunch = new Lunch("Salad", "Fruit", 300, Lunch.LunchType.VEGAN); | ||
assertEquals("This lunch is healthy!", lunch.getHealthMessage()); | ||
} | ||
|
||
@Test | ||
public void testGetHealthMessageUnhealthy() throws InvalidCalorieException { | ||
lunch = new Lunch("Cheeseburger", "Fries", 800, Lunch.LunchType.NON_VEGETARIAN); | ||
assertEquals("This lunch is high in calories.", lunch.getHealthMessage()); | ||
} | ||
|
||
@Test | ||
public void testDisplayDrinks() { | ||
// Redirect output to capture printed text | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(outputStream)); | ||
|
||
lunch.addDrink("Water"); | ||
lunch.addDrink("Soda"); | ||
lunch.displayDrinks(); | ||
|
||
// Reset System.out to original | ||
System.setOut(originalOut); | ||
|
||
// Prepare the expected output | ||
String expectedOutput = "Available drinks:\n- Water\n- Soda\n"; | ||
assertEquals(expectedOutput, outputStream.toString()); | ||
} | ||
|
||
// Helper method to access the drinks list for testing | ||
public ArrayList<String> getDrinks() { | ||
return lunch.getDrinks(); | ||
} | ||
|
||
@Test | ||
public void testGetSideDish() throws InvalidCalorieException { | ||
assertEquals("Caesar Salad", lunch.getSideDish()); | ||
} | ||
|
||
@Test | ||
public void testLunchType() throws InvalidCalorieException { | ||
lunch = new Lunch("Tofu Stir Fry", "Brown Rice", 350, Lunch.LunchType.VEGAN); | ||
assertEquals(Lunch.LunchType.VEGAN, lunch.getLunchType()); | ||
} | ||
|
||
@Test | ||
public void testAddMultipleDrinks() { | ||
lunch.addDrink("Soda"); | ||
lunch.addDrink("Juice"); | ||
lunch.addDrink("Coffee"); | ||
|
||
ArrayList<String> expectedDrinks = new ArrayList<>(); | ||
expectedDrinks.add("Soda"); | ||
expectedDrinks.add("Juice"); | ||
expectedDrinks.add("Coffee"); | ||
|
||
assertEquals(expectedDrinks.size(), lunch.getDrinks().size()); | ||
assertTrue(lunch.getDrinks().containsAll(expectedDrinks)); | ||
} | ||
|
||
@Test | ||
public void testHighCalories() throws InvalidCalorieException { | ||
lunch = new Lunch("Double Cheeseburger", "Loaded Fries", 2000, Lunch.LunchType.NON_VEGETARIAN); | ||
assertEquals(2000, lunch.getCalories()); | ||
} | ||
|
||
@Test | ||
public void testEmptyMainDish() throws InvalidCalorieException { | ||
lunch = new Lunch("", "Salad", 300, Lunch.LunchType.VEGETARIAN); | ||
assertEquals("", lunch.getMainDish()); | ||
} | ||
|
||
// Expected to fail with exception message: Calories cannot be zero or negative | ||
@Test | ||
public void testZeroCaloriesThrowsException() { | ||
try { | ||
lunch = new Lunch("Grilled Chicken", "Caesar Salad", 0, Lunch.LunchType.NON_VEGETARIAN); | ||
fail("Expected InvalidCalorieException to be thrown"); | ||
} catch (InvalidCalorieException e) { | ||
assertEquals("Calories cannot be zero or negative.", e.getMessage()); | ||
} | ||
} | ||
|
||
// Expected to fail with exception message: Calories cannot be zero or negative. | ||
@Test | ||
public void testNegativeCaloriesThrowsException() { | ||
try { | ||
lunch = new Lunch("Grilled Chicken", "Caesar Salad", -100, Lunch.LunchType.NON_VEGETARIAN); | ||
fail("Expected InvalidCalorieException to be thrown"); | ||
} catch (InvalidCalorieException e) { | ||
assertEquals("Calories cannot be zero or negative.", e.getMessage()); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.