-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnqueuePushHandler.java
More file actions
56 lines (53 loc) · 1.69 KB
/
EnqueuePushHandler.java
File metadata and controls
56 lines (53 loc) · 1.69 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
import javax.swing.JOptionPane;
/**
*This class defines two overloaded constructors, in order to match the overloaded constructors
*in 'GeneralHandler' class. And responsible to add one element from the Stack/Queue
*/
public class EnqueuePushHandler extends GeneralHandler{
/**EnqueuePushHandler constructor, initializes a new EnqueuePushHandler
*@param intQ the reference to a Queue of Integers
*/
public EnqueuePushHandler(Queue<Integer> intQ){
super(intQ);
}
/**EnqueuePushHandler constructor, initializes a new EnqueuePushHandler
*@param intSt the reference to a Stack of Integers
*/
public EnqueuePushHandler(Stack<Integer> intSt){
super(intSt);
}
@Override
/**This method implements the abstract method "processRequest",
*inherited from GeneralHandler class. This method acquires input from the user
*and validates the input and if valid puts it in the
*Queue/Stack respectively.
*@throws Exception if input is not valid
*/
public void processRequest(){
JOptionPane dialog = new JOptionPane();
String action=null,type=null;
if(intQ==null){
action="push";
type="Stack";
}
else {
action="enqueue";
type="Queue";
}
String dialogmsg = "Please enter a number to "+action+" to the "+type;
String input=dialog.showInputDialog(dialogmsg);
int val=0;
try {
val = Integer.parseInt(input);
}
catch (NumberFormatException e) {
dialog.showMessageDialog(null,input+" is not Numeric, "+action+" aborted!");
return;
}
if(type=="Stack") //check if by-value
intSt.push(val);
else
intQ.enqueue(val);
dialog.showMessageDialog(null,action+" operation of "+val+" successfully completed!");
}
}