Skip to content

chore: added class NintendoSwitch with a test method and exception. #514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.codedifferently.lesson16.NintendoSwitch;

public class InvalidBatteryException extends Exception {
public InvalidBatteryException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -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<String> installedGames;

public class InvalidBatteryException extends Exception {
public InvalidBatteryException(String message) {
super(message);
}
}

public NintendoSwitch(
String serialNumber,
Model model,
boolean isDocked,
double batteryLife,
ArrayList<String> 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<String> 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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String> installedGames;

public ArrayList<String> getInstalledGames() {
return installedGames;
}

@BeforeEach
public void setUp() {
ArrayList<String> 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<String> emptyGames = new ArrayList<>();
NintendoSwitch brokenSwitch =
new NintendoSwitch("SWITCH20250002", NintendoSwitch.Model.LITE, false, -1.0, emptyGames);
assertThrows(
NintendoSwitch.InvalidBatteryException.class, () -> brokenSwitch.checkBatteryStatus());
}
}
Loading