Skip to content

Commit 33c6601

Browse files
committed
fix:debug: made to java test files/angielesson16"
1 parent 72f8958 commit 33c6601

File tree

4 files changed

+111
-74
lines changed

4 files changed

+111
-74
lines changed
Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,57 @@
11
package com.codedifferently.lesson16;
22

3-
import java.util.ArrayList;
4-
3+
import com.codedifferently.lesson16.gumball.Colors;
4+
import java.util.HashMap;
5+
import java.util.Random;
56

67
public class GumBallMachine {
7-
8-
enum Colors{
9-
GREEN,
10-
RED,
11-
PINK
12-
}
13-
14-
enum coinTypes{
15-
0.05,
16-
0.10,
17-
0.25
18-
}
19-
20-
private Colors colors;
21-
private int gumBallCount;
22-
private boolean isBroken;
23-
private ArrayList coinType;
24-
private int invalidCoin;
25-
26-
public GumBallMachine(int GumBallCount, boolean isBroken) {
27-
this.colors = colors.GREEN;
8+
private Colors colors; // Current color of the gumball
9+
private int gumBallCount; // Count of gumballs in the machine
10+
private boolean isBroken; // Status of the machine
11+
private HashMap<Colors, Integer> gumball; // Collection of gumballs
12+
private Random rand; // Random object for color selection
13+
14+
// Constructor
15+
public GumBallMachine(int gumBallCount, boolean isBroken) {
16+
this.colors = Colors.GREEN; // Default color
2817
this.gumBallCount = gumBallCount;
2918
this.isBroken = isBroken;
30-
this.coinType = coinType;
31-
this.invalidCoin = invalidCoin;
19+
this.gumball = new HashMap<>();
20+
this.rand = new Random();
3221
}
3322

34-
public gumBallMachineCount(int gumBallCount) {
35-
if(previousGumballMachine < new GumballMachineCount);{
23+
public Colors getCurrentColor() {
24+
return this.colors;
3625
}
3726

38-
get.GumballCount = new gumballMachineCount;{
27+
public void setRandomColors() {
28+
Colors[] colorArray = Colors.values();
29+
int randIndex = rand.nextInt(colorArray.length);
30+
this.colors = colorArray[randIndex]; // Set a random color
3931
}
4032

41-
public int getGumBallCount(){
42-
return new gumBallMachineCount;
33+
public int getGumBallCount() {
34+
return this.gumBallCount;
4335
}
4436

45-
public Colors getcolors(){
46-
return colors
37+
public void getGumBall(double quarter) throws invalidCoinInsertedException {
38+
if (quarter != 0.25) {
39+
throw new invalidCoinInsertedException("You need a quarter!");
40+
}
41+
if (gumBallCount > 0 && !isBroken) {
42+
gumBallCount--; // Dispense a gumball
43+
} else if (gumBallCount == 0) {
44+
throw new invalidCoinInsertedException("No more gumballs!");
45+
} else {
46+
throw new invalidCoinInsertedException("Machine is broken!");
47+
}
4748
}
4849

50+
public boolean isBroken() {
51+
return isBroken;
52+
}
53+
54+
public void breakMachine() {
55+
isBroken = true;
56+
}
4957
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.codedifferently.lesson16;
2+
23
public class invalidCoinInsertedException extends Exception {
3-
public invalidCoinInsertedException(String message ){
4-
super(message);
5-
}
4+
public invalidCoinInsertedException(String message) {
5+
super(message);
6+
}
67
}

lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/GumballMachineTest.java

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,78 @@
11
package com.codedifferently.lesson16;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertFalse;
54
import static org.junit.jupiter.api.Assertions.assertNotEquals;
65
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import com.codedifferently.lesson16.gumball.Colors;
9+
import org.junit.jupiter.api.BeforeEach;
710
import org.junit.jupiter.api.Test;
811

912
public class GumballMachineTest {
10-
GumBallMachine testGumBallMachine(20, );
13+
GumBallMachine testGumBallMachine;
14+
15+
@BeforeEach
16+
void setUp() {
17+
testGumBallMachine = new GumBallMachine(10, false); // Initialize with 10 gumballs
18+
}
1119

1220
@Test
13-
void testGumballMachineCount() {
14-
//arrange
15-
testGumBallMachine = new gumballMachine();
16-
//act
17-
testGumballMachine.removeGumball();
18-
//assert
19-
assertNotEquals(GumballmachineCount.getGumballCount(),testGumballMachine);
20-
}
21+
void testGumBallCount_afterDispense() throws invalidCoinInsertedException {
22+
int initialCount = testGumBallMachine.getGumBallCount();
23+
testGumBallMachine.getGumBall(0.25); // Dispense a gumball
24+
assertEquals(initialCount - 1, testGumBallMachine.getGumBallCount());
25+
}
2126

22-
@Test
23-
void testGetGumBall() {
24-
// Arrange
25-
var quater = 0.25;
26-
var previousGumBallCount = gumBallMachine.getGumBallCount();
27-
// Act
28-
gumballmachine.getGumBall(quater);
27+
@Test
28+
void testSetRandomColor() {
29+
Colors previousColor = testGumBallMachine.getCurrentColor();
30+
Colors newColor = previousColor;
31+
int attempts = 0;
32+
int maxAttempts = 10; // Set a limit for attempts
2933

30-
// Assert
31-
assertNotEquals(gumballmachine.getGumBallCount(),previousGumBallCount);
34+
// Try to change the color until it is different or max attempts reached
35+
while (newColor.equals(previousColor) && attempts < maxAttempts) {
36+
testGumBallMachine.setRandomColors(); // Set a new random color
37+
newColor = testGumBallMachine.getCurrentColor();
38+
attempts++;
3239
}
33-
@Test
34-
testGumballMachineIsEmpty_whenEmpty() {
35-
// Arrange
36-
3740

38-
// Act
39-
testGumballMachine.getGumBall(threedollars);
41+
assertNotEquals(
42+
previousColor, newColor, "The color should have changed after multiple attempts");
43+
}
4044

41-
// Assert
42-
assertEquals("No more gum! Refill.", testGumballMachine.IsEmpty());
45+
@Test
46+
void testGetGumBall_whenValidQuarter() throws invalidCoinInsertedException {
47+
int previousCount = testGumBallMachine.getGumBallCount();
48+
testGumBallMachine.getGumBall(0.25);
49+
assertEquals(previousCount - 1, testGumBallMachine.getGumBallCount());
50+
}
51+
52+
@Test
53+
void testGetGumBall_whenMachineIsEmpty() throws invalidCoinInsertedException {
54+
for (int i = 0; i < 10; i++) {
55+
testGumBallMachine.getGumBall(0.25); // Dispense all gumballs
4356
}
44-
@Test
45-
testGetGumBall_InvalidCoinPenny() {
46-
47-
assertThrows (invalidCoinInsertedException.class,()->{
48-
testGumBallMachine.getGumBall(0.01);
57+
assertThrows(
58+
invalidCoinInsertedException.class,
59+
() -> {
60+
testGumBallMachine.getGumBall(0.25); // Try to dispense from an empty machine
4961
});
50-
}
51-
@Test
52-
testGumBallMachineIsBroken(){
53-
testGumBallMachine.IsBroken();
54-
assertFalse(testGumBallMachine.IsBroken);
55-
}
62+
}
63+
64+
@Test
65+
void testGetGumBall_InvalidCoin() {
66+
assertThrows(
67+
invalidCoinInsertedException.class,
68+
() -> {
69+
testGumBallMachine.getGumBall(0.01); // Try to insert an invalid coin
70+
});
71+
}
72+
73+
@Test
74+
void testGumBallMachineIsBroken() {
75+
testGumBallMachine.breakMachine();
76+
assertTrue(testGumBallMachine.isBroken());
77+
}
5678
}
57-

0 commit comments

Comments
 (0)