Skip to content

Commit f52c9c8

Browse files
committed
Chore: enhance Xbox class with cleaner comments and organization
1 parent 7adbb8d commit f52c9c8

File tree

2 files changed

+29
-48
lines changed
  • lesson_16/objects/objects_app/src

2 files changed

+29
-48
lines changed

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

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,16 @@
22

33
import java.util.HashMap;
44

5-
/*
6-
Create a sub-folder in the main app folder with a unique name for your work.
7-
Design at least one custom class that represents a real-world object. //Object: Xbox HashMap
8-
You must also incorporate an enum type as well.
9-
Genre
10-
The class must have at least one constructor.
11-
I have this
12-
The class must have at least 3 member functions.
13-
I need this to be added maybe I can do addGame, removeGame, and getGame
14-
One of your functions must make use of a conditional expression.
15-
I can do this with the addGame method by implementing a check if the xbox has a disk drive && if the disk drive is empty
16-
17-
One of your functions must make use of your collection member variable.
18-
I can do this with the getGame method by calling the games inside of the hashmap
19-
20-
One of your functions must make use of a loop.
21-
I can do this with the removeGame method by using a for loop to iterate through the games and remove the game that matches the id passed in.
22-
You must use at least one custom exception.
23-
24-
I can do this with the addGame method by creating a custom exception that checks if the disk drive is empty and throws an exception if it is not.
25-
Create a matching subfolder in the test folder and a test file. Your test must include at least 5 test methods.
26-
Tests:
27-
1. testAddGame
28-
2. testRemoveGame
29-
3. testGetGame
30-
4. testGetAllGames
31-
5. testGetDiskDrive
32-
*/
335
public class Xbox {
346
private HashMap<Integer, String> games;
35-
private XboxModel model; // Use the enum type here
7+
// Declares the model of the Xbox by using the enum XboxModel
8+
private XboxModel model;
369
private String color;
3710
private int price;
38-
private static final int MAX_GAMES = 10;
39-
private boolean diskDrive; // Declares if there is a disk drive on the Xbox
40-
private boolean diskDriveFull =
41-
true; // If there is a disk drive, this will be t/f based on if there is a disk
42-
43-
// inside the xbox
11+
// 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;
4415

4516
// Defines a fixed set of constants for GameGenre
4617
public enum XboxModel {
@@ -52,6 +23,7 @@ public enum XboxModel {
5223
XBOXSERIESX
5324
}
5425

26+
// Constructor for the Xbox class
5527
public Xbox(String model, int price, String color, boolean diskDrive, boolean diskDriveFull) {
5628
this.model = XboxModel.valueOf(model.toUpperCase());
5729
this.price = price;
@@ -61,6 +33,7 @@ public Xbox(String model, int price, String color, boolean diskDrive, boolean di
6133
this.games = new HashMap<>();
6234
}
6335

36+
// Getters for the Xbox class
6437
public XboxModel getModel() {
6538
return model;
6639
}
@@ -85,19 +58,27 @@ public boolean DiskDriveFull() {
8558
return diskDriveFull;
8659
}
8760

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 turnign it to true.
8864
public void inputGame(int id, String name) throws Exception {
8965

66+
// These are my custom exceptions that will be thrown if the disk drive is empty or if the disk
67+
// drive is full.
9068
if (!diskDrive) {
9169
throw new Exception("This Xbox does not have a disk drive. Cannot insert game.");
9270
}
9371
if (diskDriveFull) {
94-
throw new Exception("Disk drive is full. Cannot insert game.");
72+
throw new DiskDriveFullException("Disk drive is full. Cannot insert game.");
9573
}
74+
9675
games.put(id, name);
9776
diskDriveFull = true;
9877
System.out.println("Game with ID: " + id + " was added to the disk drive.");
9978
}
10079

80+
// Method that will eject a game from the disk drive
81+
// it will check if the game is in the drive and if it is, it will turn the drive to false.
10182
public void ejectGame(int id) {
10283
if (games.containsKey(id)) {
10384
games.remove(id);
@@ -108,12 +89,15 @@ public void ejectGame(int id) {
10889
}
10990
}
11091

92+
// This method will print all the games in the HashMap
93+
// By running a for loop that will iterate through the games
11194
public void printAllGames() {
11295
for (Integer id : games.keySet()) {
11396
System.out.println("Game ID: " + id + ", Game Name: " + games.get(id));
11497
}
11598
}
11699

100+
// This method will remove a game from the HashMap
117101
public void setDiskDriveFull(boolean b) {
118102
this.diskDriveFull = b;
119103
}

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package XboxTest;
22

3-
import java.io.ByteArrayOutputStream;
4-
import java.io.PrintStream;
5-
import java.util.HashMap;
6-
73
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

127
import com.codedifferently.lesson16.dylans_xbox.LoadGame;
138
import com.codedifferently.lesson16.dylans_xbox.Xbox;
9+
import java.io.ByteArrayOutputStream;
10+
import java.io.PrintStream;
11+
import java.util.HashMap;
12+
import org.junit.jupiter.api.Test;
1413

1514
public class XboxTest {
1615

@@ -79,13 +78,10 @@ public void testPrintAllGames() {
7978
PrintStream originalOut = System.out;
8079
System.setOut(new PrintStream(outputStream));
8180

82-
// Act: Calls the printAllGames method
8381
xbox.printAllGames();
8482

85-
// Restore the original console output
8683
System.setOut(originalOut);
8784

88-
// Assert: Verify the captured output
8985
String expectedOutput =
9086
"""
9187
Game ID: 1, Game Name: Call of Duty
@@ -119,25 +115,26 @@ public void testEjectGame() {
119115
xbox.ejectGame(1);
120116

121117
// 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.");
118+
assertTrue(
119+
!xbox.getGames().containsKey(1), "Game with ID 1 should be removed from the games list.");
123120
}
124121

125-
@Test
122+
@Test
126123
public void testGetGames() {
124+
127125
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
128126
LoadGame loader =
129127
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv");
128+
130129
try {
131130
loader.loadGamesFromFile(xbox);
132131
} catch (Exception e) {
133132
e.printStackTrace();
134133
fail("Exception occurred while loading games: " + e.getMessage());
135134
}
136135

137-
// Act: Get the games
138136
HashMap<Integer, String> games = xbox.getGames();
139137

140-
// Assert: Verify the games are loaded correctly
141138
assertEquals(10, games.size(), "There should be 10 games loaded.");
142139
}
143140
}

0 commit comments

Comments
 (0)