Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lesson_16/objects/objects_app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ tasks.named<Test>("test") {
tasks.jacocoTestReport {
dependsOn(tasks.test)
reports {
xml.required = true
xml.required.set(true)

}
}

Expand Down
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,
}
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);
}
}
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;
}
}
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");
}
}