Skip to content

Commit 910c14e

Browse files
committed
Feat: Adds Shawn Dunsmore Jr lesson 16 SlotMachine.java and SlotMachineTest.java
1 parent a3a21bb commit 910c14e

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.codedifferently.lesson16.ShawnDunsmore;
2+
3+
4+
public enum BuyType {
5+
BONUS_BUY,
6+
DOUBLE_CHANCE,
7+
NORMAL_BUY,
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.codedifferently.lesson16.ShawnDunsmore;
2+
3+
4+
public class InvalidPayAmountException extends Exception {
5+
6+
7+
public InvalidPayAmountException(String message) {
8+
super(message);
9+
}
10+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.codedifferently.lesson16.ShawnDunsmore;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.Collections;
6+
7+
8+
public class SlotMachine {
9+
private int numOfSlots;
10+
private int payAmount;
11+
private String name;
12+
private BuyType buyType;
13+
private ArrayList<String> iconList;
14+
private int moneyNeeded;
15+
16+
17+
public SlotMachine(
18+
int numOfSlots,
19+
int payAmount,
20+
String name,
21+
BuyType buyType,
22+
ArrayList<String> iconList,
23+
int moneyNeeded) {
24+
this.numOfSlots = numOfSlots;
25+
this.payAmount = payAmount;
26+
this.name = name;
27+
this.buyType = buyType;
28+
this.iconList = iconList;
29+
this.moneyNeeded = moneyNeeded;
30+
}
31+
32+
33+
public int getNumOfSlots() {
34+
return numOfSlots;
35+
}
36+
37+
38+
public int getPayAmount() {
39+
return payAmount;
40+
}
41+
public String getName() {
42+
return name;
43+
}
44+
45+
46+
public BuyType getBuyType() {
47+
return buyType;
48+
}
49+
50+
51+
public ArrayList<String> getIconList() {
52+
return iconList;
53+
}
54+
55+
56+
57+
58+
public int payOut() {
59+
if (buyType.equals(BuyType.DOUBLE_CHANCE)) {
60+
return payAmount * 2;
61+
}
62+
63+
64+
if (buyType.equals(BuyType.BONUS_BUY)) {
65+
return payAmount * 3;
66+
}
67+
return payAmount;
68+
}
69+
70+
71+
public ArrayList<String> spin(int money) throws InvalidPayAmountException {
72+
if (money < moneyNeeded) {
73+
throw new InvalidPayAmountException("Amount inavalid");
74+
}
75+
Collections.shuffle(iconList);
76+
return iconList;
77+
}
78+
79+
80+
public ArrayList<String> spin(int money, int numOfSpins) throws InvalidPayAmountException {
81+
if (money < moneyNeeded) {
82+
throw new InvalidPayAmountException("Amount inavalid");
83+
}
84+
for (int i = 0; i < numOfSpins; i++) {
85+
Collections.shuffle(iconList);
86+
}
87+
88+
89+
return iconList;
90+
}
91+
92+
93+
public int getMoneyNeeded() {
94+
return moneyNeeded;
95+
}
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.codedifferently.lesson16.ShawnDunsmore;
2+
3+
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
7+
import java.util.ArrayList;
8+
import org.junit.jupiter.api.Test;
9+
10+
11+
public class SlotMachineTest {
12+
@Test
13+
public void testMoney_Exists() {
14+
SlotMachine slot = new SlotMachine(0, 0, null, null, null, 10);
15+
assertEquals(10, slot.getMoneyNeeded());
16+
}
17+
18+
19+
@Test
20+
public void testNumOfSlots_Exists() {
21+
SlotMachine slot = new SlotMachine(1, 0, null, null, null, 10);
22+
assertEquals(1, slot.getNumOfSlots());
23+
}
24+
25+
26+
@Test
27+
public void testNameOfSlot_Exists() {
28+
SlotMachine slot = new SlotMachine(1, 0, "Goldie", null, null, 10);
29+
assertEquals("Goldie", slot.getName());
30+
}
31+
32+
33+
@Test
34+
public void testPayAmount_Exists() {
35+
SlotMachine slot = new SlotMachine(1, 10, "Goldie", null, null, 10);
36+
assertEquals(10, slot.getPayAmount());
37+
}
38+
39+
40+
@Test
41+
public void testBuyType_Exists() {
42+
SlotMachine slot = new SlotMachine(1, 10, "Goldie", null, null, 10);
43+
assertEquals(null, slot.getBuyType());
44+
}
45+
46+
47+
@Test
48+
public void testUseDoubleSlots() {
49+
SlotMachine slottwo = new SlotMachine(1, 10, "Goldie", null, null, 10);
50+
SlotMachine slotone = new SlotMachine(1, 10, "Diamond", null, null, 10);
51+
assertEquals(1, slotone.getNumOfSlots());
52+
assertEquals(1, slottwo.getNumOfSlots());
53+
}
54+
55+
56+
@Test
57+
public void testSlotSpin() {
58+
SlotMachine slot = new SlotMachine(1, 10, "Goldie", null, null, 10);
59+
assertEquals(null, slot.getIconList());
60+
}
61+
62+
63+
@Test
64+
public void testPayOut() {
65+
66+
67+
ArrayList<String> icons = new ArrayList<>();
68+
SlotMachine slotMachine1 =
69+
new SlotMachine(1, 10, "Test Slot 1", BuyType.DOUBLE_CHANCE, icons, 10);
70+
assertEquals(20, slotMachine1.payOut(), "Expected payout for DOUBLE_CHANCE to be 20");
71+
72+
73+
SlotMachine slotMachine2 = new SlotMachine(1, 10, "Test Slot 2", BuyType.BONUS_BUY, icons, 10);
74+
assertEquals(30, slotMachine2.payOut(), "Expected payout for BONUS_BUY to be 30");
75+
}
76+
}

0 commit comments

Comments
 (0)