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..0b6fa69f5 --- /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/NintendoSwitch.java b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/NintendoSwitch/NintendoSwitch.java new file mode 100644 index 000000000..249b541b5 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/NintendoSwitch/NintendoSwitch.java @@ -0,0 +1,99 @@ +package com.codedifferently.lesson16.NintendoSwitch; + +import java.util.ArrayList; + +public class NintendoSwitch { + + 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; + + public class InvalidBatteryException extends Exception { + public InvalidBatteryException(String message) { + super(message); + } + } + + public NintendoSwitch( + 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; + } + + // Function 1: This will check the battery status. + public void checkBatteryStatus() throws InvalidBatteryException { + // Conditional expression checking battery status + if (batteryLife < 0) { + throw new InvalidBatteryException("Battery life cannot be negative."); + } + System.out.println("Battery life: " + batteryLife + " hours."); + } + + // Function 2: Adds a game to the installed games collection + public void installGame(String game) { + installedGames.add(game); + System.out.println(game + " has been added to your Nintendo Switch."); + } + + // Function 3: Displays all installed games using a loop + public void displayInstalledGames() { + System.out.println("Installed games on the Nintendo Switch:"); + // Using a loop to iterate over installed games + for (String game : installedGames) { + System.out.println("- " + game); + } + } + + // Function 4: Checks if the Switch is docked and suggests a mode (using conditional expression) + public void suggestMode() { + // Using a conditional expression to suggest a mode + String mode = isDocked ? "TV mode" : "Handheld mode"; + System.out.println("Your Nintendo Switch is in " + mode + "."); + } + + // Getter for Model + public Model getModel() { + return model; + } + + // Getter for Serial Number + public String getSerialNumber() { + return serialNumber; + } + + // Main method to test the class + public static void main(String[] args) { + // Create an ArrayList to hold installed games + ArrayList games = new ArrayList<>(); + games.add("Super Mario Odyssey"); + games.add("The Legend of Zelda: Breath of the Wild"); + + // This will create a NintendoSwitch object + NintendoSwitch switchConsole = new NintendoSwitch("SN123456789", Model.OLED, true, 4.5, games); + + try { + // Test methods + switchConsole.checkBatteryStatus(); // Check battery life + switchConsole.installGame("Animal Crossing: New Horizons"); // Install a new game + switchConsole.displayInstalledGames(); // Display installed games + switchConsole.suggestMode(); // This suggests whether to use in docked or handheld mode + } catch (InvalidBatteryException e) { + System.out.println("Error: " + e.getMessage()); + } + } +} diff --git a/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/Switch/NintendoSwitchTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/Switch/NintendoSwitchTest.java new file mode 100644 index 000000000..cc08cd55b --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/Switch/NintendoSwitchTest.java @@ -0,0 +1,73 @@ +package com.codedifferently.lesson16.Switch; + +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.util.ArrayList; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.codedifferently.lesson16.NintendoSwitch.NintendoSwitch; + +public class NintendoSwitchTest { + + private NintendoSwitch mySwitch; + + private ArrayList installedGames; + + public ArrayList getInstalledGames() { + return installedGames; + } + + @BeforeEach + public void setUp() { + ArrayList preloadGames = new ArrayList<>(); + preloadGames.add("Mario Kart 8 Deluxe"); + preloadGames.add("Splatoon 3"); + + mySwitch = + new NintendoSwitch( + "SWITCH20250001", NintendoSwitch.Model.STANDARD, false, 3.0, preloadGames); + } + + @Test + public void testGetModelAndSerialNumber() { + assertEquals(NintendoSwitch.Model.STANDARD, mySwitch.getModel()); + assertEquals("SWITCH20250001", mySwitch.getSerialNumber()); + } + + @Test + public void testCheckBatteryStatus_NoExceptionThrown() { + assertDoesNotThrow(() -> mySwitch.checkBatteryStatus()); + } + + @Test + public void testInstallGame() { + mySwitch.installGame("Metroid Dread"); + assertTrue(mySwitch.getInstalledGames().contains("Metroid Dread")); + } + + @Test + public void testDisplayInstalledGames_Output() { + // This one just ensures method runs without crashing + assertDoesNotThrow(() -> mySwitch.displayInstalledGames()); + } + + @Test + public void testSuggestMode() { + // This one doesn't return anything, so we just check that it doesn't throw + assertDoesNotThrow(() -> mySwitch.suggestMode()); + } + + @Test + public void testInvalidBatteryExceptionThrown() { + ArrayList emptyGames = new ArrayList<>(); + NintendoSwitch brokenSwitch = + new NintendoSwitch("SWITCH20250002", NintendoSwitch.Model.LITE, false, -1.0, emptyGames); + assertThrows( + NintendoSwitch.InvalidBatteryException.class, () -> brokenSwitch.checkBatteryStatus()); + } +}