Skip to content

Commit f1ddfac

Browse files
chore: implement john's nintendo object and test cases (#565)
* feat: implement custom class NintendoSwitch.java & test cases with exception handling. * chore: made package name and folder path lowercase. Also added JavaDoc formatting. * chore: add proper javadoc * chore: formatting --------- Co-authored-by: Vicente Vigueras <[email protected]>
1 parent a171a9f commit f1ddfac

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-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+
}
Lines changed: 99 additions & 0 deletions
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 nintendo {
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+
/**
20+
* Constructs a Nintendo Switch object with the specified attributes.
21+
*
22+
* @param serialNumber serial number of the device
23+
* @param model the model type of the Nintendo Switch
24+
* @param isDocked indicates if the device is currently docked
25+
* @param batteryLife current battery life in hours
26+
* @param installedGames list of games currently installed on the device
27+
*/
28+
public nintendo(
29+
String serialNumber,
30+
Model model,
31+
boolean isDocked,
32+
double batteryLife,
33+
ArrayList<String> installedGames) {
34+
this.serialNumber = serialNumber;
35+
this.model = model;
36+
this.isDocked = isDocked;
37+
this.batteryLife = batteryLife;
38+
this.installedGames = installedGames;
39+
}
40+
41+
public class InvalidBatteryException extends Exception {
42+
public InvalidBatteryException(String message) {
43+
super(message);
44+
}
45+
}
46+
47+
/**
48+
* Checks the current battery status of the Nintendo Switch. If the battery life is negative, it
49+
* throws an InvalidBatteryException.
50+
*
51+
* @throws InvalidBatteryException if battery life is less than 0
52+
*/
53+
public void checkBatteryStatus() throws InvalidBatteryException {
54+
if (batteryLife < 0) {
55+
throw new InvalidBatteryException("Battery life cannot be negative.");
56+
}
57+
System.out.println("Battery life: " + batteryLife + " hours.");
58+
}
59+
60+
/**
61+
* Installs a game to the Nintendo Switch.
62+
*
63+
* @param game the name of the game to install
64+
*/
65+
public void installGame(String game) {
66+
installedGames.add(game);
67+
System.out.println(game + " has been added to your Nintendo Switch.");
68+
}
69+
70+
/** Displays all the games currently installed on the Nintendo Switch. */
71+
public void displayInstalledGames() {
72+
System.out.println("Installed games on the Nintendo Switch:");
73+
/** Using a normal for loop to iterate over installed games */
74+
for (int i = 0; i < installedGames.size(); i++) {
75+
System.out.println("- " + installedGames.get(i));
76+
}
77+
}
78+
79+
/* Getter methods */
80+
public String getSerialNumber() {
81+
return serialNumber;
82+
}
83+
84+
public Model getModel() {
85+
return model;
86+
}
87+
88+
public boolean isDocked() {
89+
return isDocked;
90+
}
91+
92+
public double getBatteryLife() {
93+
return batteryLife;
94+
}
95+
96+
public ArrayList<String> getInstalledGames() {
97+
return installedGames;
98+
}
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.codedifferently.lesson16.nintendoswitch;
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.io.ByteArrayOutputStream;
9+
import java.io.PrintStream;
10+
import java.util.ArrayList;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
14+
public class nintendoswitchtest {
15+
16+
private nintendo ns;
17+
private ArrayList<String> games;
18+
19+
@BeforeEach
20+
void setUp() {
21+
games = new ArrayList<>();
22+
ns = new nintendo("SN001", nintendo.Model.STANDARD, true, 5.0, games);
23+
}
24+
25+
@Test
26+
void testInstallGame() {
27+
ns.installGame("Zelda");
28+
29+
assertTrue(games.contains("Zelda"), "Game should be installed");
30+
}
31+
32+
@Test
33+
void testCheckBatteryStatusValid() {
34+
assertDoesNotThrow(
35+
() -> ns.checkBatteryStatus(),
36+
"Battery check should not throw an exception for valid battery");
37+
}
38+
39+
@Test
40+
void testCheckBatteryStatusInvalid() {
41+
nintendo faultySwitch =
42+
new nintendo("SN002", nintendo.Model.LITE, false, -2.0, new ArrayList<>());
43+
44+
assertThrows(
45+
nintendo.InvalidBatteryException.class,
46+
faultySwitch::checkBatteryStatus,
47+
"Negative battery should throw InvalidBatteryException");
48+
}
49+
50+
@Test
51+
void testDisplayInstalledGames() {
52+
games.add("Mario Kart");
53+
games.add("Smash Bros");
54+
55+
ByteArrayOutputStream output = new ByteArrayOutputStream();
56+
System.setOut(new PrintStream(output));
57+
58+
ns.displayInstalledGames();
59+
60+
String printedOutput = output.toString();
61+
62+
assertTrue(printedOutput.contains("Mario Kart"));
63+
assertTrue(printedOutput.contains("Smash Bros"));
64+
65+
System.setOut(System.out);
66+
}
67+
68+
@Test
69+
void testInstallMultipleGames() {
70+
ns.installGame("Animal Crossing");
71+
ns.installGame("Splatoon");
72+
73+
assertEquals(2, games.size(), "Two games should be installed");
74+
assertTrue(games.contains("Animal Crossing"));
75+
assertTrue(games.contains("Splatoon"));
76+
}
77+
}

0 commit comments

Comments
 (0)