Skip to content
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

feat: added file, and test with for loop #234

Merged
merged 3 commits into from
Mar 19, 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,114 @@
package com.codedifferently.lesson7.kevinmason;

public class Food {
// Member variables
private String name;
private double price;
private boolean isVegetarian;
private int calories;
private Category category;

// Custom exception for handling invalid food category
public static class InvalidFoodCategoryException extends Exception {
public InvalidFoodCategoryException(String message) {
super(message);
}
}

// Enum for food categories
public enum Category {
FRUIT,
VEGETABLE,
MEAT,
DAIRY,
GRAIN,
SNACK,
BEVERAGE
}

// Constructor
public Food(String name, double price, boolean isVegetarian, int calories, Category category) {
this.name = name;
this.price = price;
this.isVegetarian = isVegetarian;
this.calories = calories;
this.category = category;
}

// Getter method for calories
public int getCalories() {
return calories;
}

// Function to check if the food is healthy based on calorie count
public boolean isHealthy() {
return calories < 500;
}

// Function to simulate eating the food
public void eat(int portions) {
if (portions <= 0) {
System.out.println("Invalid number of portions.");
return;
}
System.out.println("Eating " + portions + " portions of " + name);
// Adjusting calories based on portions eaten
calories -= portions * 50; // Assuming each portion has 50 calories
if (calories < 0) {
calories = 0; // Calories cannot be negative
}
System.out.println("Remaining calories: " + calories);
}

// Function to simulate purchasing the food
public void purchase(int quantity) {
if (quantity <= 0) {
System.out.println("Invalid quantity.");
anthonydmays marked this conversation as resolved.
Show resolved Hide resolved
return;
}
System.out.println("Purchasing " + quantity + " " + name);
// Adjusting quantity based on purchase
// Dummy implementation, not considering actual stock management
System.out.println("Purchase successful!");
}

// Function to check if the food is expired (dummy implementation)
public boolean isExpired() {
// Dummy implementation, always returns false
return false;
}

// Custom method to handle invalid food categories
public static Category parseCategory(String category) throws InvalidFoodCategoryException {
switch (category.toLowerCase()) {
anthonydmays marked this conversation as resolved.
Show resolved Hide resolved
case "fruit":
return Category.FRUIT;
case "vegetable":
return Category.VEGETABLE;
case "meat":
return Category.MEAT;
case "dairy":
return Category.DAIRY;
case "grain":
return Category.GRAIN;
case "snack":
return Category.SNACK;
case "beverage":
return Category.BEVERAGE;
default:
throw new InvalidFoodCategoryException("Invalid food category: " + category);
}
}

// Function to print details of food for a specified number of times
public void printFoodDetails(int times) {
for (int i = 0; i < times; i++) {
System.out.println("Name: " + name);
System.out.println("Price: " + price);
System.out.println("Is Vegetarian: " + isVegetarian);
System.out.println("Calories: " + calories);
System.out.println("Category: " + category);
System.out.println("--------------------------");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.codedifferently.lesson7.kevinmason;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

public class FoodTest {

@Test
public void testIsHealthy() {
Food healthyFood = new Food("Apple", 1.0, true, 50, Food.Category.FRUIT);
Food unhealthyFood = new Food("Chocolate", 2.0, false, 600, Food.Category.SNACK);

assertTrue(healthyFood.isHealthy());
// Ensure unhealthy food is not considered healthy
assertTrue(!unhealthyFood.isHealthy());
}

@Test
public void testPurchase() {
Food food = new Food("Apple", 1.0, true, 50, Food.Category.FRUIT);
food.purchase(3);
// Assert purchase successful
// You can add additional assertions based on your specific implementation
}

@Test
public void testIsExpired() {
Food food = new Food("Apple", 1.0, true, 50, Food.Category.FRUIT);
// Ensure food is not expired
assertTrue(!food.isExpired());
}

@Test
public void testParseCategory() {
try {
Food.Category fruitCategory = Food.parseCategory("fruit");
assertEquals(Food.Category.FRUIT, fruitCategory);
} catch (Food.InvalidFoodCategoryException e) {
// Fail if exception is thrown for a valid category
e.printStackTrace();
}
}

@Test
public void testPrintFoodDetails() {
Food food = new Food("Apple", 1.0, true, 50, Food.Category.FRUIT);
int times = 3;
food.printFoodDetails(times);
// Ensure no assertion is needed as this method only prints details
}
}