-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManager.java
More file actions
73 lines (68 loc) · 2.06 KB
/
Manager.java
File metadata and controls
73 lines (68 loc) · 2.06 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
import java.util.Scanner;
import java.util.InputMismatchException;
import javax.swing.JOptionPane;
/**
*this class implements the state and behaviour of a manager that will
*define a queue and a stack. this class also display and handle a
*simple menu to perform operations on the queue and the stack.
*this class holds the main method to start and load the application.
*/
public class Manager{
static Stack<Integer> st = new Stack<>();
static Queue<Integer> qu = new Queue<>();
final String[] menuMsgs={"Enqueue", "Dequeue", "Display Queue",
"Push", "Pop", "Display Stack", "Exit"};
//An array that holds the possible handlers from the menu
static private IntQueueStackHandler[] handlers=
{ new EnqueuePushHandler(qu),
new DequeuePopHandler(qu),
new DisplayHandler(qu),
new EnqueuePushHandler(st),
new DequeuePopHandler(st),
new DisplayHandler(st),
new ExitHandler()
};
/**
*Displays and handles the user choices interactively
*/
private void displayMenu(){
System.out.println("");
for (int i=0;i<menuMsgs.length;i++){
System.out.println((i+1)+". "+menuMsgs[i]);
}
System.out.print("please enter your choice: ");
}
/**
*the static main method, defines and initializes a new manager object
*/
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Manager mngr = new Manager();
int choice=1,num=0;
while(choice!=7){
mngr.displayMenu();
boolean i=false;
while(!i){
try{
choice=input.nextInt();
i=true;
}
catch(InputMismatchException e){ //not an integer
input.nextLine();
System.out.print("Try an integer between 1-7 from the menu: ");
i=false;
}
try{
handlers[choice-1].processRequest();
}
catch (ArrayIndexOutOfBoundsException e){ //array out of bounds
if(choice<1 || choice>7){
i=true;
JOptionPane dialog = new JOptionPane();
dialog.showMessageDialog(null, choice + " is an invalid choice! Please try again");
}
}
}
}
}
}