Skip to content

Commit 5f02591

Browse files
authored
feat:angielesson16/java/gumballmachine (#525)
* Stashing commit * fix:debug: made to java test files/angielesson16" * fix: files placed in the correct directory/lesson16/angieC * fix:changes on getGumball to dispenseGumBall * fix:changes on getGumball to dispenseGumBall * fix:changes on getGumball to dispenseGumBall
1 parent 3607a2b commit 5f02591

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.gumball;
2+
3+
public enum Colors {
4+
GREEN,
5+
RED,
6+
PINK
7+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.codedifferently.lesson16.gumball;
2+
3+
import java.util.HashMap;
4+
import java.util.Random;
5+
6+
public class GumBallMachine {
7+
private Colors colors; // Current color of the gumball
8+
private int gumBallCount; // Count of gumballs in the machine
9+
private boolean isBroken; // Status of the machine
10+
private HashMap<Colors, Integer> gumball; // Collection of gumballs
11+
private Random rand; // Random object for color selection
12+
13+
// Constructor
14+
public GumBallMachine(int gumBallCount, boolean isBroken) {
15+
this.colors = Colors.GREEN; // Default color
16+
this.gumBallCount = gumBallCount;
17+
this.isBroken = isBroken;
18+
this.gumball = new HashMap<>();
19+
this.rand = new Random();
20+
}
21+
22+
public Colors getCurrentColor() {
23+
return this.colors;
24+
}
25+
26+
public void setRandomColors() {
27+
Colors[] colorArray = Colors.values();
28+
int randIndex = rand.nextInt(colorArray.length);
29+
this.colors = colorArray[randIndex]; // Set a random color
30+
}
31+
32+
public int getGumBallCount() {
33+
return this.gumBallCount;
34+
}
35+
36+
public void dispenseGumBall(double quarter) throws invalidCoinInsertedException {
37+
if (quarter != 0.25) {
38+
throw new invalidCoinInsertedException("You need a quarter!");
39+
}
40+
if (gumBallCount > 0 && !isBroken) {
41+
gumBallCount--; // Dispense a gumball
42+
} else if (gumBallCount == 0) {
43+
throw new invalidCoinInsertedException("No more gumballs!");
44+
} else {
45+
throw new invalidCoinInsertedException("Machine is broken!");
46+
}
47+
}
48+
49+
public boolean isBroken() {
50+
return isBroken;
51+
}
52+
53+
public void breakMachine() {
54+
isBroken = true;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.gumball;
2+
3+
public class invalidCoinInsertedException extends Exception {
4+
public invalidCoinInsertedException(String message) {
5+
super(message);
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.codedifferently.lesson16.gumball;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class GumballMachineTest {
12+
GumBallMachine testGumBallMachine;
13+
14+
@BeforeEach
15+
void setUp() {
16+
testGumBallMachine = new GumBallMachine(10, false); // Initialize with 10 gumballs
17+
}
18+
19+
@Test
20+
void testGumBallCount_afterDispense() throws invalidCoinInsertedException {
21+
int initialCount = testGumBallMachine.getGumBallCount();
22+
testGumBallMachine.dispenseGumBall(0.25); // Dispense a gumball
23+
assertEquals(initialCount - 1, testGumBallMachine.getGumBallCount());
24+
}
25+
26+
@Test
27+
void testSetRandomColor() {
28+
Colors previousColor = testGumBallMachine.getCurrentColor();
29+
Colors newColor = previousColor;
30+
int attempts = 0;
31+
int maxAttempts = 10; // Set a limit for attempts
32+
33+
// Try to change the color until it is different or max attempts reached
34+
while (newColor.equals(previousColor) && attempts < maxAttempts) {
35+
testGumBallMachine.setRandomColors(); // Set a new random color
36+
newColor = testGumBallMachine.getCurrentColor();
37+
attempts++;
38+
}
39+
40+
assertNotEquals(
41+
previousColor, newColor, "The color should have changed after multiple attempts");
42+
}
43+
44+
@Test
45+
void testGetGumBall_whenMachineIsEmpty() throws invalidCoinInsertedException {
46+
for (int i = 0; i < 10; i++) {
47+
testGumBallMachine.dispenseGumBall(0.25); // Dispense all gumballs
48+
}
49+
assertThrows(
50+
invalidCoinInsertedException.class,
51+
() -> {
52+
testGumBallMachine.dispenseGumBall(0.25); // Try to dispense from an empty machine
53+
});
54+
}
55+
56+
@Test
57+
void testGetGumBall_InvalidCoin() {
58+
assertThrows(
59+
invalidCoinInsertedException.class,
60+
() -> {
61+
testGumBallMachine.dispenseGumBall(0.01); // Try to insert an invalid coin
62+
});
63+
}
64+
65+
@Test
66+
void testGumBallMachineIsBroken() {
67+
testGumBallMachine.breakMachine();
68+
assertTrue(testGumBallMachine.isBroken());
69+
}
70+
}

0 commit comments

Comments
 (0)