Skip to content

Commit 27b9c6a

Browse files
committed
feat: refactor LoadGame and Xbox classes to improve game loading and insert disk logic; add unit tests for DiskDriveFullException and LoadGame functionality
1 parent 65a12e4 commit 27b9c6a

File tree

5 files changed

+74
-63
lines changed

5 files changed

+74
-63
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ public void loadGamesFromFile(Xbox xbox) throws Exception {
2121

2222
int id = Integer.parseInt(gameDetails[0].trim());
2323
String name = gameDetails[1].trim();
24-
xbox.inputGame(id, name);
25-
xbox.setDiskDriveFull(false); // Set diskDriveFull to true after adding a game
24+
xbox.getGames().put(id, name);
2625
}
2726
}
2827
} catch (IOException e) {

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package com.codedifferently.lesson16.dylans_xbox;
22

3+
import java.util.ArrayList;
34
import java.util.HashMap;
45

56
public class Xbox {
7+
private ArrayList<String> insertedGames = new ArrayList<>();
68
private HashMap<Integer, String> games;
79
// Declares the model of the Xbox by using the enum XboxModel
810
private XboxModel model;
911
private String color;
1012
private int price;
1113
// Declares if there is a disk drive on the Xbox
12-
private boolean diskDrive;
13-
// If there is a disk drive, this will be t/f based on if there is a disk inside of the xbox
14-
private boolean diskDriveFull = true;
14+
private boolean diskDrive = false;
1515

1616
// Defines a fixed set of constants for GameGenre
1717
public enum XboxModel {
@@ -29,10 +29,14 @@ public Xbox(String model, int price, String color, boolean diskDrive, boolean di
2929
this.price = price;
3030
this.color = color;
3131
this.diskDrive = diskDrive;
32-
this.diskDriveFull = diskDriveFull;
32+
this.insertedGames = new ArrayList<>();
3333
this.games = new HashMap<>();
3434
}
3535

36+
public int getInsertedGamesSize() {
37+
return insertedGames.size();
38+
}
39+
3640
// Getters for the Xbox class
3741
public XboxModel getModel() {
3842
return model;
@@ -54,36 +58,37 @@ public boolean DiskDrive() {
5458
return diskDrive;
5559
}
5660

57-
public boolean DiskDriveFull() {
58-
return diskDriveFull;
59-
}
60-
6161
// Method that will add a game to the disk drive
6262
// it will check if the disk drive is empty and if it is, it will add the game to the disk drive
63-
// by turnign it to true.
64-
public void inputGame(int id, String name) throws Exception {
65-
66-
// These are my custom exceptions that will be thrown if the disk drive is empty or if the disk
67-
// drive is full.
63+
// by turning it to true.
64+
public void inputGame(int id) throws Exception {
6865
if (!diskDrive) {
6966
throw new Exception("This Xbox does not have a disk drive. Cannot insert game.");
7067
}
71-
if (diskDriveFull) {
68+
if (insertedGames.size() >= 2) {
7269
throw new DiskDriveFullException("Disk drive is full. Cannot insert game.");
7370
}
7471

75-
games.put(id, name);
76-
diskDriveFull = true;
77-
System.out.println("Game with ID: " + id + " was added to the disk drive.");
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.");
7884
}
7985

8086
// Method that will eject a game from the disk drive
8187
// it will check if the game is in the drive and if it is, it will turn the drive to false.
8288
public void ejectGame(int id) {
83-
if (games.containsKey(id)) {
84-
games.remove(id);
85-
diskDrive = false;
86-
System.out.println("Game with ID: " + id + " was removed from the disk drive.");
89+
if (insertedGames.size() >= 1) {
90+
insertedGames.removeAll(insertedGames);
91+
System.out.println("Game with ID: " + id + " was ejected from the disk drive.");
8792
} else {
8893
System.out.println("Game with ID: " + id + " not found in the disk drive.");
8994
}
@@ -96,9 +101,4 @@ public void printAllGames() {
96101
System.out.println("Game ID: " + id + ", Game Name: " + games.get(id));
97102
}
98103
}
99-
100-
// This method will remove a game from the HashMap
101-
public void setDiskDriveFull(boolean b) {
102-
this.diskDriveFull = b;
103-
}
104104
}

lesson_16/objects/objects_app/src/test/xboxtest/DiskdrivefullTest.java renamed to lesson_16/objects/objects_app/src/test/java/xboxtest/DiskdrivefullTest.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1-
package XboxTest;
1+
package xboxtest;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.fail;
55

66
import com.codedifferently.lesson16.dylans_xbox.DiskDriveFullException;
7+
import com.codedifferently.lesson16.dylans_xbox.LoadGame;
78
import com.codedifferently.lesson16.dylans_xbox.Xbox;
89
import org.junit.jupiter.api.Test;
910

1011
public class DiskdrivefullTest {
1112
@Test
1213
public void testDiskDriveFullException() throws Exception {
13-
// Create an instance of Xbox with diskDriveFull set to true
14-
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, true);
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);
1524

1625
try {
17-
xbox.inputGame(1, "Call of Duty");
26+
xbox.inputGame(3); // This should trigger the exception
1827
fail("Expected DiskDriveFullException to be thrown.");
1928
} catch (DiskDriveFullException e) {
2029
assertEquals("Disk drive is full. Cannot insert game.", e.getMessage());

lesson_16/objects/objects_app/src/test/xboxtest/LoadgameTest.java renamed to lesson_16/objects/objects_app/src/test/java/xboxtest/LoadgameTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package XboxTest;
1+
package xboxtest;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertTrue;

lesson_16/objects/objects_app/src/test/xboxtest/XboxTest.java renamed to lesson_16/objects/objects_app/src/test/java/xboxtest/XboxTest.java

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
package XboxTest;
1+
package xboxtest;
22

3-
import java.io.ByteArrayOutputStream;
4-
import java.io.PrintStream;
5-
import java.util.HashMap; // Ensure LoadGame is imported
6-
7-
import static org.junit.jupiter.api.Assertions.assertEquals;
3+
import static org.junit.jupiter.api.Assertions.assertEquals; // Ensure LoadGame is imported
84
import static org.junit.jupiter.api.Assertions.assertTrue;
95
import static org.junit.jupiter.api.Assertions.fail;
10-
import org.junit.jupiter.api.Test;
116

7+
import com.codedifferently.lesson16.dylans_xbox.DiskDriveFullException;
128
import com.codedifferently.lesson16.dylans_xbox.LoadGame;
139
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;
1414

1515
public class XboxTest {
1616

@@ -36,34 +36,36 @@ public void testAddGame() {
3636

3737
@Test
3838
public void testAddGameIfFull() {
39-
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, true); // Set diskDriveFull to true
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+
4043
try {
41-
xbox.inputGame(1, "Call of Duty");
42-
fail("Expected an exception to be thrown when adding a game to a full disk drive.");
43-
} catch (Exception e) {
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) {
4454
assertEquals("Disk drive is full. Cannot insert game.", e.getMessage());
55+
} catch (Exception e) {
56+
e.printStackTrace();
57+
fail("Unexpected exception: " + e.getMessage());
4558
}
4659
}
4760

4861
@Test
4962
public void testXboxModelEnumValues() {
5063
Xbox.XboxModel[] models = Xbox.XboxModel.values();
5164
assertEquals(6, models.length);
52-
assertEquals(
53-
Xbox.XboxModel.XBOX360, models[0]);
65+
assertEquals(Xbox.XboxModel.XBOX360, models[0]);
5466
assertEquals(Xbox.XboxModel.XBOXSERIESX, models[5]);
5567
}
5668

57-
@Test
58-
public void testDiskDrive() {
59-
Xbox xbox =
60-
new Xbox(
61-
"XBOXONE", 400, "White", true,
62-
false);
63-
assertTrue(
64-
xbox.DiskDrive(), "Disk drive should be empty");
65-
}
66-
6769
@Test
6870
public void testPrintAllGames() {
6971
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
@@ -105,17 +107,18 @@ public void testEjectGame() {
105107
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
106108
LoadGame loader =
107109
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv");
110+
108111
try {
109-
loader.loadGamesFromFile(xbox);
112+
loader.loadGamesFromFile(xbox); // Load games into library
113+
xbox.inputGame(1); // Insert game with ID 1
114+
assertEquals(1, xbox.getInsertedGamesSize(), "Game should be inserted.");
115+
116+
xbox.ejectGame(1); // Eject by name (you may use ID instead if implemented that way)
117+
assertEquals(0, xbox.getInsertedGamesSize(), "The game should be ejected.");
110118
} catch (Exception e) {
111119
e.printStackTrace();
112-
fail("Exception occurred while loading games: " + e.getMessage());
120+
fail("Exception occurred: " + e.getMessage());
113121
}
114-
115-
xbox.ejectGame(1);
116-
117-
assertTrue(
118-
!xbox.getGames().containsKey(1), "Game with ID 1 should be removed from the games list.");
119122
}
120123

121124
@Test

0 commit comments

Comments
 (0)