Skip to content

Commit

Permalink
feat:angielesson16/java/gumballmachine (code-differently#525)
Browse files Browse the repository at this point in the history
* 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
angie-3 authored Nov 15, 2024
1 parent 3607a2b commit 5f02591
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
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());
}
}

0 comments on commit 5f02591

Please sign in to comment.