Skip to content

Commit e776a62

Browse files
committed
Feat: Adds Shawn Dunsmore Jr lesson 16 Added For Loop and Test
1 parent c7713a4 commit e776a62

File tree

2 files changed

+142
-1
lines changed

2 files changed

+142
-1
lines changed

β€Žlesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/ShawnDunsmore/SlotMachine.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.codedifferently.lesson16.ShawnDunsmore;
22

33
import java.util.ArrayList;
4+
import java.util.Collections;
45

56
public class SlotMachine {
67
private int numOfSlots;
@@ -56,7 +57,51 @@ public int payOut() {
5657
return payAmount;
5758
}
5859

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+
5979
public int getMoneyNeeded() {
6080
return moneyNeeded;
6181
}
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+
62107
}

β€Žlesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/ShawnDunsmore/SlotMachineTest.java

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.codedifferently.lesson16.ShawnDunsmore;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import java.util.Collections;
56
import java.util.ArrayList;
67
import org.junit.jupiter.api.Test;
78

@@ -61,4 +62,99 @@ public void testPayOut() {
6162
SlotMachine slotMachine2 = new SlotMachine(1, 10, "Test Slot 2", BuyType.BONUS_BUY, icons, 10);
6263
assertEquals(30, slotMachine2.payOut(), "Expected payout for BONUS_BUY to be 30");
6364
}
65+
@Test
66+
public void testSetNumOfSlots() {
67+
SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10);
68+
69+
slotMachine.setNumOfSlots(5);
70+
71+
assertEquals(5, slotMachine.getNumOfSlots(), "The number of slots should be updated to 5");
72+
}
73+
@Test
74+
public void testSetPayAmount() {
75+
SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10);
76+
77+
slotMachine.setPayAmount(20);
78+
79+
assertEquals(20, slotMachine.getPayAmount(), "The pay amount should be updated to 20");
80+
}
81+
82+
@Test
83+
public void testSetName() {
84+
SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10);
85+
86+
slotMachine.setName("Lucky Slot");
87+
88+
assertEquals("Lucky Slot", slotMachine.getName(), "The name should be updated to 'Lucky Slot'");
89+
}
90+
91+
@Test
92+
public void testSetBuyType() {
93+
SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10);
94+
95+
slotMachine.setBuyType(BuyType.BONUS_BUY);
96+
97+
assertEquals(BuyType.BONUS_BUY, slotMachine.getBuyType(), "The buy type should be updated to BONUS_BUY");
98+
}
99+
@Test
100+
public void testSetIconList() {
101+
SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10);
102+
103+
ArrayList<String> newIcons = new ArrayList<>();
104+
newIcons.add("πŸ’");
105+
newIcons.add("πŸ‹");
106+
newIcons.add("🍊");
107+
108+
slotMachine.setIconList(newIcons);
109+
110+
assertEquals(newIcons, slotMachine.getIconList(), "The icon list should be updated with the new icons");
111+
}
112+
113+
@Test
114+
public void testSetMoneyNeeded() {
115+
SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10);
116+
117+
slotMachine.setMoneyNeeded(15);
118+
119+
assertEquals(15, slotMachine.getMoneyNeeded(), "The money needed should be updated to 15");
120+
}
121+
@Test
122+
public void testSpinThrowsExceptionWhenMoneyIsInsufficient() {
123+
ArrayList<String> icons = new ArrayList<>();
124+
icons.add("πŸ’");
125+
icons.add("πŸ‹");
126+
icons.add("🍊");
127+
128+
SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, icons, 10);
129+
130+
Exception exception = assertThrows(InvalidPayAmountException.class, () -> {
131+
slotMachine.spin(5);
132+
});
133+
134+
assertEquals("Amount inavalid", exception.getMessage());
135+
}
136+
137+
@Test
138+
public void testSpinShufflesIconList() throws InvalidPayAmountException {
139+
ArrayList<String> icons = new ArrayList<>();
140+
icons.add("πŸ’");
141+
icons.add("πŸ‹");
142+
icons.add("🍊");
143+
144+
SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, icons, 10);
145+
146+
ArrayList<String> originalIcons = new ArrayList<>(slotMachine.getIconList());
147+
148+
slotMachine.spin(10);
149+
150+
ArrayList<String> shuffledIcons = slotMachine.getIconList();
151+
152+
Collections.sort(originalIcons);
153+
Collections.sort(shuffledIcons);
154+
155+
assertEquals(originalIcons, shuffledIcons, "The icon list should have the same contents after spinning");
156+
}
64157
}
158+
159+
160+

0 commit comments

Comments
Β (0)