-
Notifications
You must be signed in to change notification settings - Fork 23
Feat: adds Xbox class with test and exception handling #516
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
Merged
anthonydmays
merged 13 commits into
code-differently:main
from
Dlafferty251:Homework16-Custom-data-Types
Apr 23, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
686e6e5
feat: implement Xbox class with game management and loading functiona…
Dlafferty251 2c7568c
feat: refactor LoadGames class for improved game loading functionality
Dlafferty251 ac4b84b
test: enhance XboxTest with improved assertions and error handling, C…
Dlafferty251 0d27828
feat: implement LoadGame class for loading games from a file and upda…
Dlafferty251 7adbb8d
feat: add DiskDriveFullException
Dlafferty251 f52c9c8
Chore: enhance Xbox class with cleaner comments and organization
Dlafferty251 4549442
test: add unit tests for DiskDriveFullException and LoadGame function…
Dlafferty251 97e489d
feat: add games.csv file into a data folder and update Test Files to …
Dlafferty251 65a12e4
test: add unit tests for DiskDriveFullException and LoadGame function…
Dlafferty251 27b9c6a
feat: refactor LoadGame and Xbox classes to improve game loading and …
Dlafferty251 f9801a0
test: add unit tests for DiskDriveFullException and LoadGame function…
Dlafferty251 0a7baed
Feat: Fixed folder name of test folder to be more conventional
Dlafferty251 ba88be3
Fix: Converts try/catch methods into assertthrows methods
Dlafferty251 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
...ts_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/DiskDriveFullException.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.dylans_xbox; | ||
|
||
public class DiskDriveFullException extends Exception { | ||
public DiskDriveFullException(String message) { | ||
super(message); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/LoadGame.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,31 @@ | ||
package com.codedifferently.lesson16.dylans_xbox; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
public class LoadGame { | ||
private String filePath; | ||
|
||
public LoadGame(String filePath) { | ||
this.filePath = filePath; | ||
} | ||
|
||
public void loadGamesFromFile(Xbox xbox) throws Exception { | ||
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { | ||
String line; | ||
br.readLine(); // Skip the header line | ||
while ((line = br.readLine()) != null) { | ||
String[] gameDetails = line.split(","); | ||
if (gameDetails.length >= 2) { | ||
|
||
int id = Integer.parseInt(gameDetails[0].trim()); | ||
String name = gameDetails[1].trim(); | ||
xbox.getGames().put(id, name); | ||
} | ||
} | ||
} catch (IOException e) { | ||
throw new Exception("Error reading the games file: " + e.getMessage()); | ||
} | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
...n_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.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,104 @@ | ||
package com.codedifferently.lesson16.dylans_xbox; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
public class Xbox { | ||
private ArrayList<String> insertedGames = new ArrayList<>(); | ||
private HashMap<Integer, String> games; | ||
// Declares the model of the Xbox by using the enum XboxModel | ||
private XboxModel model; | ||
private String color; | ||
private int price; | ||
// Declares if there is a disk drive on the Xbox | ||
private boolean diskDrive = false; | ||
|
||
// Defines a fixed set of constants for GameGenre | ||
public enum XboxModel { | ||
XBOX360, | ||
XBOXONE, | ||
XBOXONES, | ||
XBOXONEX, | ||
XBOXSERIESS, | ||
XBOXSERIESX | ||
} | ||
|
||
// Constructor for the Xbox class | ||
public Xbox(String model, int price, String color, boolean diskDrive, boolean diskDriveFull) { | ||
this.model = XboxModel.valueOf(model.toUpperCase()); | ||
this.price = price; | ||
this.color = color; | ||
this.diskDrive = diskDrive; | ||
this.insertedGames = new ArrayList<>(); | ||
this.games = new HashMap<>(); | ||
} | ||
|
||
public int getInsertedGamesSize() { | ||
return insertedGames.size(); | ||
} | ||
|
||
// Getters for the Xbox class | ||
public XboxModel getModel() { | ||
return model; | ||
} | ||
|
||
public HashMap<Integer, String> getGames() { | ||
return games; | ||
} | ||
|
||
public int getPrice() { | ||
return price; | ||
} | ||
|
||
public String getColor() { | ||
return color; | ||
} | ||
|
||
public boolean DiskDrive() { | ||
return diskDrive; | ||
} | ||
|
||
// Method that will add a game to the disk drive | ||
// it will check if the disk drive is empty and if it is, it will add the game to the disk drive | ||
// by turning it to true. | ||
public void inputGame(int id) throws Exception { | ||
if (!diskDrive) { | ||
throw new Exception("This Xbox does not have a disk drive. Cannot insert game."); | ||
} | ||
if (insertedGames.size() >= 2) { | ||
throw new DiskDriveFullException("Disk drive is full. Cannot insert game."); | ||
} | ||
|
||
String gameName = games.get(id); | ||
if (gameName == null) { | ||
throw new Exception("Game with ID: " + id + " does not exist in the library."); | ||
} | ||
if (insertedGames.contains(gameName)) { | ||
throw new Exception("Game \"" + gameName + "\" is already inserted."); | ||
} | ||
|
||
insertedGames.add(gameName); | ||
games.remove(id); | ||
|
||
System.out.println("Game \"" + gameName + "\" (ID: " + id + ") was added to the disk drive."); | ||
} | ||
|
||
// Method that will eject a game from the disk drive | ||
// it will check if the game is in the drive and if it is, it will turn the drive to false. | ||
public void ejectGame(int id) { | ||
if (insertedGames.size() >= 1) { | ||
insertedGames.removeAll(insertedGames); | ||
System.out.println("Game with ID: " + id + " was ejected from the disk drive."); | ||
} else { | ||
System.out.println("Game with ID: " + id + " not found in the disk drive."); | ||
} | ||
} | ||
|
||
// This method will print all the games in the HashMap | ||
// By running a for loop that will iterate through the games | ||
public void printAllGames() { | ||
for (Integer id : games.keySet()) { | ||
System.out.println("Game ID: " + id + ", Game Name: " + games.get(id)); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv
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,11 @@ | ||
id, name | ||
1, Call of Duty | ||
2, Elden Ring | ||
3, Minecraft | ||
4, Monster Hunter | ||
5, Fortnite | ||
6, Marvel Rivals | ||
7, Tetris | ||
8, Madden NFL | ||
9, Terraria | ||
10,Baldur's Gate 3 |
34 changes: 34 additions & 0 deletions
34
...cts_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/DiskdrivefullTest.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,34 @@ | ||
package com.codedifferently.lesson16.dylans_xboxTest; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import com.codedifferently.lesson16.dylans_xbox.DiskDriveFullException; | ||
import com.codedifferently.lesson16.dylans_xbox.LoadGame; | ||
import com.codedifferently.lesson16.dylans_xbox.Xbox; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class DiskdrivefullTest { | ||
@Test | ||
public void testDiskDriveFullException() throws Exception { | ||
// Create Xbox with a working disk drive | ||
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); | ||
LoadGame loader = | ||
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); | ||
|
||
loader.loadGamesFromFile(xbox); // Load game library | ||
|
||
// Insert two games to fill the disk drive | ||
xbox.inputGame(1); | ||
xbox.inputGame(2); | ||
|
||
DiskDriveFullException exception = | ||
assertThrows( | ||
DiskDriveFullException.class, | ||
() -> { | ||
xbox.inputGame(3); // This should trigger the exception | ||
}); | ||
|
||
assertEquals("Disk drive is full. Cannot insert game.", exception.getMessage()); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
.../objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/LoadgameTest.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,27 @@ | ||
package com.codedifferently.lesson16.dylans_xboxTest; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.codedifferently.lesson16.dylans_xbox.LoadGame; | ||
import com.codedifferently.lesson16.dylans_xbox.Xbox; | ||
import java.util.HashMap; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class LoadgameTest { | ||
@Test | ||
public void testLoadGame() throws Exception { | ||
// Create an instance of Xbox | ||
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); | ||
|
||
// Create an instance of LoadGame | ||
LoadGame loader = | ||
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); | ||
|
||
loader.loadGamesFromFile(xbox); | ||
// Check if the game was loaded correctly | ||
HashMap<Integer, String> games = xbox.getGames(); | ||
assertTrue(games.containsKey(1)); // Check that the first game is loaded (ID 1) | ||
assertEquals("Call of Duty", games.get(1)); // Ensure the first game matches the CSV | ||
} | ||
} |
159 changes: 159 additions & 0 deletions
159
...ects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/XboxTest.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,159 @@ | ||
package com.codedifferently.lesson16.dylans_xboxTest; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import com.codedifferently.lesson16.dylans_xbox.DiskDriveFullException; | ||
import com.codedifferently.lesson16.dylans_xbox.LoadGame; | ||
import com.codedifferently.lesson16.dylans_xbox.Xbox; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.util.HashMap; | ||
import org.junit.jupiter.api.Test; // Ensure LoadGame is imported | ||
|
||
public class XboxTest { | ||
|
||
@Test | ||
public void testAddGame() { | ||
LoadGame loader = | ||
new LoadGame( | ||
"src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); // Ensure | ||
// LoadGame | ||
|
||
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); // Create an instance of Xbox | ||
try { | ||
loader.loadGamesFromFile(xbox); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
fail("Exception occurred while loading games: " + e.getMessage()); | ||
} | ||
|
||
HashMap<Integer, String> games = xbox.getGames(); | ||
assertTrue(games.containsKey(1)); // Check that the first game is loaded (ID 1) | ||
assertEquals("Call of Duty", games.get(1)); // Ensure the first game matches the CSV | ||
} | ||
|
||
@Test | ||
public void testAddGameIfFull() { | ||
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); // false means not full yet | ||
LoadGame loader = | ||
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); | ||
|
||
try { | ||
loader.loadGamesFromFile(xbox); | ||
|
||
// Insert two games (disk limit) | ||
xbox.inputGame(1); | ||
xbox.inputGame(2); | ||
|
||
// This third insert should throw an exception | ||
xbox.inputGame(3); | ||
fail("Expected DiskDriveFullException to be thrown."); | ||
} catch (DiskDriveFullException e) { | ||
assertEquals("Disk drive is full. Cannot insert game.", e.getMessage()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
fail("Unexpected exception: " + e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testXboxModelEnumValues() { | ||
Xbox.XboxModel[] models = Xbox.XboxModel.values(); | ||
assertEquals(6, models.length); | ||
assertEquals(Xbox.XboxModel.XBOX360, models[0]); | ||
assertEquals(Xbox.XboxModel.XBOXSERIESX, models[5]); | ||
} | ||
|
||
@Test | ||
public void testPrintAllGames() throws Exception { | ||
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); | ||
LoadGame loader = | ||
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); | ||
|
||
loader.loadGamesFromFile(xbox); // Load games into library | ||
|
||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
PrintStream originalOut = System.out; | ||
System.setOut(new PrintStream(outputStream)); | ||
|
||
xbox.printAllGames(); | ||
|
||
System.setOut(originalOut); | ||
|
||
String expectedOutput = | ||
""" | ||
Game ID: 1, Game Name: Call of Duty | ||
Game ID: 2, Game Name: Elden Ring | ||
Game ID: 3, Game Name: Minecraft | ||
Game ID: 4, Game Name: Monster Hunter | ||
Game ID: 5, Game Name: Fortnite | ||
Game ID: 6, Game Name: Marvel Rivals | ||
Game ID: 7, Game Name: Tetris | ||
Game ID: 8, Game Name: Madden NFL | ||
Game ID: 9, Game Name: Terraria | ||
Game ID: 10, Game Name: Baldur's Gate 3 | ||
"""; | ||
assertEquals(expectedOutput.trim(), outputStream.toString().trim()); | ||
} | ||
|
||
@Test | ||
public void testEjectGame() { | ||
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); | ||
LoadGame loader = | ||
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); | ||
|
||
try { | ||
loader.loadGamesFromFile(xbox); // Load games into library | ||
xbox.inputGame(1); // Insert game with ID 1 | ||
assertEquals(1, xbox.getInsertedGamesSize(), "Game should be inserted."); | ||
|
||
xbox.ejectGame(1); // Eject by name (you may use ID instead if implemented that way) | ||
assertEquals(0, xbox.getInsertedGamesSize(), "The game should be ejected."); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
fail("Exception occurred: " + e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testGetGames() { | ||
|
||
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); | ||
LoadGame loader = | ||
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); | ||
|
||
try { | ||
loader.loadGamesFromFile(xbox); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
fail("Exception occurred while loading games: " + e.getMessage()); | ||
} | ||
|
||
HashMap<Integer, String> games = xbox.getGames(); | ||
|
||
assertEquals(10, games.size(), "There should be 10 games loaded."); | ||
} | ||
|
||
@Test | ||
public void testGetPrice() { | ||
Xbox xbox = new Xbox("XBOX360", 400, "White", true, false); | ||
int price = xbox.getPrice(); | ||
assertEquals(400, price, "The price should be 400."); | ||
} | ||
|
||
@Test | ||
public void testGetColor() { | ||
Xbox xbox = new Xbox("XBOX360", 400, "White", true, false); | ||
String color = xbox.getColor(); | ||
assertEquals("White", color, "The color should be White."); | ||
} | ||
|
||
@Test | ||
public void testDiskDrive() { | ||
Xbox xbox = new Xbox("XBOX360", 400, "White", true, false); | ||
boolean diskDrive = xbox.DiskDrive(); | ||
assertTrue(diskDrive, "The disk drive should be present."); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.