-
Notifications
You must be signed in to change notification settings - Fork 25
Feat: Adds Shawn Dunsmore Jr lesson 16 SlotMachine.java and SlotMachi… #529
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 16 commits into
code-differently:main
from
Djtheman1:Shawn-lesson16
Nov 13, 2024
Merged
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
910c14e
Feat: Adds Shawn Dunsmore Jr lesson 16 SlotMachine.java and SlotMachi…
Djtheman1 24359c0
Added Spotless
Djtheman1 531ea2a
Feat: Adds Shawn Dunsmore Jr lesson 16 91 Percent Coverage
Djtheman1 c7713a4
Feat: Adds Shawn Dunsmore Jr Added Spotless
Djtheman1 e776a62
Feat: Adds Shawn Dunsmore Jr lesson 16 Added For Loop and Test
Djtheman1 5bdc97a
Feat: Adds Shawn Dunsmore Jr Added Spotless
Djtheman1 7859007
Adds Fixed Changed
Djtheman1 6a7cf15
refactor: proper casing
Djtheman1 1680482
refactor: tab
Djtheman1 8febffe
Removes File Live Server
Djtheman1 cd338d3
feat: removed files
Djtheman1 f3bb860
refactor: lowercase
Djtheman1 7afa9a7
refactor: spacing
Djtheman1 ccded0c
Adds fixed changes
Djtheman1 1235474
Update build.gradle.kts
Djtheman1 d50f330
Update build.gradle.kts
Djtheman1 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
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
7 changes: 7 additions & 0 deletions
7
...objects/objects_app/src/main/java/com/codedifferently/lesson16/ShawnDunsmore/BuyType.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.ShawnDunsmore; | ||
|
|
||
| public enum BuyType { | ||
| BONUS_BUY, | ||
| DOUBLE_CHANCE, | ||
| NORMAL_BUY, | ||
| } |
8 changes: 8 additions & 0 deletions
8
...p/src/main/java/com/codedifferently/lesson16/ShawnDunsmore/InvalidPayAmountException.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,8 @@ | ||
| package com.codedifferently.lesson16.ShawnDunsmore; | ||
|
|
||
| public class InvalidPayAmountException extends Exception { | ||
|
|
||
| public InvalidPayAmountException(String message) { | ||
| super(message); | ||
| } | ||
| } |
106 changes: 106 additions & 0 deletions
106
...cts/objects_app/src/main/java/com/codedifferently/lesson16/ShawnDunsmore/SlotMachine.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,106 @@ | ||
| package com.codedifferently.lesson16.ShawnDunsmore; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
|
|
||
| public class SlotMachine { | ||
| private int numOfSlots; | ||
| private int payAmount; | ||
| private String name; | ||
| private BuyType buyType; | ||
| private ArrayList<String> iconList; | ||
| private int moneyNeeded; | ||
|
|
||
| public SlotMachine( | ||
| int numOfSlots, | ||
| int payAmount, | ||
| String name, | ||
| BuyType buyType, | ||
| ArrayList<String> iconList, | ||
| int moneyNeeded) { | ||
| this.numOfSlots = numOfSlots; | ||
| this.payAmount = payAmount; | ||
| this.name = name; | ||
| this.buyType = buyType; | ||
| this.iconList = iconList; | ||
| this.moneyNeeded = moneyNeeded; | ||
| } | ||
|
|
||
| public int getNumOfSlots() { | ||
| return numOfSlots; | ||
| } | ||
|
|
||
| public int getPayAmount() { | ||
| return payAmount; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public BuyType getBuyType() { | ||
| return buyType; | ||
| } | ||
|
|
||
| public ArrayList<String> getIconList() { | ||
| return iconList; | ||
| } | ||
|
|
||
| public int payOut() { | ||
| if (buyType.equals(BuyType.DOUBLE_CHANCE)) { | ||
| return payAmount * 2; | ||
| } | ||
|
|
||
| if (buyType.equals(BuyType.BONUS_BUY)) { | ||
| return payAmount * 3; | ||
| } | ||
| return payAmount; | ||
| } | ||
|
|
||
| public ArrayList<String> spin(int money) throws InvalidPayAmountException { | ||
| if (money < moneyNeeded) { | ||
| throw new InvalidPayAmountException("Amount inavalid"); | ||
| } | ||
| Collections.shuffle(iconList); | ||
| return iconList; | ||
| } | ||
|
|
||
| public ArrayList<String> spin(int money, int numOfSpins) throws InvalidPayAmountException { | ||
| if (money < moneyNeeded) { | ||
| throw new InvalidPayAmountException("Amount inavalid"); | ||
| } | ||
| for (int i = 0; i < numOfSpins; i++) { | ||
| Collections.shuffle(iconList); | ||
| } | ||
|
|
||
| return iconList; | ||
| } | ||
|
|
||
| public int getMoneyNeeded() { | ||
| return moneyNeeded; | ||
| } | ||
|
|
||
| public void setNumOfSlots(int numOfSlots) { | ||
| this.numOfSlots = numOfSlots; | ||
| } | ||
|
|
||
| public void setPayAmount(int payAmount) { | ||
| this.payAmount = payAmount; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public void setBuyType(BuyType buyType) { | ||
| this.buyType = buyType; | ||
| } | ||
|
|
||
| public void setIconList(ArrayList<String> iconList) { | ||
| this.iconList = iconList; | ||
| } | ||
|
|
||
| public void setMoneyNeeded(int moneyNeeded) { | ||
| this.moneyNeeded = moneyNeeded; | ||
| } | ||
| } |
174 changes: 174 additions & 0 deletions
174
...objects_app/src/test/java/com/codedifferently/lesson16/ShawnDunsmore/SlotMachineTest.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,174 @@ | ||
| package com.codedifferently.lesson16.ShawnDunsmore; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class SlotMachineTest { | ||
| @Test | ||
| public void testMoney_Exists() { | ||
| SlotMachine slot = new SlotMachine(0, 0, null, null, null, 10); | ||
| assertEquals(10, slot.getMoneyNeeded()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNumOfSlots_Exists() { | ||
| SlotMachine slot = new SlotMachine(1, 0, null, null, null, 10); | ||
| assertEquals(1, slot.getNumOfSlots()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNameOfSlot_Exists() { | ||
| SlotMachine slot = new SlotMachine(1, 0, "Goldie", null, null, 10); | ||
| assertEquals("Goldie", slot.getName()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPayAmount_Exists() { | ||
| SlotMachine slot = new SlotMachine(1, 10, "Goldie", null, null, 10); | ||
| assertEquals(10, slot.getPayAmount()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBuyType_Exists() { | ||
| SlotMachine slot = new SlotMachine(1, 10, "Goldie", null, null, 10); | ||
| assertEquals(null, slot.getBuyType()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUseDoubleSlots() { | ||
| SlotMachine slottwo = new SlotMachine(1, 10, "Goldie", null, null, 10); | ||
| SlotMachine slotone = new SlotMachine(1, 10, "Diamond", null, null, 10); | ||
| assertEquals(1, slotone.getNumOfSlots()); | ||
| assertEquals(1, slottwo.getNumOfSlots()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSlotSpin() { | ||
| SlotMachine slot = new SlotMachine(1, 10, "Goldie", null, null, 10); | ||
| assertEquals(null, slot.getIconList()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testPayOut() { | ||
|
|
||
| ArrayList<String> icons = new ArrayList<>(); | ||
| SlotMachine slotMachine1 = | ||
| new SlotMachine(1, 10, "Test Slot 1", BuyType.DOUBLE_CHANCE, icons, 10); | ||
| assertEquals(20, slotMachine1.payOut(), "Expected payout for DOUBLE_CHANCE to be 20"); | ||
|
|
||
| SlotMachine slotMachine2 = new SlotMachine(1, 10, "Test Slot 2", BuyType.BONUS_BUY, icons, 10); | ||
| assertEquals(30, slotMachine2.payOut(), "Expected payout for BONUS_BUY to be 30"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSetNumOfSlots() { | ||
| SlotMachine slotMachine = | ||
| new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10); | ||
|
|
||
| slotMachine.setNumOfSlots(5); | ||
|
|
||
| assertEquals(5, slotMachine.getNumOfSlots(), "The number of slots should be updated to 5"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSetPayAmount() { | ||
| SlotMachine slotMachine = | ||
| new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10); | ||
|
|
||
| slotMachine.setPayAmount(20); | ||
|
|
||
| assertEquals(20, slotMachine.getPayAmount(), "The pay amount should be updated to 20"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSetName() { | ||
| SlotMachine slotMachine = | ||
| new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10); | ||
|
|
||
| slotMachine.setName("Lucky Slot"); | ||
|
|
||
| assertEquals("Lucky Slot", slotMachine.getName(), "The name should be updated to 'Lucky Slot'"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSetBuyType() { | ||
| SlotMachine slotMachine = | ||
| new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10); | ||
|
|
||
| slotMachine.setBuyType(BuyType.BONUS_BUY); | ||
|
|
||
| assertEquals( | ||
| BuyType.BONUS_BUY, slotMachine.getBuyType(), "The buy type should be updated to BONUS_BUY"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSetIconList() { | ||
| SlotMachine slotMachine = | ||
| new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10); | ||
|
|
||
| ArrayList<String> newIcons = new ArrayList<>(); | ||
| newIcons.add("🍒"); | ||
| newIcons.add("🍋"); | ||
| newIcons.add("🍊"); | ||
|
|
||
| slotMachine.setIconList(newIcons); | ||
|
|
||
| assertEquals( | ||
| newIcons, slotMachine.getIconList(), "The icon list should be updated with the new icons"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSetMoneyNeeded() { | ||
| SlotMachine slotMachine = | ||
| new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, new ArrayList<>(), 10); | ||
|
|
||
| slotMachine.setMoneyNeeded(15); | ||
|
|
||
| assertEquals(15, slotMachine.getMoneyNeeded(), "The money needed should be updated to 15"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSpinThrowsExceptionWhenMoneyIsInsufficient() { | ||
| ArrayList<String> icons = new ArrayList<>(); | ||
| icons.add("🍒"); | ||
| icons.add("🍋"); | ||
| icons.add("🍊"); | ||
|
|
||
| SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, icons, 10); | ||
|
|
||
| Exception exception = | ||
| assertThrows( | ||
| InvalidPayAmountException.class, | ||
| () -> { | ||
| slotMachine.spin(5); | ||
| }); | ||
|
|
||
| assertEquals("Amount inavalid", exception.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSpinShufflesIconList() throws InvalidPayAmountException { | ||
| ArrayList<String> icons = new ArrayList<>(); | ||
| icons.add("🍒"); | ||
| icons.add("🍋"); | ||
| icons.add("🍊"); | ||
|
|
||
| SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, icons, 10); | ||
|
|
||
| ArrayList<String> originalIcons = new ArrayList<>(slotMachine.getIconList()); | ||
|
|
||
| slotMachine.spin(10); | ||
|
|
||
| ArrayList<String> shuffledIcons = slotMachine.getIconList(); | ||
|
|
||
| Collections.sort(originalIcons); | ||
| Collections.sort(shuffledIcons); | ||
|
|
||
| assertEquals( | ||
| originalIcons, shuffledIcons, "The icon list should have the same contents after spinning"); | ||
| } | ||
| } |
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.