-
Notifications
You must be signed in to change notification settings - Fork 25
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
VicenteVigueras
merged 14 commits into
code-differently:main
from
angie-3:angielesson16
Nov 15, 2024
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
72f8958
Stashing commit
angie-3 2e8fd39
Merge branch 'code-differently:main' into angielesson16
angie-3 33c6601
fix:debug: made to java test files/angielesson16"
angie-3 ace81d5
Merge branch 'angielesson16' of https://github.com/angie-3/code-diffe…
angie-3 c16013a
Merge branch 'code-differently:main' into angielesson16
angie-3 83a8793
Merge branch 'code-differently:main' into angielesson16
angie-3 edfcb3b
Merge branch 'code-differently:main' into angielesson16
angie-3 6978a66
fix: files placed in the correct directory/lesson16/angieC
angie-3 7d50a32
Merge branch 'main' of https://github.com/angie-3/code-differently-24…
angie-3 ae6c7ef
Merge branch 'angielesson16' of https://github.com/angie-3/code-diffe…
angie-3 1771c80
fix:changes on getGumball to dispenseGumBall
angie-3 c81b4f2
fix:changes on getGumball to dispenseGumBall
angie-3 25f8a25
Merge branch 'main' of https://github.com/angie-3/code-differently-24…
angie-3 d458fe2
fix:changes on getGumball to dispenseGumBall
angie-3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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 getGumBall(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 hidden or 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); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...ts/objects_app/src/test/java/com/codedifferently/lesson16/gumball/GumballMachineTest.java
This file contains hidden or 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,77 @@ | ||
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.getGumBall(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_whenValidQuarter() throws invalidCoinInsertedException { | ||
angie-3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int previousCount = testGumBallMachine.getGumBallCount(); | ||
testGumBallMachine.getGumBall(0.25); | ||
assertEquals(previousCount - 1, testGumBallMachine.getGumBallCount()); | ||
} | ||
|
||
@Test | ||
void testGetGumBall_whenMachineIsEmpty() throws invalidCoinInsertedException { | ||
for (int i = 0; i < 10; i++) { | ||
testGumBallMachine.getGumBall(0.25); // Dispense all gumballs | ||
} | ||
assertThrows( | ||
invalidCoinInsertedException.class, | ||
() -> { | ||
testGumBallMachine.getGumBall(0.25); // Try to dispense from an empty machine | ||
}); | ||
} | ||
|
||
@Test | ||
void testGetGumBall_InvalidCoin() { | ||
assertThrows( | ||
invalidCoinInsertedException.class, | ||
() -> { | ||
testGumBallMachine.getGumBall(0.01); // Try to insert an invalid coin | ||
}); | ||
} | ||
|
||
@Test | ||
void testGumBallMachineIsBroken() { | ||
testGumBallMachine.breakMachine(); | ||
assertTrue(testGumBallMachine.isBroken()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.