Skip to content

Commit 358ac58

Browse files
committed
feat: implement custom class NintendoSwitch.java & test cases with exception handling.
1 parent faa4f80 commit 358ac58

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-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,85 @@
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+
// This will be the attributes, or member variables, of the NintendoSwitch class.
14+
private String serialNumber;
15+
private Model model;
16+
private boolean isDocked;
17+
private double batteryLife; // This will be in hours.
18+
private ArrayList<String> installedGames;
19+
20+
// This will be the constructor for the NintendoSwitch class.
21+
public NintendoSwitch(
22+
String serialNumber,
23+
Model model,
24+
boolean isDocked,
25+
double batteryLife,
26+
ArrayList<String> installedGames) {
27+
this.serialNumber = serialNumber;
28+
this.model = model;
29+
this.isDocked = isDocked;
30+
this.batteryLife = batteryLife;
31+
this.installedGames = installedGames;
32+
}
33+
34+
public class InvalidBatteryException extends Exception {
35+
public InvalidBatteryException(String message) {
36+
super(message);
37+
}
38+
}
39+
40+
// Function 1: This will check the battery status. This also has the exception handling.
41+
public void checkBatteryStatus() throws InvalidBatteryException {
42+
// Conditional expression checking battery status
43+
if (batteryLife < 0) {
44+
throw new InvalidBatteryException("Battery life cannot be negative.");
45+
}
46+
System.out.println("Battery life: " + batteryLife + " hours.");
47+
}
48+
49+
// Function 2: Adds a game to the installed games collection
50+
public void installGame(String game) {
51+
installedGames.add(game);
52+
System.out.println(game + " has been added to your Nintendo Switch.");
53+
}
54+
55+
// Function 3: Displays all installed games using a loop
56+
public void displayInstalledGames() {
57+
System.out.println("Installed games on the Nintendo Switch:");
58+
// Using a normal for loop to iterate over installed games
59+
for (int i = 0; i < installedGames.size(); i++) {
60+
System.out.println("- " + installedGames.get(i));
61+
}
62+
}
63+
64+
// Getter methods
65+
66+
public String getSerialNumber() {
67+
return serialNumber;
68+
}
69+
70+
public Model getModel() {
71+
return model;
72+
}
73+
74+
public boolean isDocked() {
75+
return isDocked;
76+
}
77+
78+
public double getBatteryLife() {
79+
return batteryLife;
80+
}
81+
82+
public ArrayList<String> getInstalledGames() {
83+
return installedGames;
84+
}
85+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 com.codedifferently.lesson16.NintendoSwitch.NintendoSwitch;
9+
import java.io.ByteArrayOutputStream;
10+
import java.io.PrintStream;
11+
import java.util.ArrayList;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
15+
public class NintendoSwitchTest {
16+
17+
private NintendoSwitch ns;
18+
private ArrayList<String> games;
19+
20+
@BeforeEach
21+
void setUp() {
22+
games = new ArrayList<>();
23+
ns = new NintendoSwitch("SN001", NintendoSwitch.Model.STANDARD, true, 5.0, games);
24+
}
25+
26+
@Test
27+
void testInstallGame() {
28+
ns.installGame("Zelda");
29+
30+
assertTrue(games.contains("Zelda"), "Game should be installed");
31+
}
32+
33+
@Test
34+
void testCheckBatteryStatusValid() {
35+
assertDoesNotThrow(
36+
() -> ns.checkBatteryStatus(),
37+
"Battery check should not throw an exception for valid battery");
38+
}
39+
40+
@Test
41+
void testCheckBatteryStatusInvalid() {
42+
NintendoSwitch faultySwitch =
43+
new NintendoSwitch("SN002", NintendoSwitch.Model.LITE, false, -2.0, new ArrayList<>());
44+
45+
assertThrows(
46+
NintendoSwitch.InvalidBatteryException.class,
47+
faultySwitch::checkBatteryStatus,
48+
"Negative battery should throw InvalidBatteryException");
49+
}
50+
51+
@Test
52+
void testDisplayInstalledGames() {
53+
games.add("Mario Kart");
54+
games.add("Smash Bros");
55+
56+
ByteArrayOutputStream output = new ByteArrayOutputStream();
57+
System.setOut(new PrintStream(output));
58+
59+
ns.displayInstalledGames();
60+
61+
String printedOutput = output.toString();
62+
63+
assertTrue(printedOutput.contains("Mario Kart"));
64+
assertTrue(printedOutput.contains("Smash Bros"));
65+
66+
System.setOut(System.out); // Reset output
67+
}
68+
69+
@Test
70+
void testInstallMultipleGames() {
71+
ns.installGame("Animal Crossing");
72+
ns.installGame("Splatoon");
73+
74+
assertEquals(2, games.size(), "Two games should be installed");
75+
assertTrue(games.contains("Animal Crossing"));
76+
assertTrue(games.contains("Splatoon"));
77+
}
78+
}

0 commit comments

Comments
 (0)