Skip to content
Open
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
35 changes: 35 additions & 0 deletions solutions/java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>coffeevendingmachine</groupId>
<artifactId>coffeevendingmachine</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Coffee Vending Machine</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.jupiter.version>5.10.2</junit.jupiter.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<useModulePath>false</useModulePath>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,8 @@ public void showIngredients() {
System.out.println("Ingredient Levels:");
ingredientStore.getAllIngredients().forEach((k, v) -> System.out.println(k + ": " + v));
}

public Map<String, Integer> showIngredientsMap() {
return ingredientStore.getAllIngredients();
}
}
116 changes: 116 additions & 0 deletions solutions/java/test/coffeevendingmachine/CoffeeVendingMachineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package coffeevendingmachine;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;

import static org.junit.jupiter.api.Assertions.*;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Map;

public class CoffeeVendingMachineTest {

private CoffeeVendingMachine machine;
private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;

@BeforeEach
void setUp() {
machine = CoffeeVendingMachine.getInstance();
System.setOut(new PrintStream(outputStream));

// Reset and refill ingredients for each test
machine.refillIngredient("Water", 150);
machine.refillIngredient("Coffee", 100);
machine.refillIngredient("Milk", 100);
}

@Test
@DisplayName("Test coffee selection")
void testSelectCoffee() {
CoffeeRecipe espresso = machine.selectCoffee("Espresso");
assertEquals("Espresso", espresso.getName());
assertEquals(2.5, espresso.getPrice());
assertEquals(50, espresso.getRecipe().get("Water"));
assertEquals(20, espresso.getRecipe().get("Coffee"));
}

@Test
@DisplayName("Test invalid coffee selection")
void testInvalidCoffeeSelection() {
Exception exception = assertThrows(RuntimeException.class, () -> {
machine.selectCoffee("MochaCoffee");
});

String expectedMessage = "Invalid coffee recipe: MochaCoffee";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}

@Test
@DisplayName("Test successful coffee dispensing")
void testDispenseCoffee() {
CoffeeRecipe latte = machine.selectCoffee("Latte");
machine.dispenseCoffee(latte, new Payment(4.0));

String output = outputStream.toString();
assertTrue(output.contains("Dispensing: Latte"));
assertTrue(output.contains("Processing Payment"));
assertTrue(output.contains("Please collect your change: $1.0"));
}

@Test
@DisplayName("Test insufficient payment")
void testInsufficientPayment() {
CoffeeRecipe cappuccino = machine.selectCoffee("Cappuccino");

Exception exception = assertThrows(RuntimeException.class, () -> {
machine.dispenseCoffee(cappuccino, new Payment(2.0));
});

String expectedMessage = "Insufficient payment for Cappuccino";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}



@Test
@DisplayName("Test ingredient consumption")
void testIngredientConsumption() {
CoffeeRecipe latte = machine.selectCoffee("Latte");

// Check initial levels
Map<String, Integer> beforeLevels = machine.showIngredientsMap();
int initialWaterLevel = beforeLevels.get("Water");
int initialCoffeeLevel = beforeLevels.get("Coffee");
int initialMilkLevel = beforeLevels.get("Milk");

machine.dispenseCoffee(latte, new Payment(3.0));

// Check levels after dispensing
Map<String, Integer> afterLevels = machine.showIngredientsMap();
assertEquals(initialWaterLevel - 50, afterLevels.get("Water").intValue());
assertEquals(initialCoffeeLevel - 20, afterLevels.get("Coffee").intValue());
assertEquals(initialMilkLevel - 30, afterLevels.get("Milk").intValue());
}

@Test
@DisplayName("Test ingredient refill")
void testIngredientRefill() {
// First check initial level
Map<String, Integer> beforeLevels = machine.showIngredientsMap();
int initialWaterLevel = beforeLevels.get("Water");

// Add more water
machine.refillIngredient("Water", 50);

// Check after refill
Map<String, Integer> afterLevels = machine.showIngredientsMap();
assertEquals(initialWaterLevel + 50, afterLevels.get("Water").intValue());
}
}