forked from code-differently/code-differently-24-q4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:angielesson16/java/gumballmachine (code-differently#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
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/gumball/Colors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
56 changes: 56 additions & 0 deletions
56
...bjects/objects_app/src/main/java/com/codedifferently/lesson16/gumball/GumBallMachine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
..._app/src/main/java/com/codedifferently/lesson16/gumball/invalidCoinInsertedException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...ts/objects_app/src/test/java/com/codedifferently/lesson16/gumball/GumballMachineTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |