2
2
3
3
import java .util .HashMap ;
4
4
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
- */
33
5
public class Xbox {
34
6
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 ;
36
9
private String color ;
37
10
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 ;
44
15
45
16
// Defines a fixed set of constants for GameGenre
46
17
public enum XboxModel {
@@ -52,6 +23,7 @@ public enum XboxModel {
52
23
XBOXSERIESX
53
24
}
54
25
26
+ // Constructor for the Xbox class
55
27
public Xbox (String model , int price , String color , boolean diskDrive , boolean diskDriveFull ) {
56
28
this .model = XboxModel .valueOf (model .toUpperCase ());
57
29
this .price = price ;
@@ -61,6 +33,7 @@ public Xbox(String model, int price, String color, boolean diskDrive, boolean di
61
33
this .games = new HashMap <>();
62
34
}
63
35
36
+ // Getters for the Xbox class
64
37
public XboxModel getModel () {
65
38
return model ;
66
39
}
@@ -85,19 +58,27 @@ public boolean DiskDriveFull() {
85
58
return diskDriveFull ;
86
59
}
87
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 turnign it to true.
88
64
public void inputGame (int id , String name ) throws Exception {
89
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.
90
68
if (!diskDrive ) {
91
69
throw new Exception ("This Xbox does not have a disk drive. Cannot insert game." );
92
70
}
93
71
if (diskDriveFull ) {
94
- throw new Exception ("Disk drive is full. Cannot insert game." );
72
+ throw new DiskDriveFullException ("Disk drive is full. Cannot insert game." );
95
73
}
74
+
96
75
games .put (id , name );
97
76
diskDriveFull = true ;
98
77
System .out .println ("Game with ID: " + id + " was added to the disk drive." );
99
78
}
100
79
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.
101
82
public void ejectGame (int id ) {
102
83
if (games .containsKey (id )) {
103
84
games .remove (id );
@@ -108,12 +89,15 @@ public void ejectGame(int id) {
108
89
}
109
90
}
110
91
92
+ // This method will print all the games in the HashMap
93
+ // By running a for loop that will iterate through the games
111
94
public void printAllGames () {
112
95
for (Integer id : games .keySet ()) {
113
96
System .out .println ("Game ID: " + id + ", Game Name: " + games .get (id ));
114
97
}
115
98
}
116
99
100
+ // This method will remove a game from the HashMap
117
101
public void setDiskDriveFull (boolean b ) {
118
102
this .diskDriveFull = b ;
119
103
}
0 commit comments