-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameInventory.java
More file actions
363 lines (323 loc) · 13.5 KB
/
GameInventory.java
File metadata and controls
363 lines (323 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* @author Edvin Hellsing
* @version 1.0
*/
package foo; // Package named 'foo'.
import java.util.Scanner; // Import of scanner class from the Java API.
import java.io.*; // Import all the classes of io package.
import java.util.HashMap; // Import of Hashmap for storing information.
import java.util.LinkedHashMap; // Import of LinkedHashmap for storing information.
import java.util.*; // Import everything from java util.
import java.util.concurrent.atomic.*; // Import functions from atomic in java util.
/**
* GameInventory class has the hashmap which holds all items and its parameters, the method for add, read, update and delete items. It has the 'askString', 'askInt' and 'deleteInt' aswell.
*/
public class GameInventory {
private LinkedHashMap<String, Item> gameInventory;
/**
* GameInventory constructor, new hashmap with string and item to hold items and its parameters.
*/
public GameInventory() {
this.gameInventory = new LinkedHashMap<String, Item>();
// Two items from start
/*Item item = new Item("JOWA", "king", 1993, Condition.MINT, true);
this.gameInventory.put("JOWA", item);
Item item1 = new Item("EDHE", "queen", 1893, Condition.POOR, false);
this.gameInventory.put("EDHE", item1);*/
}
/**
* addItem method to add items.
*/
public void addItem() {
System.out.println("So you want to add an item? Fill in the form below.");
String name = askString("name");
String desc = askString("description");
int year = askInt("year (between 1000-2021)", 1000, 2021); // Take a look at this since you can add a year with just to digits today
int intCond = askInt("condition (1 for New, 2 Mint, 3 for Good, 4 for Poor)", 1, 4);
int intCurrentlyLoaned = askInt("loaned status (0 for No, 1 for Yes", 0, 1);
Condition cond;
switch (intCond) {
case 1:
cond = Condition.NEW;
break;
case 2:
cond = Condition.MINT;
break;
case 3:
cond = Condition.GOOD;
break;
case 4:
cond = Condition.POOR;
break;
default:
cond = Condition.POOR;
}
Boolean currentlyLoaned;
if (intCurrentlyLoaned == 0) {
currentlyLoaned = false;
}
else {
currentlyLoaned = true;
}
System.out.println("The item '" + name + "' has been added.");
this.gameInventory.put(name, new Item(name, desc, year, cond, currentlyLoaned));
}
/**
* readItem method to read items.
*/
public void readItem() {
System.out.println("So you want to read about an item? Here's a list of all items that currently are saved in the database:");
// Atomic to show all items in the database
/* AtomicInteger i = new AtomicInteger(1);
this.gameInventory.forEach((key, item) -> {
int counter = i.getAndIncrement();
System.out.println(counter + ". " + key);
}); */
// For each to show all items in the database
int i = 1;
for (Map.Entry<String, Item> entry : this.gameInventory.entrySet()) {
System.out.println(i + ". " + entry.getKey());
i++;
}
int size = this.gameInventory.size();
if (size == 0) {
System.out.println("No items have been saved.");
return;
}
int number = askInt("number", 1, size);
String name = "";
int j = 1;
for (Map.Entry<String, Item> entry : this.gameInventory.entrySet()) {
if (j == number) {
name = entry.getKey();
break;
}
j++;
}
// Check if the entered name exsits in the hashmap, present information about it if it does.
if (gameInventory.containsKey(name)) {
Item item = this.gameInventory.get(name);
System.out.println("Information about '" + name + "' presented below:");
System.out.println("Item name: " + item.getName());
System.out.println("Item description: " + item.getDesc());
System.out.println("Item year: " + item.getYear());
System.out.println("Item condition: " + item.getCond());
System.out.println("Item currently loaned: " + item.getCurrentlyLoaned());
}
// If no information about the entered name, print this message.
else {
System.out.println("It seems like there's no information available about '" + name + "'. " + toMenuMessage);
}
}
/**
* updateItem method to update items.
*/
public void updateItem() {
System.out.println("So you want to update an item? When you're done, press S to save, or press Q to quit.");
// System.out.println("Updating item");
String name = askString("name");
// Check if the entered name exsits in the hashmap, present information about it if it does.
if (gameInventory.containsKey(name)) {
Item item = this.gameInventory.get(name);
String tempDesc = item.getDesc();
int tempYear = item.getYear();
Condition tempCond = item.getCond();
boolean tempLoaned = item.getCurrentlyLoaned();
boolean shouldLoop = true;
while (shouldLoop) {
System.out.println("Current information about '" + name + "' presented below:");
System.out.println("1. Item description: " + tempDesc);
System.out.println("2. Item year: " + tempYear);
System.out.println("3. Item condition: " + tempCond);
System.out.println("4. Item currently loaned: " + tempLoaned);
String userChoice = askString("number between 1-4 to choose which information you want to update (it's not possible to change the item's name)");
switch (userChoice) {
case "1":
tempDesc = askString("description");
break;
case "2":
tempYear = askInt("year", 1000, 2021);
break;
case "3":
int intCond = askInt("condition", 1, 4);
switch (intCond) {
case 1:
tempCond = Condition.NEW;
break;
case 2:
tempCond = Condition.MINT;
break;
case 3:
tempCond = Condition.GOOD;
break;
case 4:
tempCond = Condition.POOR;
break;
default:
tempCond = Condition.POOR;
}
break;
case "4":
int i = askInt("loaned (0 for No, 1 for Yes", 0, 1);
if (i == 0) {
tempLoaned = false;
}
else {
tempLoaned = true;
}
break;
case "S":
case "s":
case "Save":
case "save": {
item.setDesc(tempDesc);
item.setReleaseYear(tempYear);
item.setCond(tempCond);
item.setCurrentlyLoaned(tempLoaned);
shouldLoop = false;
System.out.println("The information has been saved.");
}
break;
case "Q":
case "q":
case "Quit":
case "quit": {
shouldLoop = false;
System.out.println("Update function has been quit. Any changes hasn't been saved.");
}
default:
shouldLoop = false;
}
}
System.out.println("Information about '" + name + "' presented below:");
System.out.println("Item description: " + item.getDesc());
System.out.println("Item year: " + item.getYear());
System.out.println("Item condition: " + item.getCond());
System.out.println("Item currently loaned: " + item.getCurrentlyLoaned());
System.out.println(toMenuMessage);
}
// If no information about the entered name, print this message.
else {
System.out.println("It seems like there's no information available about '" + name + "'." + toMenuMessage);
}
}
/**
* deleteItem method to delete items.
*/
public void deleteItem() {
System.out.println("So you want to delete about an item?");
// System.out.println("Deleting item");
String name = askString("name");
// Check if the entered name exsits in the hashmap, present information about it if it does.
if (gameInventory.containsKey(name)) {
Item item = this.gameInventory.get(name);
System.out.println("The item '" + name + "' contains the informtaion below:");
System.out.println("Item name: " + item.getName());
System.out.println("Item description: " + item.getDesc());
System.out.println("Item year: " + item.getYear());
System.out.println("Item condition: " + item.getCond());
System.out.println("Item currently loaned: " + item.getCurrentlyLoaned());
// Double check if the user wants to remaove the entered item.
int intDeleteItem = deleteInt("Do you like to delete '" + name + "' and all its information? Enter 0 for no, 1 for yes. (This action cannot be undone): ", 0, 1);
// If not, print message.
if (intDeleteItem == 0) {
System.out.println("The item '" + name + "' wasn't deleted from your Retro Games Inventory.");
}
// If else (yes), delete item from hashmap, i.e. inventory and print message.
else {
gameInventory.remove(name);
System.out.println("The item '" + name + "' has been deleted.");
}
}
// If no information about the entered name, print this message.
else {
System.out.println("It seems like there's no item named '" + name + "'." + toMenuMessage);
}
}
String toMenuMessage = "You'll be directed to the menu.";
/**
* askString method to ask quesitions with strings as answer.
* Create a Scanner object to read keyboard input regarding strings, and get the user's input. Input validation included.
* @param what which is used to make the program a bit more flexible since it's possible to customize the string a bit where it's used.
* @return str entered by user in upper cases (name, description, menu choice).
* @exception e for Scanner, and a catch of exception.
*/
private String askString(String what) {
Scanner keyboard = new Scanner(System.in);
String promtName = "Enter a " + what + " for the item: ";
String invalidName = "You haven't entered a " + what + ". " + promtName;
String str = "";
try {
System.out.print(promtName);
str = keyboard.nextLine();
while (str.length() < 1) {
System.out.print(invalidName);
str = keyboard.nextLine();
}
}
catch(Exception e) {
System.out.println(e);
System.exit(0); //Have something else than system.exit?
}
if (what.equals("name")) {
str = str.toUpperCase();
}
return str;
}
/**
* askInt method to ask quesitions with integers as answer.
* Create a Scanner object to read keyboard input regarding integers, and get the user's input. Input validation included.
* @param what which is defined where it's used.
* @param smallest to make the program a bit more flexible since it's possible to customize the values of the int 'smallest' where it's used.
* @param largest to make the program a bit more flexible since it's possible to customize the values of the int 'largest' where it's used.
* @return userInt entered by user (year, condition, loaned status).
* @exception e for Scanner, and a catch of exception.
*/
private int askInt(String what, int smallest, int largest) {
Scanner keyboard = new Scanner(System.in);
String prompt = "Enter a " + what + " for the item: ";
String invalid = "You haven't entered a valid " + what + ". " + prompt;
int userInt = 0;
try {
System.out.print(prompt);
userInt = keyboard.nextInt();
while (userInt < smallest || userInt > largest) {
System.out.print(invalid);
userInt = keyboard.nextInt();
}
}
catch(Exception e) {
System.out.println(e);
System.exit(0);
}
return userInt;
}
/**
* deleteInt method to ask validation quesitions with integers as answers before deleting items.
* Create a Scanner object to read keyboard input regarding delete of integers, and get the user's input. Input validation included.
* @param what which is defined where it's used.
* @param smallest which is defined where it's used.
* @param largest which is defined where it's used.
* @return userInt entered in deleteInt (validation question when about to delete an item).
* @exception e for Scanner, and a catch of exception.
*/
private int deleteInt(String what, int smallest, int largest) {
Scanner keyboard = new Scanner(System.in);
String prompt = what;
String invalid = "You haven't entered a valid choise, please do. " + prompt;
int userInt = 0;
try {
System.out.print(prompt);
userInt = keyboard.nextInt();
while (userInt < smallest || userInt > largest) {
System.out.print(invalid);
userInt = keyboard.nextInt();
}
}
catch(Exception e) {
System.out.println(e);
System.exit(0);
}
return userInt;
}
}