diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/gumball/Colors.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/gumball/Colors.java new file mode 100644 index 000000000..67a04f6a9 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/gumball/Colors.java @@ -0,0 +1,7 @@ +package com.codedifferently.lesson16.gumball; + +public enum Colors { + GREEN, + RED, + PINK +} diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/gumball/GumBallMachine.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/gumball/GumBallMachine.java new file mode 100644 index 000000000..34a378ee1 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/gumball/GumBallMachine.java @@ -0,0 +1,56 @@ +package com.codedifferently.lesson16.gumball; + +import java.util.HashMap; +import java.util.Random; + +public class GumBallMachine { + private Colors colors; // Current color of the gumball + private int gumBallCount; // Count of gumballs in the machine + private boolean isBroken; // Status of the machine + private HashMap gumball; // Collection of gumballs + private Random rand; // Random object for color selection + + // Constructor + public GumBallMachine(int gumBallCount, boolean isBroken) { + this.colors = Colors.GREEN; // Default color + this.gumBallCount = gumBallCount; + this.isBroken = isBroken; + this.gumball = new HashMap<>(); + this.rand = new Random(); + } + + public Colors getCurrentColor() { + return this.colors; + } + + public void setRandomColors() { + Colors[] colorArray = Colors.values(); + int randIndex = rand.nextInt(colorArray.length); + this.colors = colorArray[randIndex]; // Set a random color + } + + public int getGumBallCount() { + return this.gumBallCount; + } + + public void dispenseGumBall(double quarter) throws invalidCoinInsertedException { + if (quarter != 0.25) { + throw new invalidCoinInsertedException("You need a quarter!"); + } + if (gumBallCount > 0 && !isBroken) { + gumBallCount--; // Dispense a gumball + } else if (gumBallCount == 0) { + throw new invalidCoinInsertedException("No more gumballs!"); + } else { + throw new invalidCoinInsertedException("Machine is broken!"); + } + } + + public boolean isBroken() { + return isBroken; + } + + public void breakMachine() { + isBroken = true; + } +} diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/gumball/invalidCoinInsertedException.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/gumball/invalidCoinInsertedException.java new file mode 100644 index 000000000..c26d67b4a --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/gumball/invalidCoinInsertedException.java @@ -0,0 +1,7 @@ +package com.codedifferently.lesson16.gumball; + +public class invalidCoinInsertedException extends Exception { + public invalidCoinInsertedException(String message) { + super(message); + } +} diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/gumball/GumballMachineTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/gumball/GumballMachineTest.java new file mode 100644 index 000000000..c1dab4432 --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/gumball/GumballMachineTest.java @@ -0,0 +1,70 @@ +package com.codedifferently.lesson16.gumball; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class GumballMachineTest { + GumBallMachine testGumBallMachine; + + @BeforeEach + void setUp() { + testGumBallMachine = new GumBallMachine(10, false); // Initialize with 10 gumballs + } + + @Test + void testGumBallCount_afterDispense() throws invalidCoinInsertedException { + int initialCount = testGumBallMachine.getGumBallCount(); + testGumBallMachine.dispenseGumBall(0.25); // Dispense a gumball + assertEquals(initialCount - 1, testGumBallMachine.getGumBallCount()); + } + + @Test + void testSetRandomColor() { + Colors previousColor = testGumBallMachine.getCurrentColor(); + Colors newColor = previousColor; + int attempts = 0; + int maxAttempts = 10; // Set a limit for attempts + + // Try to change the color until it is different or max attempts reached + while (newColor.equals(previousColor) && attempts < maxAttempts) { + testGumBallMachine.setRandomColors(); // Set a new random color + newColor = testGumBallMachine.getCurrentColor(); + attempts++; + } + + assertNotEquals( + previousColor, newColor, "The color should have changed after multiple attempts"); + } + + @Test + void testGetGumBall_whenMachineIsEmpty() throws invalidCoinInsertedException { + for (int i = 0; i < 10; i++) { + testGumBallMachine.dispenseGumBall(0.25); // Dispense all gumballs + } + assertThrows( + invalidCoinInsertedException.class, + () -> { + testGumBallMachine.dispenseGumBall(0.25); // Try to dispense from an empty machine + }); + } + + @Test + void testGetGumBall_InvalidCoin() { + assertThrows( + invalidCoinInsertedException.class, + () -> { + testGumBallMachine.dispenseGumBall(0.01); // Try to insert an invalid coin + }); + } + + @Test + void testGumBallMachineIsBroken() { + testGumBallMachine.breakMachine(); + assertTrue(testGumBallMachine.isBroken()); + } +}