Skip to content

Commit 448b52e

Browse files
authored
Feat: Adds Shawn Dunsmore Jr lesson 16 SlotMachine.java and SlotMachi… (#529)
* Feat: Adds Shawn Dunsmore Jr lesson 16 SlotMachine.java and SlotMachineTest.java * Added Spotless * Feat: Adds Shawn Dunsmore Jr lesson 16 91 Percent Coverage * Feat: Adds Shawn Dunsmore Jr Added Spotless * Feat: Adds Shawn Dunsmore Jr lesson 16 Added For Loop and Test * Feat: Adds Shawn Dunsmore Jr Added Spotless * Adds Fixed Changed * refactor: proper casing * refactor: tab * Removes File Live Server * feat: removed files * refactor: lowercase * refactor: spacing * Adds fixed changes * Update build.gradle.kts * Update build.gradle.kts
1 parent 30c9c25 commit 448b52e

File tree

4 files changed

+295
-0
lines changed

4 files changed

+295
-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.shawndunsmore;
2+
3+
public enum BuyType {
4+
BONUS_BUY,
5+
DOUBLE_CHANCE,
6+
NORMAL_BUY,
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.codedifferently.lesson16.shawndunsmore;
2+
3+
public class InvalidPayAmountException extends Exception {
4+
5+
public InvalidPayAmountException(String message) {
6+
super(message);
7+
}
8+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.codedifferently.lesson16.shawndunsmore;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
6+
public class SlotMachine {
7+
private int numOfSlots;
8+
private int payAmount;
9+
private String name;
10+
private BuyType buyType;
11+
private ArrayList<String> iconList;
12+
private int moneyNeeded;
13+
14+
public SlotMachine(
15+
int numOfSlots,
16+
int payAmount,
17+
String name,
18+
BuyType buyType,
19+
ArrayList<String> iconList,
20+
int moneyNeeded) {
21+
this.numOfSlots = numOfSlots;
22+
this.payAmount = payAmount;
23+
this.name = name;
24+
this.buyType = buyType;
25+
this.iconList = iconList;
26+
this.moneyNeeded = moneyNeeded;
27+
}
28+
29+
public int getNumOfSlots() {
30+
return numOfSlots;
31+
}
32+
33+
public int getPayAmount() {
34+
return payAmount;
35+
}
36+
37+
public String getName() {
38+
return name;
39+
}
40+
41+
public BuyType getBuyType() {
42+
return buyType;
43+
}
44+
45+
public ArrayList<String> getIconList() {
46+
return iconList;
47+
}
48+
49+
public int payOut() {
50+
if (buyType.equals(BuyType.DOUBLE_CHANCE)) {
51+
return payAmount * 2;
52+
}
53+
54+
if (buyType.equals(BuyType.BONUS_BUY)) {
55+
return payAmount * 3;
56+
}
57+
return payAmount;
58+
}
59+
60+
public ArrayList<String> spin(int money) throws InvalidPayAmountException {
61+
if (money < moneyNeeded) {
62+
throw new InvalidPayAmountException("Amount inavalid");
63+
}
64+
Collections.shuffle(iconList);
65+
return iconList;
66+
}
67+
68+
public ArrayList<String> spin(int money, int numOfSpins) throws InvalidPayAmountException {
69+
if (money < moneyNeeded) {
70+
throw new InvalidPayAmountException("Amount inavalid");
71+
}
72+
for (int i = 0; i < numOfSpins; i++) {
73+
Collections.shuffle(iconList);
74+
}
75+
76+
return iconList;
77+
}
78+
79+
public int getMoneyNeeded() {
80+
return moneyNeeded;
81+
}
82+
83+
public void setNumOfSlots(int numOfSlots) {
84+
this.numOfSlots = numOfSlots;
85+
}
86+
87+
public void setPayAmount(int payAmount) {
88+
this.payAmount = payAmount;
89+
}
90+
91+
public void setName(String name) {
92+
this.name = name;
93+
}
94+
95+
public void setBuyType(BuyType buyType) {
96+
this.buyType = buyType;
97+
}
98+
99+
public void setIconList(ArrayList<String> iconList) {
100+
this.iconList = iconList;
101+
}
102+
103+
public void setMoneyNeeded(int moneyNeeded) {
104+
this.moneyNeeded = moneyNeeded;
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package com.codedifferently.lesson16.shawndunsmore;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class SlotMachineTest {
11+
@Test
12+
public void testMoney_Exists() {
13+
SlotMachine slot = new SlotMachine(0, 0, null, null, null, 10);
14+
assertEquals(10, slot.getMoneyNeeded());
15+
}
16+
17+
@Test
18+
public void testNumOfSlots_Exists() {
19+
SlotMachine slot = new SlotMachine(1, 0, null, null, null, 10);
20+
assertEquals(1, slot.getNumOfSlots());
21+
}
22+
23+
@Test
24+
public void testNameOfSlot_Exists() {
25+
SlotMachine slot = new SlotMachine(1, 0, "Goldie", null, null, 10);
26+
assertEquals("Goldie", slot.getName());
27+
}
28+
29+
@Test
30+
public void testPayAmount_Exists() {
31+
SlotMachine slot = new SlotMachine(1, 10, "Goldie", null, null, 10);
32+
assertEquals(10, slot.getPayAmount());
33+
}
34+
35+
@Test
36+
public void testBuyType_Exists() {
37+
SlotMachine slot = new SlotMachine(1, 10, "Goldie", null, null, 10);
38+
assertEquals(null, slot.getBuyType());
39+
}
40+
41+
@Test
42+
public void testUseDoubleSlots() {
43+
SlotMachine slottwo = new SlotMachine(1, 10, "Goldie", null, null, 10);
44+
SlotMachine slotone = new SlotMachine(1, 10, "Diamond", null, null, 10);
45+
assertEquals(1, slotone.getNumOfSlots());
46+
assertEquals(1, slottwo.getNumOfSlots());
47+
}
48+
49+
@Test
50+
public void testSlotSpin() {
51+
SlotMachine slot = new SlotMachine(1, 10, "Goldie", null, null, 10);
52+
assertEquals(null, slot.getIconList());
53+
}
54+
55+
@Test
56+
public void testPayOut() {
57+
58+
ArrayList<String> icons = new ArrayList<>();
59+
SlotMachine slotMachine1 =
60+
new SlotMachine(1, 10, "Test Slot 1", BuyType.DOUBLE_CHANCE, icons, 10);
61+
assertEquals(20, slotMachine1.payOut(), "Expected payout for DOUBLE_CHANCE to be 20");
62+
63+
SlotMachine slotMachine2 = new SlotMachine(1, 10, "Test Slot 2", BuyType.BONUS_BUY, icons, 10);
64+
assertEquals(30, slotMachine2.payOut(), "Expected payout for BONUS_BUY to be 30");
65+
}
66+
67+
@Test
68+
public void testSetNumOfSlots() {
69+
SlotMachine slotMachine =
70+
new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10);
71+
72+
slotMachine.setNumOfSlots(5);
73+
74+
assertEquals(5, slotMachine.getNumOfSlots(), "The number of slots should be updated to 5");
75+
}
76+
77+
@Test
78+
public void testSetPayAmount() {
79+
SlotMachine slotMachine =
80+
new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10);
81+
82+
slotMachine.setPayAmount(20);
83+
84+
assertEquals(20, slotMachine.getPayAmount(), "The pay amount should be updated to 20");
85+
}
86+
87+
@Test
88+
public void testSetName() {
89+
SlotMachine slotMachine =
90+
new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10);
91+
92+
slotMachine.setName("Lucky Slot");
93+
94+
assertEquals("Lucky Slot", slotMachine.getName(), "The name should be updated to 'Lucky Slot'");
95+
}
96+
97+
@Test
98+
public void testSetBuyType() {
99+
SlotMachine slotMachine =
100+
new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10);
101+
102+
slotMachine.setBuyType(BuyType.BONUS_BUY);
103+
104+
assertEquals(
105+
BuyType.BONUS_BUY, slotMachine.getBuyType(), "The buy type should be updated to BONUS_BUY");
106+
}
107+
108+
@Test
109+
public void testSetIconList() {
110+
SlotMachine slotMachine =
111+
new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10);
112+
113+
ArrayList<String> newIcons = new ArrayList<>();
114+
newIcons.add("🍒");
115+
newIcons.add("🍋");
116+
newIcons.add("🍊");
117+
118+
slotMachine.setIconList(newIcons);
119+
120+
assertEquals(
121+
newIcons, slotMachine.getIconList(), "The icon list should be updated with the new icons");
122+
}
123+
124+
@Test
125+
public void testSetMoneyNeeded() {
126+
SlotMachine slotMachine =
127+
new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10);
128+
129+
slotMachine.setMoneyNeeded(15);
130+
131+
assertEquals(15, slotMachine.getMoneyNeeded(), "The money needed should be updated to 15");
132+
}
133+
134+
@Test
135+
public void testSpinThrowsExceptionWhenMoneyIsInsufficient() {
136+
ArrayList<String> icons = new ArrayList<>();
137+
icons.add("🍒");
138+
icons.add("🍋");
139+
icons.add("🍊");
140+
141+
SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, icons, 10);
142+
143+
Exception exception =
144+
assertThrows(
145+
InvalidPayAmountException.class,
146+
() -> {
147+
slotMachine.spin(5);
148+
});
149+
150+
assertEquals("Amount inavalid", exception.getMessage());
151+
}
152+
153+
@Test
154+
public void testSpinShufflesIconList() throws InvalidPayAmountException {
155+
ArrayList<String> icons = new ArrayList<>();
156+
icons.add("🍒");
157+
icons.add("🍋");
158+
icons.add("🍊");
159+
160+
SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, icons, 10);
161+
162+
ArrayList<String> originalIcons = new ArrayList<>(slotMachine.getIconList());
163+
164+
slotMachine.spin(10);
165+
166+
ArrayList<String> shuffledIcons = slotMachine.getIconList();
167+
168+
Collections.sort(originalIcons);
169+
Collections.sort(shuffledIcons);
170+
171+
assertEquals(
172+
originalIcons, shuffledIcons, "The icon list should have the same contents after spinning");
173+
}
174+
}

0 commit comments

Comments
 (0)