-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModerator.java
More file actions
77 lines (71 loc) · 2.85 KB
/
Moderator.java
File metadata and controls
77 lines (71 loc) · 2.85 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
import java.io.*;
import java.util.NoSuchElementException;
import java.util.Scanner;
/**
* Driver program for questionqueue
* @author Michael Roscoe
* @author Adrian Bolesnikov
*/
public class Moderator {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
QuestionQueue queue = new QuestionQueue();
String s = "Moderator options:\nN = Add a new question\nA = Ask current question\nP = Pass on current question" +
"\nQ = quit";
System.out.println("\nNo submitted questions pending.");
System.out.println(s);
System.out.print("Enter your choice: ");
char ch = sc.nextLine().toUpperCase().charAt(0);
while (ch != 'Q') {
switch (ch) {
case 'N':
System.out.println("Enter question:");
String question = sc.nextLine();
System.out.println("Submitted by:");
String submitter = sc.nextLine();
queue.enqueue(new Question(question, submitter));
break;
case 'A':
System.out.println("Question asked.\n");
try {
queue.dequeue();
}
catch (NoSuchElementException e) {
System.out.println("Error - No question pending.");
}
break;
case 'P':
System.out.println("Passed.\n");
try {
queue.enqueue(queue.dequeue());
}
catch (NoSuchElementException e) {
System.out.println("Error - No question pending.");
}
break;
default:
System.out.println("Incorrect input, character not available choice - please enter valid input.");
}
if (queue.isEmpty()) {
System.out.println("\nNo submitted questions pending.");
} else {
System.out.println("\nCurrent question: " + queue.peek());
}
System.out.println(s);
System.out.print("Enter your choice: ");
ch = sc.nextLine().toUpperCase().charAt(0);
}
try {
FileOutputStream out = new FileOutputStream("QuestionPool.dat");
BufferedOutputStream buffer = new BufferedOutputStream(out);
ObjectOutputStream o = new ObjectOutputStream(buffer);
o.writeObject(queue);
o.flush();
out.close();
System.out.println("Unused questions archived to: QuestionPool.dat");
}
catch (IOException e) {
System.out.println("Something went wrong - check file permissions and file location.");
}
}
}