Skip to content

feat:angielesson16/java/gumballmachine #525

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 14 commits into from
Nov 15, 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.gumball;

public enum Colors {
GREEN,
RED,
PINK
}
Original file line number Diff line number Diff line change
@@ -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<Colors, Integer> 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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.codedifferently.lesson16.gumball;

public class invalidCoinInsertedException extends Exception {
public invalidCoinInsertedException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}