Skip to content

Commit d782c88

Browse files
authored
feat: adds Dylan's Xbox class with test and exception handling (#516)
* feat: implement Xbox class with game management and loading functionality Todo: Add error handling and logging * feat: refactor LoadGames class for improved game loading functionality * test: enhance XboxTest with improved assertions and error handling, Currently Failing * feat: implement LoadGame class for loading games from a file and update Xbox class for disk drive management * feat: add DiskDriveFullException * Chore: enhance Xbox class with cleaner comments and organization * test: add unit tests for DiskDriveFullException and LoadGame functionality, Also simplifies imports for XboxTest * feat: add games.csv file into a data folder and update Test Files to be able to find filepath. * test: add unit tests for DiskDriveFullException and LoadGame functionality * feat: refactor LoadGame and Xbox classes to improve game loading and insert disk logic; add unit tests for DiskDriveFullException and LoadGame functionality * test: add unit tests for DiskDriveFullException and LoadGame functionality; include tests for game insertion and ejection * Feat: Fixed folder name of test folder to be more conventional * Fix: Converts try/catch methods into assertthrows methods
1 parent 7f2920a commit d782c88

File tree

7 files changed

+373
-0
lines changed

7 files changed

+373
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.codedifferently.lesson16.dylans_xbox;
2+
3+
public class DiskDriveFullException extends Exception {
4+
public DiskDriveFullException(String message) {
5+
super(message);
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.codedifferently.lesson16.dylans_xbox;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
public class LoadGame {
8+
private String filePath;
9+
10+
public LoadGame(String filePath) {
11+
this.filePath = filePath;
12+
}
13+
14+
public void loadGamesFromFile(Xbox xbox) throws Exception {
15+
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
16+
String line;
17+
br.readLine(); // Skip the header line
18+
while ((line = br.readLine()) != null) {
19+
String[] gameDetails = line.split(",");
20+
if (gameDetails.length >= 2) {
21+
22+
int id = Integer.parseInt(gameDetails[0].trim());
23+
String name = gameDetails[1].trim();
24+
xbox.getGames().put(id, name);
25+
}
26+
}
27+
} catch (IOException e) {
28+
throw new Exception("Error reading the games file: " + e.getMessage());
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.codedifferently.lesson16.dylans_xbox;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
6+
public class Xbox {
7+
private ArrayList<String> insertedGames = new ArrayList<>();
8+
private HashMap<Integer, String> games;
9+
// Declares the model of the Xbox by using the enum XboxModel
10+
private XboxModel model;
11+
private String color;
12+
private int price;
13+
// Declares if there is a disk drive on the Xbox
14+
private boolean diskDrive = false;
15+
16+
// Defines a fixed set of constants for GameGenre
17+
public enum XboxModel {
18+
XBOX360,
19+
XBOXONE,
20+
XBOXONES,
21+
XBOXONEX,
22+
XBOXSERIESS,
23+
XBOXSERIESX
24+
}
25+
26+
// Constructor for the Xbox class
27+
public Xbox(String model, int price, String color, boolean diskDrive, boolean diskDriveFull) {
28+
this.model = XboxModel.valueOf(model.toUpperCase());
29+
this.price = price;
30+
this.color = color;
31+
this.diskDrive = diskDrive;
32+
this.insertedGames = new ArrayList<>();
33+
this.games = new HashMap<>();
34+
}
35+
36+
public int getInsertedGamesSize() {
37+
return insertedGames.size();
38+
}
39+
40+
// Getters for the Xbox class
41+
public XboxModel getModel() {
42+
return model;
43+
}
44+
45+
public HashMap<Integer, String> getGames() {
46+
return games;
47+
}
48+
49+
public int getPrice() {
50+
return price;
51+
}
52+
53+
public String getColor() {
54+
return color;
55+
}
56+
57+
public boolean DiskDrive() {
58+
return diskDrive;
59+
}
60+
61+
// Method that will add a game to the disk drive
62+
// it will check if the disk drive is empty and if it is, it will add the game to the disk drive
63+
// by turning it to true.
64+
public void inputGame(int id) throws Exception {
65+
if (!diskDrive) {
66+
throw new Exception("This Xbox does not have a disk drive. Cannot insert game.");
67+
}
68+
if (insertedGames.size() >= 2) {
69+
throw new DiskDriveFullException("Disk drive is full. Cannot insert game.");
70+
}
71+
72+
String gameName = games.get(id);
73+
if (gameName == null) {
74+
throw new Exception("Game with ID: " + id + " does not exist in the library.");
75+
}
76+
if (insertedGames.contains(gameName)) {
77+
throw new Exception("Game \"" + gameName + "\" is already inserted.");
78+
}
79+
80+
insertedGames.add(gameName);
81+
games.remove(id);
82+
83+
System.out.println("Game \"" + gameName + "\" (ID: " + id + ") was added to the disk drive.");
84+
}
85+
86+
// Method that will eject a game from the disk drive
87+
// it will check if the game is in the drive and if it is, it will turn the drive to false.
88+
public void ejectGame(int id) {
89+
if (insertedGames.size() >= 1) {
90+
insertedGames.removeAll(insertedGames);
91+
System.out.println("Game with ID: " + id + " was ejected from the disk drive.");
92+
} else {
93+
System.out.println("Game with ID: " + id + " not found in the disk drive.");
94+
}
95+
}
96+
97+
// This method will print all the games in the HashMap
98+
// By running a for loop that will iterate through the games
99+
public void printAllGames() {
100+
for (Integer id : games.keySet()) {
101+
System.out.println("Game ID: " + id + ", Game Name: " + games.get(id));
102+
}
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
id, name
2+
1, Call of Duty
3+
2, Elden Ring
4+
3, Minecraft
5+
4, Monster Hunter
6+
5, Fortnite
7+
6, Marvel Rivals
8+
7, Tetris
9+
8, Madden NFL
10+
9, Terraria
11+
10,Baldur's Gate 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.codedifferently.lesson16.dylans_xboxTest;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import com.codedifferently.lesson16.dylans_xbox.DiskDriveFullException;
7+
import com.codedifferently.lesson16.dylans_xbox.LoadGame;
8+
import com.codedifferently.lesson16.dylans_xbox.Xbox;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class DiskdrivefullTest {
12+
@Test
13+
public void testDiskDriveFullException() throws Exception {
14+
// Create Xbox with a working disk drive
15+
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
16+
LoadGame loader =
17+
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv");
18+
19+
loader.loadGamesFromFile(xbox); // Load game library
20+
21+
// Insert two games to fill the disk drive
22+
xbox.inputGame(1);
23+
xbox.inputGame(2);
24+
25+
DiskDriveFullException exception =
26+
assertThrows(
27+
DiskDriveFullException.class,
28+
() -> {
29+
xbox.inputGame(3); // This should trigger the exception
30+
});
31+
32+
assertEquals("Disk drive is full. Cannot insert game.", exception.getMessage());
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.codedifferently.lesson16.dylans_xboxTest;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import com.codedifferently.lesson16.dylans_xbox.LoadGame;
7+
import com.codedifferently.lesson16.dylans_xbox.Xbox;
8+
import java.util.HashMap;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class LoadgameTest {
12+
@Test
13+
public void testLoadGame() throws Exception {
14+
// Create an instance of Xbox
15+
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
16+
17+
// Create an instance of LoadGame
18+
LoadGame loader =
19+
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv");
20+
21+
loader.loadGamesFromFile(xbox);
22+
// Check if the game was loaded correctly
23+
HashMap<Integer, String> games = xbox.getGames();
24+
assertTrue(games.containsKey(1)); // Check that the first game is loaded (ID 1)
25+
assertEquals("Call of Duty", games.get(1)); // Ensure the first game matches the CSV
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package com.codedifferently.lesson16.dylans_xboxTest;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
import static org.junit.jupiter.api.Assertions.fail;
6+
7+
import com.codedifferently.lesson16.dylans_xbox.DiskDriveFullException;
8+
import com.codedifferently.lesson16.dylans_xbox.LoadGame;
9+
import com.codedifferently.lesson16.dylans_xbox.Xbox;
10+
import java.io.ByteArrayOutputStream;
11+
import java.io.PrintStream;
12+
import java.util.HashMap;
13+
import org.junit.jupiter.api.Test; // Ensure LoadGame is imported
14+
15+
public class XboxTest {
16+
17+
@Test
18+
public void testAddGame() {
19+
LoadGame loader =
20+
new LoadGame(
21+
"src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv"); // Ensure
22+
// LoadGame
23+
24+
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); // Create an instance of Xbox
25+
try {
26+
loader.loadGamesFromFile(xbox);
27+
} catch (Exception e) {
28+
e.printStackTrace();
29+
fail("Exception occurred while loading games: " + e.getMessage());
30+
}
31+
32+
HashMap<Integer, String> games = xbox.getGames();
33+
assertTrue(games.containsKey(1)); // Check that the first game is loaded (ID 1)
34+
assertEquals("Call of Duty", games.get(1)); // Ensure the first game matches the CSV
35+
}
36+
37+
@Test
38+
public void testAddGameIfFull() {
39+
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); // false means not full yet
40+
LoadGame loader =
41+
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv");
42+
43+
try {
44+
loader.loadGamesFromFile(xbox);
45+
46+
// Insert two games (disk limit)
47+
xbox.inputGame(1);
48+
xbox.inputGame(2);
49+
50+
// This third insert should throw an exception
51+
xbox.inputGame(3);
52+
fail("Expected DiskDriveFullException to be thrown.");
53+
} catch (DiskDriveFullException e) {
54+
assertEquals("Disk drive is full. Cannot insert game.", e.getMessage());
55+
} catch (Exception e) {
56+
e.printStackTrace();
57+
fail("Unexpected exception: " + e.getMessage());
58+
}
59+
}
60+
61+
@Test
62+
public void testXboxModelEnumValues() {
63+
Xbox.XboxModel[] models = Xbox.XboxModel.values();
64+
assertEquals(6, models.length);
65+
assertEquals(Xbox.XboxModel.XBOX360, models[0]);
66+
assertEquals(Xbox.XboxModel.XBOXSERIESX, models[5]);
67+
}
68+
69+
@Test
70+
public void testPrintAllGames() throws Exception {
71+
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
72+
LoadGame loader =
73+
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv");
74+
75+
loader.loadGamesFromFile(xbox); // Load games into library
76+
77+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
78+
PrintStream originalOut = System.out;
79+
System.setOut(new PrintStream(outputStream));
80+
81+
xbox.printAllGames();
82+
83+
System.setOut(originalOut);
84+
85+
String expectedOutput =
86+
"""
87+
Game ID: 1, Game Name: Call of Duty
88+
Game ID: 2, Game Name: Elden Ring
89+
Game ID: 3, Game Name: Minecraft
90+
Game ID: 4, Game Name: Monster Hunter
91+
Game ID: 5, Game Name: Fortnite
92+
Game ID: 6, Game Name: Marvel Rivals
93+
Game ID: 7, Game Name: Tetris
94+
Game ID: 8, Game Name: Madden NFL
95+
Game ID: 9, Game Name: Terraria
96+
Game ID: 10, Game Name: Baldur's Gate 3
97+
""";
98+
assertEquals(expectedOutput.trim(), outputStream.toString().trim());
99+
}
100+
101+
@Test
102+
public void testEjectGame() {
103+
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
104+
LoadGame loader =
105+
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv");
106+
107+
try {
108+
loader.loadGamesFromFile(xbox); // Load games into library
109+
xbox.inputGame(1); // Insert game with ID 1
110+
assertEquals(1, xbox.getInsertedGamesSize(), "Game should be inserted.");
111+
112+
xbox.ejectGame(1); // Eject by name (you may use ID instead if implemented that way)
113+
assertEquals(0, xbox.getInsertedGamesSize(), "The game should be ejected.");
114+
} catch (Exception e) {
115+
e.printStackTrace();
116+
fail("Exception occurred: " + e.getMessage());
117+
}
118+
}
119+
120+
@Test
121+
public void testGetGames() {
122+
123+
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
124+
LoadGame loader =
125+
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv");
126+
127+
try {
128+
loader.loadGamesFromFile(xbox);
129+
} catch (Exception e) {
130+
e.printStackTrace();
131+
fail("Exception occurred while loading games: " + e.getMessage());
132+
}
133+
134+
HashMap<Integer, String> games = xbox.getGames();
135+
136+
assertEquals(10, games.size(), "There should be 10 games loaded.");
137+
}
138+
139+
@Test
140+
public void testGetPrice() {
141+
Xbox xbox = new Xbox("XBOX360", 400, "White", true, false);
142+
int price = xbox.getPrice();
143+
assertEquals(400, price, "The price should be 400.");
144+
}
145+
146+
@Test
147+
public void testGetColor() {
148+
Xbox xbox = new Xbox("XBOX360", 400, "White", true, false);
149+
String color = xbox.getColor();
150+
assertEquals("White", color, "The color should be White.");
151+
}
152+
153+
@Test
154+
public void testDiskDrive() {
155+
Xbox xbox = new Xbox("XBOX360", 400, "White", true, false);
156+
boolean diskDrive = xbox.DiskDrive();
157+
assertTrue(diskDrive, "The disk drive should be present.");
158+
}
159+
}

0 commit comments

Comments
 (0)