From 358ac58aa9c86ef60720309a0bf5a3e98c9904b0 Mon Sep 17 00:00:00 2001 From: jbey251 Date: Fri, 11 Apr 2025 20:23:35 +0000 Subject: [PATCH 1/4] feat: implement custom class NintendoSwitch.java & test cases with exception handling. --- .../InvalidBatteryException.java | 7 ++ .../NintendoSwitch/NintendoSwitch.java | 85 +++++++++++++++++++ .../lesson16/Switch/NintendoSwitchTest.java | 78 +++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/NintendoSwitch/InvalidBatteryException.java create mode 100644 lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/NintendoSwitch/NintendoSwitch.java create mode 100644 lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/Switch/NintendoSwitchTest.java 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..c9b249827 --- /dev/null +++ b/lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/NintendoSwitch/NintendoSwitch.java @@ -0,0 +1,85 @@ +package com.codedifferently.lesson16.NintendoSwitch; + +import java.util.ArrayList; + +public class NintendoSwitch { + + public enum Model { + STANDARD, + LITE, + OLED + } + + // This will be the attributes, or member variables, of the NintendoSwitch class. + private String serialNumber; + private Model model; + private boolean isDocked; + private double batteryLife; // This will be in hours. + private ArrayList installedGames; + + // This will be the constructor for the NintendoSwitch class. + 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; + } + + public class InvalidBatteryException extends Exception { + public InvalidBatteryException(String message) { + super(message); + } + } + + // Function 1: This will check the battery status. This also has the exception handling. + 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 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/Switch/NintendoSwitchTest.java b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/Switch/NintendoSwitchTest.java new file mode 100644 index 000000000..c410f9297 --- /dev/null +++ b/lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/Switch/NintendoSwitchTest.java @@ -0,0 +1,78 @@ +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 com.codedifferently.lesson16.NintendoSwitch.NintendoSwitch; +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 NintendoSwitch ns; + private ArrayList games; + + @BeforeEach + void setUp() { + games = new ArrayList<>(); + ns = new NintendoSwitch("SN001", NintendoSwitch.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() { + NintendoSwitch faultySwitch = + new NintendoSwitch("SN002", NintendoSwitch.Model.LITE, false, -2.0, new ArrayList<>()); + + assertThrows( + NintendoSwitch.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); // Reset output + } + + @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")); + } +} From 6e922e3a052861c9606cd9fd7b9107b02237e73e Mon Sep 17 00:00:00 2001 From: jbey251 Date: Sat, 26 Apr 2025 22:53:57 +0000 Subject: [PATCH 2/4] chore: made package name and folder path lowercase. Also added JavaDoc formatting. --- .../InvalidBatteryException.java | 2 +- .../nintendo.java} | 19 +++++++++---------- .../nintendoswitchtest.java} | 17 ++++++++--------- 3 files changed, 18 insertions(+), 20 deletions(-) rename lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/{NintendoSwitch => nintendoswitch}/InvalidBatteryException.java (71%) rename lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/{NintendoSwitch/NintendoSwitch.java => nintendoswitch/nintendo.java} (79%) rename lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/{Switch/NintendoSwitchTest.java => nintendoswitch/nintendoswitchtest.java} (77%) 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 similarity index 71% rename from lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/NintendoSwitch/InvalidBatteryException.java rename to lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/nintendoswitch/InvalidBatteryException.java index 0b6fa69f5..0635a76d5 100644 --- 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 @@ -1,4 +1,4 @@ -package com.codedifferently.lesson16.NintendoSwitch; +package com.codedifferently.lesson16.nintendoswitch; public class InvalidBatteryException extends Exception { public InvalidBatteryException(String 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/nintendo.java similarity index 79% rename from lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/NintendoSwitch/NintendoSwitch.java rename to lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/nintendoswitch/nintendo.java index c9b249827..00bb8e5a5 100644 --- 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/nintendo.java @@ -1,8 +1,8 @@ -package com.codedifferently.lesson16.NintendoSwitch; +package com.codedifferently.lesson16.nintendoswitch; import java.util.ArrayList; -public class NintendoSwitch { +public class nintendo { public enum Model { STANDARD, @@ -18,7 +18,7 @@ public enum Model { private ArrayList installedGames; // This will be the constructor for the NintendoSwitch class. - public NintendoSwitch( + public nintendo( String serialNumber, Model model, boolean isDocked, @@ -37,32 +37,31 @@ public InvalidBatteryException(String message) { } } - // Function 1: This will check the battery status. This also has the exception handling. + /** Function 1: This will check the battery status. This also has the exception handling. */ public void checkBatteryStatus() throws InvalidBatteryException { - // Conditional expression checking battery status + /** 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 + /** 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 + /** Function 3: Displays all installed games using a loop */ public void displayInstalledGames() { System.out.println("Installed games on the Nintendo Switch:"); - // Using a normal for loop to iterate over installed games + /** 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 - + /** Getter methods */ public String getSerialNumber() { return serialNumber; } 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/nintendoswitch/nintendoswitchtest.java similarity index 77% rename from lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/Switch/NintendoSwitchTest.java rename to lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/nintendoswitch/nintendoswitchtest.java index c410f9297..1b31d980c 100644 --- 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/nintendoswitch/nintendoswitchtest.java @@ -1,26 +1,25 @@ -package com.codedifferently.lesson16.Switch; +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 com.codedifferently.lesson16.NintendoSwitch.NintendoSwitch; 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 { +public class nintendoswitchtest { - private NintendoSwitch ns; + private nintendo ns; private ArrayList games; @BeforeEach void setUp() { games = new ArrayList<>(); - ns = new NintendoSwitch("SN001", NintendoSwitch.Model.STANDARD, true, 5.0, games); + ns = new nintendo("SN001", nintendo.Model.STANDARD, true, 5.0, games); } @Test @@ -39,11 +38,11 @@ void testCheckBatteryStatusValid() { @Test void testCheckBatteryStatusInvalid() { - NintendoSwitch faultySwitch = - new NintendoSwitch("SN002", NintendoSwitch.Model.LITE, false, -2.0, new ArrayList<>()); + nintendo faultySwitch = + new nintendo("SN002", nintendo.Model.LITE, false, -2.0, new ArrayList<>()); assertThrows( - NintendoSwitch.InvalidBatteryException.class, + nintendo.InvalidBatteryException.class, faultySwitch::checkBatteryStatus, "Negative battery should throw InvalidBatteryException"); } @@ -63,7 +62,7 @@ void testDisplayInstalledGames() { assertTrue(printedOutput.contains("Mario Kart")); assertTrue(printedOutput.contains("Smash Bros")); - System.setOut(System.out); // Reset output + System.setOut(System.out); } @Test From 8f25b09287bf32887acc27195a8a5d2f5a09ed04 Mon Sep 17 00:00:00 2001 From: Vicente Vigueras Date: Wed, 21 May 2025 13:33:38 -0400 Subject: [PATCH 3/4] chore: add proper javadoc --- .../lesson16/nintendoswitch/nintendo.java | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) 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 index 00bb8e5a5..735339c49 100644 --- 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 @@ -10,14 +10,21 @@ public enum Model { OLED } - // This will be the attributes, or member variables, of the NintendoSwitch class. private String serialNumber; private Model model; private boolean isDocked; private double batteryLife; // This will be in hours. private ArrayList installedGames; - // This will be the constructor for the NintendoSwitch class. + /** + * 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, @@ -37,22 +44,33 @@ public InvalidBatteryException(String message) { } } - /** Function 1: This will check the battery status. This also has the exception handling. */ + + /** + * 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 { - /** 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 */ + /** + * 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."); } - /** Function 3: Displays all installed games using a loop */ + /** + * 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 */ @@ -61,7 +79,7 @@ public void displayInstalledGames() { } } - /** Getter methods */ + /* Getter methods */ public String getSerialNumber() { return serialNumber; } From c2283460683a3f31adc7c41b6a8524c39ae669b4 Mon Sep 17 00:00:00 2001 From: Vicente Vigueras Date: Wed, 21 May 2025 13:35:51 -0400 Subject: [PATCH 4/4] chore: formatting --- .../lesson16/nintendoswitch/nintendo.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) 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 index 735339c49..4f4fd4437 100644 --- 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 @@ -19,10 +19,10 @@ public enum Model { /** * 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 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( @@ -44,10 +44,9 @@ public InvalidBatteryException(String message) { } } - /** - * Checks the current battery status of the Nintendo Switch. - * If the battery life is negative, it throws an InvalidBatteryException. + * 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 */ @@ -68,9 +67,7 @@ public void installGame(String game) { System.out.println(game + " has been added to your Nintendo Switch."); } - /** - * Displays all the games currently installed on the 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 */