Skip to content

Commit 0d27828

Browse files
committed
feat: implement LoadGame class for loading games from a file and update Xbox class for disk drive management
1 parent ac4b84b commit 0d27828

File tree

3 files changed

+93
-31
lines changed

3 files changed

+93
-31
lines changed
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import java.io.FileReader;
55
import java.io.IOException;
66

7-
public class LoadGames {
7+
public class LoadGame {
88
private String filePath;
99

10-
public LoadGames(String filePath) {
10+
public LoadGame(String filePath) {
1111
this.filePath = filePath;
1212
}
1313

@@ -17,10 +17,13 @@ public void loadGamesFromFile(Xbox xbox) throws Exception {
1717
br.readLine(); // Skip the header line
1818
while ((line = br.readLine()) != null) {
1919
String[] gameDetails = line.split(",");
20-
int id = Integer.parseInt(gameDetails[0].trim());
21-
String name = gameDetails[1].trim();
20+
if (gameDetails.length >= 2) {
2221

23-
xbox.inputGame(id, name);
22+
int id = Integer.parseInt(gameDetails[0].trim());
23+
String name = gameDetails[1].trim();
24+
xbox.inputGame(id, name);
25+
xbox.setDiskDriveFull(false); // Set diskDriveFull to true after adding a game
26+
}
2427
}
2528
} catch (IOException e) {
2629
throw new Exception("Error reading the games file: " + e.getMessage());

lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/dylans_xbox/Xbox.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public class Xbox {
3737
private int price;
3838
private static final int MAX_GAMES = 10;
3939
private boolean diskDrive; // Declares if there is a disk drive on the Xbox
40-
private boolean
41-
diskDriveFull; // If there is a disk drive, this will be t/f based on if there is a disk
40+
private boolean diskDriveFull =
41+
true; // If there is a disk drive, this will be t/f based on if there is a disk
4242

4343
// inside the xbox
4444

@@ -52,11 +52,12 @@ public enum XboxModel {
5252
XBOXSERIESX
5353
}
5454

55-
public Xbox(String model, int price, String color, boolean diskDrive) {
55+
public Xbox(String model, int price, String color, boolean diskDrive, boolean diskDriveFull) {
5656
this.model = XboxModel.valueOf(model.toUpperCase());
5757
this.price = price;
5858
this.color = color;
5959
this.diskDrive = diskDrive;
60+
this.diskDriveFull = diskDriveFull;
6061
this.games = new HashMap<>();
6162
}
6263

@@ -112,4 +113,8 @@ public void printAllGames() {
112113
System.out.println("Game ID: " + id + ", Game Name: " + games.get(id));
113114
}
114115
}
116+
117+
public void setDiskDriveFull(boolean b) {
118+
this.diskDriveFull = b;
119+
}
115120
}

lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java

Lines changed: 77 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,44 @@
44
import java.io.PrintStream;
55
import java.util.HashMap;
66

7-
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertEquals; // Ensure LoadGame is imported
88
import static org.junit.jupiter.api.Assertions.assertTrue;
99
import static org.junit.jupiter.api.Assertions.fail;
1010
import org.junit.jupiter.api.Test;
1111

12+
import com.codedifferently.lesson16.dylans_xbox.LoadGame;
1213
import com.codedifferently.lesson16.dylans_xbox.Xbox;
1314

1415
public class XboxTest {
1516

1617
@Test
1718
public void testAddGame() {
18-
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", false);
19-
loadGames loader =
20-
new loadGames("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv");
19+
LoadGame loader =
20+
new LoadGame(
21+
"src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv"); // Ensure LoadGame
22+
23+
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false); // Create an instance of Xbox
2124
try {
2225
loader.loadGamesFromFile(xbox);
2326
} catch (Exception e) {
2427
e.printStackTrace();
2528
fail("Exception occurred while loading games: " + e.getMessage());
2629
}
2730

28-
HashMap<Integer, String> games =
29-
xbox.getGames(); // Fixed: Changed from `loadGames()` to `getGames()`
30-
assertTrue(games.containsKey(10));
31-
assertEquals(
32-
"Baldur's Gate 3", games.get(10)); // Fixed: Ensured the game name matches the CSV file
31+
HashMap<Integer, String> games = xbox.getGames();
32+
assertTrue(games.containsKey(1)); // Check that the first game is loaded (ID 1)
33+
assertEquals("Call of Duty", games.get(1)); // Ensure the first game matches the CSV
34+
}
35+
36+
@Test
37+
public void testAddGameIfFull() {
38+
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, true); // Set diskDriveFull to true
39+
try {
40+
xbox.inputGame(1, "Call of Duty");
41+
fail("Expected an exception to be thrown when adding a game to a full disk drive.");
42+
} catch (Exception e) {
43+
assertEquals("Disk drive is full. Cannot insert game.", e.getMessage());
44+
}
3345
}
3446

3547
@Test
@@ -45,16 +57,17 @@ public void testXboxModelEnumValues() {
4557
public void testDiskDrive() {
4658
Xbox xbox =
4759
new Xbox(
48-
"XBOXONE", 400, "White", true); // Fixed: Set diskDrive to `false` to match the test
60+
"XBOXONE", 400, "White", true,
61+
false); // Fixed: Set diskDrive to `false` to match the test
4962
assertTrue(
5063
xbox.DiskDrive(), "Disk drive should be empty"); // Fixed: Corrected the assertion syntax
5164
}
5265

5366
@Test
5467
public void testPrintAllGames() {
55-
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true);
56-
loadGames loader =
57-
new loadGames("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv");
68+
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
69+
LoadGame loader =
70+
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv");
5871
try {
5972
loader.loadGamesFromFile(xbox);
6073
} catch (Exception e) {
@@ -74,16 +87,57 @@ public void testPrintAllGames() {
7487

7588
// Assert: Verify the captured output
7689
String expectedOutput =
77-
"Game ID: 1, Game Name: Call of Duty\n"
78-
+ "Game ID: 2, Game Name: Elden Ring\n"
79-
+ "Game ID: 3, Game Name: Minecraft\n"
80-
+ "Game ID: 4, Game Name: Monster Hunter\n"
81-
+ "Game ID: 5, Game Name: Fortnite\n"
82-
+ "Game ID: 6, Game Name: Marvel Rivals\n"
83-
+ "Game ID: 7, Game Name: Tetris\n"
84-
+ "Game ID: 8, Game Name: Madden NFL\n"
85-
+ "Game ID: 9, Game Name: Terraria\n"
86-
+ "Game ID: 10, Game Name: Baldur's Gate 3\n";
90+
"""
91+
Game ID: 1, Game Name: Call of Duty
92+
Game ID: 2, Game Name: Elden Ring
93+
Game ID: 3, Game Name: Minecraft
94+
Game ID: 4, Game Name: Monster Hunter
95+
Game ID: 5, Game Name: Fortnite
96+
Game ID: 6, Game Name: Marvel Rivals
97+
Game ID: 7, Game Name: Tetris
98+
Game ID: 8, Game Name: Madden NFL
99+
Game ID: 9, Game Name: Terraria
100+
Game ID: 10, Game Name: Baldur's Gate 3
101+
""";
87102
assertEquals(expectedOutput.trim(), outputStream.toString().trim());
88103
}
104+
105+
@Test
106+
public void testEjectGame() {
107+
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
108+
LoadGame loader =
109+
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv");
110+
try {
111+
loader.loadGamesFromFile(xbox);
112+
} catch (Exception e) {
113+
e.printStackTrace();
114+
fail("Exception occurred while loading games: " + e.getMessage());
115+
}
116+
117+
// Act: Eject a game
118+
// Act: Eject a game
119+
xbox.ejectGame(1);
120+
121+
// Assert: Verify the game was ejected (if needed, check the state of the Xbox object)
122+
assertTrue(!xbox.getGames().containsKey(1), "Game with ID 1 should be removed from the games list.");
123+
}
124+
125+
@Test
126+
public void testGetGames() {
127+
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
128+
LoadGame loader =
129+
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv");
130+
try {
131+
loader.loadGamesFromFile(xbox);
132+
} catch (Exception e) {
133+
e.printStackTrace();
134+
fail("Exception occurred while loading games: " + e.getMessage());
135+
}
136+
137+
// Act: Get the games
138+
HashMap<Integer, String> games = xbox.getGames();
139+
140+
// Assert: Verify the games are loaded correctly
141+
assertEquals(10, games.size(), "There should be 10 games loaded.");
142+
}
89143
}

0 commit comments

Comments
 (0)