-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRetroGames.java
More file actions
87 lines (74 loc) · 3.26 KB
/
RetroGames.java
File metadata and controls
87 lines (74 loc) · 3.26 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
/**
* @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.
/**
* RetroGames class holds the main method and the runMenu method.
*/
public class RetroGames {
/**
* main method in which the userinput from runMenu is placed in a switch in a while loop to handle input from menu choices, includes an exit message and an error message as well.
* @exception e for try Scanner, and a catch of exception.
*/
public static void main(String[] args) {
GameInventory gameInventory = new GameInventory();
String exitMessage = "Program has been quit. Over and out.";
while (true) {
int numInput = runMenu();
switch (numInput) {
case 1:
gameInventory.addItem();
break;
case 2:
gameInventory.readItem();
break;
case 3:
gameInventory.updateItem();
break;
case 4:
gameInventory.deleteItem();
break;
case 5:
System.out.println(exitMessage);
System.exit(0);
break;
default:
System.out.println("Oops! Something went wrong. Program exiting.");
System.exit(0);
}
}
}
/**
* runMenu method that creates a scanner object to read keyboard input, and gets the user's menu choice as input.
* @return userMenuInput which is the input given by the user when doing a choice in the program menu. Input validation.
* @exception e for try Scanner, and a catch of exception.
*/
private static int runMenu() {
Scanner keyboard = new Scanner(System.in);
int userMenuInput;
System.out.println("\n1. Add an item to your Retro Games Inventory"); // a menu option to add new instances of the class GameInventory to the program
System.out.println("2. Read about an item in your Retro Games Inventory"); // a menu option for selecting an item and show all information in a clear way.
System.out.println("3. Update an item in your Retro Games Inventory"); // update the fields of an item from a menu option, except for the name, after the item has been added to the program.
System.out.println("4. Delete an item in your Retro Games Inventory"); // delete a selected Game Inventory from the program by a menu option.
System.out.println("5. Ouit Retro Games Inventory program"); // menu option to exit the program
String promtUserMenuInput = "Choose an option from the menu by enter a number: ";
String invalidUserMenuInput = "That's not a choise in the menu. " + promtUserMenuInput;
try {
System.out.print(promtUserMenuInput);
userMenuInput = keyboard.nextInt();
while (userMenuInput != 1 && userMenuInput != 2 && userMenuInput != 3 && userMenuInput != 4 && userMenuInput != 5) {
System.out.print(invalidUserMenuInput);
userMenuInput = keyboard.nextInt();
}
return userMenuInput;
}
catch(Exception e) {
System.out.println(e);
return -1;
}
}
}