diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/shawndunsmore/BuyType.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/shawndunsmore/BuyType.java new file mode 100644 index 000000000..f29599cd1 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/shawndunsmore/BuyType.java @@ -0,0 +1,7 @@ +package com.codedifferently.lesson16.shawndunsmore; + +public enum BuyType { + BONUS_BUY, + DOUBLE_CHANCE, + NORMAL_BUY, +} diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/shawndunsmore/InvalidPayAmountException.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/shawndunsmore/InvalidPayAmountException.java new file mode 100644 index 000000000..df082503a --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/shawndunsmore/InvalidPayAmountException.java @@ -0,0 +1,8 @@ +package com.codedifferently.lesson16.shawndunsmore; + +public class InvalidPayAmountException extends Exception { + + public InvalidPayAmountException(String message) { + super(message); + } +} diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/shawndunsmore/SlotMachine.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/shawndunsmore/SlotMachine.java new file mode 100644 index 000000000..1dbc998bd --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/shawndunsmore/SlotMachine.java @@ -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 iconList; + private int moneyNeeded; + + public SlotMachine( + int numOfSlots, + int payAmount, + String name, + BuyType buyType, + ArrayList 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 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 spin(int money) throws InvalidPayAmountException { + if (money < moneyNeeded) { + throw new InvalidPayAmountException("Amount inavalid"); + } + Collections.shuffle(iconList); + return iconList; + } + + public ArrayList 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 iconList) { + this.iconList = iconList; + } + + public void setMoneyNeeded(int moneyNeeded) { + this.moneyNeeded = moneyNeeded; + } +} diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/ShawnDunsmore/SlotMachineTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/ShawnDunsmore/SlotMachineTest.java new file mode 100644 index 000000000..be5cc64b5 --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/ShawnDunsmore/SlotMachineTest.java @@ -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 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 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 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 icons = new ArrayList<>(); + icons.add("🍒"); + icons.add("🍋"); + icons.add("🍊"); + + SlotMachine slotMachine = new SlotMachine(3, 10, "Test Slot", BuyType.NORMAL_BUY, icons, 10); + + ArrayList originalIcons = new ArrayList<>(slotMachine.getIconList()); + + slotMachine.spin(10); + + ArrayList shuffledIcons = slotMachine.getIconList(); + + Collections.sort(originalIcons); + Collections.sort(shuffledIcons); + + assertEquals( + originalIcons, shuffledIcons, "The icon list should have the same contents after spinning"); + } +}