diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/nintendoswitch/InvalidBatteryException.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/nintendoswitch/InvalidBatteryException.java new file mode 100644 index 000000000..0635a76d5 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/nintendoswitch/InvalidBatteryException.java @@ -0,0 +1,7 @@ +package com.codedifferently.lesson16.nintendoswitch; + +public class InvalidBatteryException extends Exception { + public InvalidBatteryException(String message) { + super(message); + } +} diff --git a/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/nintendoswitch/nintendo.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/nintendoswitch/nintendo.java new file mode 100644 index 000000000..4f4fd4437 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/nintendoswitch/nintendo.java @@ -0,0 +1,99 @@ +package com.codedifferently.lesson16.nintendoswitch; + +import java.util.ArrayList; + +public class nintendo { + + public enum Model { + STANDARD, + LITE, + OLED + } + + private String serialNumber; + private Model model; + private boolean isDocked; + private double batteryLife; // This will be in hours. + private ArrayList installedGames; + + /** + * Constructs a Nintendo Switch object with the specified attributes. + * + * @param serialNumber serial number of the device + * @param model the model type of the Nintendo Switch + * @param isDocked indicates if the device is currently docked + * @param batteryLife current battery life in hours + * @param installedGames list of games currently installed on the device + */ + public nintendo( + String serialNumber, + Model model, + boolean isDocked, + double batteryLife, + ArrayList installedGames) { + this.serialNumber = serialNumber; + this.model = model; + this.isDocked = isDocked; + this.batteryLife = batteryLife; + this.installedGames = installedGames; + } + + public class InvalidBatteryException extends Exception { + public InvalidBatteryException(String message) { + super(message); + } + } + + /** + * Checks the current battery status of the Nintendo Switch. If the battery life is negative, it + * throws an InvalidBatteryException. + * + * @throws InvalidBatteryException if battery life is less than 0 + */ + public void checkBatteryStatus() throws InvalidBatteryException { + if (batteryLife < 0) { + throw new InvalidBatteryException("Battery life cannot be negative."); + } + System.out.println("Battery life: " + batteryLife + " hours."); + } + + /** + * Installs a game to the Nintendo Switch. + * + * @param game the name of the game to install + */ + public void installGame(String game) { + installedGames.add(game); + System.out.println(game + " has been added to your Nintendo Switch."); + } + + /** Displays all the games currently installed on the Nintendo Switch. */ + public void displayInstalledGames() { + System.out.println("Installed games on the Nintendo Switch:"); + /** Using a normal for loop to iterate over installed games */ + for (int i = 0; i < installedGames.size(); i++) { + System.out.println("- " + installedGames.get(i)); + } + } + + /* Getter methods */ + public String getSerialNumber() { + return serialNumber; + } + + public Model getModel() { + return model; + } + + public boolean isDocked() { + return isDocked; + } + + public double getBatteryLife() { + return batteryLife; + } + + public ArrayList getInstalledGames() { + return installedGames; + } +} diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/nintendoswitch/nintendoswitchtest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/nintendoswitch/nintendoswitchtest.java new file mode 100644 index 000000000..1b31d980c --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/nintendoswitch/nintendoswitchtest.java @@ -0,0 +1,77 @@ +package com.codedifferently.lesson16.nintendoswitch; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.ArrayList; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class nintendoswitchtest { + + private nintendo ns; + private ArrayList games; + + @BeforeEach + void setUp() { + games = new ArrayList<>(); + ns = new nintendo("SN001", nintendo.Model.STANDARD, true, 5.0, games); + } + + @Test + void testInstallGame() { + ns.installGame("Zelda"); + + assertTrue(games.contains("Zelda"), "Game should be installed"); + } + + @Test + void testCheckBatteryStatusValid() { + assertDoesNotThrow( + () -> ns.checkBatteryStatus(), + "Battery check should not throw an exception for valid battery"); + } + + @Test + void testCheckBatteryStatusInvalid() { + nintendo faultySwitch = + new nintendo("SN002", nintendo.Model.LITE, false, -2.0, new ArrayList<>()); + + assertThrows( + nintendo.InvalidBatteryException.class, + faultySwitch::checkBatteryStatus, + "Negative battery should throw InvalidBatteryException"); + } + + @Test + void testDisplayInstalledGames() { + games.add("Mario Kart"); + games.add("Smash Bros"); + + ByteArrayOutputStream output = new ByteArrayOutputStream(); + System.setOut(new PrintStream(output)); + + ns.displayInstalledGames(); + + String printedOutput = output.toString(); + + assertTrue(printedOutput.contains("Mario Kart")); + assertTrue(printedOutput.contains("Smash Bros")); + + System.setOut(System.out); + } + + @Test + void testInstallMultipleGames() { + ns.installGame("Animal Crossing"); + ns.installGame("Splatoon"); + + assertEquals(2, games.size(), "Two games should be installed"); + assertTrue(games.contains("Animal Crossing")); + assertTrue(games.contains("Splatoon")); + } +}