Skip to content

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
merged 7 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
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;
}
}
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());
}
}
}