-
Notifications
You must be signed in to change notification settings - Fork 24
feat: implement lesson 16 custom class NintendoSwitch.java & test cases with exception handling #521
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
Open
jbey251
wants to merge
1
commit into
code-differently:main
Choose a base branch
from
jbey251:L16
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: implement lesson 16 custom class NintendoSwitch.java & test cases with exception handling #521
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
...pp/src/main/java/com/codedifferently/lesson16/NintendoSwitch/InvalidBatteryException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
...objects_app/src/main/java/com/codedifferently/lesson16/NintendoSwitch/NintendoSwitch.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> installedGames; | ||
|
||
// This will be the constructor for the NintendoSwitch class. | ||
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; | ||
} | ||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to use JavaDoc format if you're adding comments for functions. |
||
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<String> getInstalledGames() { | ||
return installedGames; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...cts/objects_app/src/test/java/com/codedifferently/lesson16/Switch/NintendoSwitchTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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")); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Package names and folder path is incorrect. Should be lowercase.