Skip to content

Commit 686e6e5

Browse files
committed
feat: implement Xbox class with game management and loading functionality
Todo: Add error handling and logging
1 parent faa4f80 commit 686e6e5

File tree

5 files changed

+219
-0
lines changed

5 files changed

+219
-0
lines changed

lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/Lesson16.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ public static void main(String[] args) {
1212
var application = new SpringApplication(Lesson16.class);
1313
application.run(args);
1414
}
15+
1516
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com.codedifferently.lesson16.dylans_xbox;
2+
import java.util.HashMap;
3+
4+
/*
5+
Create a sub-folder in the main app folder with a unique name for your work.
6+
Design at least one custom class that represents a real-world object. //Object: Xbox HashMap
7+
You must also incorporate an enum type as well.
8+
Genre
9+
The class must have at least one constructor.
10+
I have this
11+
The class must have at least 3 member functions.
12+
I need this to be added maybe I can do addGame, removeGame, and getGame
13+
One of your functions must make use of a conditional expression.
14+
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
15+
16+
One of your functions must make use of your collection member variable.
17+
I can do this with the getGame method by calling the games inside of the hashmap
18+
19+
One of your functions must make use of a loop.
20+
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.
21+
You must use at least one custom exception.
22+
23+
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.
24+
Create a matching subfolder in the test folder and a test file. Your test must include at least 5 test methods.
25+
Tests:
26+
1. testAddGame
27+
2. testRemoveGame
28+
3. testGetGame
29+
4. testGetAllGames
30+
5. testGetDiskDrive
31+
*/
32+
public class Xbox {
33+
private HashMap<Integer, String> games;
34+
private XboxModel model; //Use the enum type here
35+
private String color;
36+
private int price;
37+
private static final int MAX_GAMES = 10;
38+
private boolean diskDrive; //Declares if there is a disk drive on the Xbox
39+
private boolean diskDriveFull; //If there is a disk drive, this will be t/f based on if there is a disk inside the xbox
40+
//Creates a file path that will call to the games.csv file
41+
42+
//Defines a fixed set of constants for GameGenre
43+
public enum XboxModel {
44+
XBOX360,
45+
XBOXONE,
46+
XBOXONES,
47+
XBOXONEX,
48+
XBOXSERIESS,
49+
XBOXSERIESX
50+
}
51+
52+
public Xbox(String model, int price, String color, boolean diskDrive) {
53+
this.model = model;
54+
this.price = price;
55+
this.color = color;
56+
this.diskDrive = diskDrive;
57+
this.games = new HashMap<>();
58+
}
59+
60+
public XboxModel getModel() {
61+
return model;
62+
}
63+
64+
public int getPrice() {
65+
return price;
66+
}
67+
68+
public String getColor() {
69+
return color;
70+
}
71+
72+
public boolean DiskDrive() {
73+
return diskDrive;
74+
}
75+
76+
public boolean DiskDriveFull() {
77+
return DiskDriveFull();
78+
}
79+
80+
public void inputGame(int id, String name) throws Exception {
81+
82+
if(!diskDrive) {
83+
throw new Exception("This Xbox does not have a disk drive. Cannot insert game.");
84+
}
85+
if(diskDriveFull) {
86+
throw new Exception("Disk drive is full. Cannot insert game.");
87+
}
88+
games.put(id, name);
89+
diskDriveFull = true;
90+
System.out.println("Game with ID: " + id + " was added to the disk drive.");
91+
92+
}
93+
94+
public void ejectGame(int id) {
95+
if(games.containsKey(id)) {
96+
games.remove(id);
97+
diskDrive = false;
98+
System.out.println("Game with ID: " + id + " was removed from the disk drive.");
99+
} else {
100+
System.out.println("Game with ID: " + id + " not found in the disk drive.");
101+
}
102+
}
103+
104+
public void printAllGames() {
105+
for(Integer id : games.keySet()) {
106+
System.out.println("Game ID: " + id + ", Game Name: " + games.get(id));
107+
}
108+
}
109+
110+
111+
112+
}
113+
Lines changed: 11 additions & 0 deletions
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
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
public class loadGames extends Xbox {
8+
private String filePath;
9+
10+
public loadGames(String filePath) {
11+
this.filePath = filePath;
12+
}
13+
14+
public void loadGamesFromFile() {
15+
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
16+
String line;
17+
while ((line = br.readLine()) != null) {
18+
String[] gameDetails = line.split(",");
19+
int id = Integer.parseInt(gameDetails[0]);
20+
String name = gameDetails[4];
21+
addGame(id, name);
22+
}
23+
} catch (IOException e) {
24+
System.err.println("Error reading file: " + e.getMessage());
25+
}
26+
}
27+
28+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.codedifferently.lesson16.dylans_xbox;
2+
3+
import java.beans.Transient;
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.PrintStream;
6+
import java.util.HashMap;
7+
8+
import main.dylans_xbox.java.loadGames;
9+
10+
public class XboxTest {
11+
@Test
12+
public void testaddGame() throws GameException {
13+
Xbox xbox = new Xbox("Series X", 600, "Black", false);
14+
loadGames loader = new loadGames("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv");
15+
loader.loadGamesFromFile(xbox);
16+
17+
HashMap<Integer, String> games = xbox.getGames();
18+
assertTrue(games.containsKey(10));
19+
assertEguals("Baldur's Gate 3", games.get(10));
20+
}
21+
22+
@Test
23+
public void testXboxModelEnumValues() {
24+
Xbox.XboxModel[] models = Xbox.XboxModel.values();
25+
assertEquals(6, models.length);
26+
assertEquals(Xbox.XboxModel.XBOXONE, models[0]);
27+
assertEquals(Xbox.XboxModel.XBOXSERIESX, models[5]);
28+
}
29+
30+
@Test
31+
public void testDiskDrive() {
32+
Xbox xbox = new Xbox("XBOXONE", 400, "White", true);
33+
assertFalse("Disk drive should be empty", xbox.DiskDrive());
34+
}
35+
36+
@Test
37+
public void testPrintAllGames() {
38+
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true);
39+
loadGames loader = new loadGames("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv");
40+
loader.loadGamesFromFile(xbox);
41+
42+
43+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
44+
PrintStream originalOut = System.out;
45+
System.setOut(new PrintStream(outputStream));
46+
47+
//Calls the printAllGames method
48+
xbox.printAllGames();
49+
50+
System,setOut(originalOut);
51+
String expectedOutput = "Game ID: 1, Game Name: Call of Duty\n" +
52+
"Game ID: 2, Game Name: Elden Ring\n" +
53+
"Game ID: 3, Game Name: Minecraft\n" +
54+
"Game ID: 4, Game Name: Monster Hunter\n" +
55+
"Game ID: 5, Game Name: Fortnite\n" +
56+
"Game ID: 6, Game Name: Marvel Rivals\n" +
57+
"Game ID: 7, Game Name: Tetris\n" +
58+
"Game ID: 8, Game Name: Madden NFL\n" +
59+
"Game ID: 9, Game Name: Terraria\n" +
60+
"Game ID: 10, Game Name: Baldur's Gate 3\n";
61+
assertEquals(expectedOutput.trim(), outputStream.toString().trim());
62+
63+
64+
65+
}
66+
}

0 commit comments

Comments
 (0)