Skip to content

Commit 07f3077

Browse files
committed
chore: added class NintendoSwitch with a test method and exception.
1 parent faa4f80 commit 07f3077

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.NintendoSwitch;
2+
3+
public class InvalidBatteryException extends Exception {
4+
public InvalidBatteryException(String message) {
5+
super(message);
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.codedifferently.lesson16.NintendoSwitch;
2+
3+
import java.util.ArrayList;
4+
5+
public class NintendoSwitch {
6+
7+
public enum Model {
8+
STANDARD,
9+
LITE,
10+
OLED
11+
}
12+
13+
private String serialNumber;
14+
private Model model;
15+
private boolean isDocked;
16+
private double batteryLife; // This will be in hours.
17+
private ArrayList<String> installedGames;
18+
19+
public class InvalidBatteryException extends Exception {
20+
public InvalidBatteryException(String message) {
21+
super(message);
22+
}
23+
}
24+
25+
public NintendoSwitch(
26+
String serialNumber,
27+
Model model,
28+
boolean isDocked,
29+
double batteryLife,
30+
ArrayList<String> installedGames) {
31+
this.serialNumber = serialNumber;
32+
this.model = model;
33+
this.isDocked = isDocked;
34+
this.batteryLife = batteryLife;
35+
this.installedGames = installedGames;
36+
}
37+
38+
// Function 1: This will check the battery status.
39+
public void checkBatteryStatus() throws InvalidBatteryException {
40+
// Conditional expression checking battery status
41+
if (batteryLife < 0) {
42+
throw new InvalidBatteryException("Battery life cannot be negative.");
43+
}
44+
System.out.println("Battery life: " + batteryLife + " hours.");
45+
}
46+
47+
// Function 2: Adds a game to the installed games collection
48+
public void installGame(String game) {
49+
installedGames.add(game);
50+
System.out.println(game + " has been added to your Nintendo Switch.");
51+
}
52+
53+
// Function 3: Displays all installed games using a loop
54+
public void displayInstalledGames() {
55+
System.out.println("Installed games on the Nintendo Switch:");
56+
// Using a loop to iterate over installed games
57+
for (String game : installedGames) {
58+
System.out.println("- " + game);
59+
}
60+
}
61+
62+
// Function 4: Checks if the Switch is docked and suggests a mode (using conditional expression)
63+
public void suggestMode() {
64+
// Using a conditional expression to suggest a mode
65+
String mode = isDocked ? "TV mode" : "Handheld mode";
66+
System.out.println("Your Nintendo Switch is in " + mode + ".");
67+
}
68+
69+
// Getter for Model
70+
public Model getModel() {
71+
return model;
72+
}
73+
74+
// Getter for Serial Number
75+
public String getSerialNumber() {
76+
return serialNumber;
77+
}
78+
79+
// Main method to test the class
80+
public static void main(String[] args) {
81+
// Create an ArrayList to hold installed games
82+
ArrayList<String> games = new ArrayList<>();
83+
games.add("Super Mario Odyssey");
84+
games.add("The Legend of Zelda: Breath of the Wild");
85+
86+
// This will create a NintendoSwitch object
87+
NintendoSwitch switchConsole = new NintendoSwitch("SN123456789", Model.OLED, true, 4.5, games);
88+
89+
try {
90+
// Test methods
91+
switchConsole.checkBatteryStatus(); // Check battery life
92+
switchConsole.installGame("Animal Crossing: New Horizons"); // Install a new game
93+
switchConsole.displayInstalledGames(); // Display installed games
94+
switchConsole.suggestMode(); // This suggests whether to use in docked or handheld mode
95+
} catch (InvalidBatteryException e) {
96+
System.out.println("Error: " + e.getMessage());
97+
}
98+
}
99+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.codedifferently.lesson16.Switch;
2+
3+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import java.util.ArrayList;
9+
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
13+
import com.codedifferently.lesson16.NintendoSwitch.NintendoSwitch;
14+
15+
public class NintendoSwitchTest {
16+
17+
private NintendoSwitch mySwitch;
18+
19+
private ArrayList<String> installedGames;
20+
21+
public ArrayList<String> getInstalledGames() {
22+
return installedGames;
23+
}
24+
25+
@BeforeEach
26+
public void setUp() {
27+
ArrayList<String> preloadGames = new ArrayList<>();
28+
preloadGames.add("Mario Kart 8 Deluxe");
29+
preloadGames.add("Splatoon 3");
30+
31+
mySwitch =
32+
new NintendoSwitch(
33+
"SWITCH20250001", NintendoSwitch.Model.STANDARD, false, 3.0, preloadGames);
34+
}
35+
36+
@Test
37+
public void testGetModelAndSerialNumber() {
38+
assertEquals(NintendoSwitch.Model.STANDARD, mySwitch.getModel());
39+
assertEquals("SWITCH20250001", mySwitch.getSerialNumber());
40+
}
41+
42+
@Test
43+
public void testCheckBatteryStatus_NoExceptionThrown() {
44+
assertDoesNotThrow(() -> mySwitch.checkBatteryStatus());
45+
}
46+
47+
@Test
48+
public void testInstallGame() {
49+
mySwitch.installGame("Metroid Dread");
50+
assertTrue(mySwitch.getInstalledGames().contains("Metroid Dread"));
51+
}
52+
53+
@Test
54+
public void testDisplayInstalledGames_Output() {
55+
// This one just ensures method runs without crashing
56+
assertDoesNotThrow(() -> mySwitch.displayInstalledGames());
57+
}
58+
59+
@Test
60+
public void testSuggestMode() {
61+
// This one doesn't return anything, so we just check that it doesn't throw
62+
assertDoesNotThrow(() -> mySwitch.suggestMode());
63+
}
64+
65+
@Test
66+
public void testInvalidBatteryExceptionThrown() {
67+
ArrayList<String> emptyGames = new ArrayList<>();
68+
NintendoSwitch brokenSwitch =
69+
new NintendoSwitch("SWITCH20250002", NintendoSwitch.Model.LITE, false, -1.0, emptyGames);
70+
assertThrows(
71+
NintendoSwitch.InvalidBatteryException.class, () -> brokenSwitch.checkBatteryStatus());
72+
}
73+
}

0 commit comments

Comments
 (0)