-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFaculty.java
180 lines (179 loc) · 5.12 KB
/
Faculty.java
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
import java.io.*;
import java.lang.*;
import java.util.*;
class MCQ{
String question;
String[] options;
String answer;
public MCQ(String qn,String[] opn, String ans){
this.question = qn;
this.options = opn.clone();
this.answer = ans;
}
public get_question(){
return this.question;
}
public get_options(){
return this.options;
}
public get_answer(){
return this.answer();
}
}
public class Faculty implements Login extends User{
String name;
String dept;
int id;
String course
ArrayList<MCQ> mcq
public Faculty(String name, String dept, int id, String course) throws IOException,FileNotFoundException{
this.name = name;
this.dept = dept;
this.id = id;
this.course = course;
mcq = new ArrayList<MCQ>();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("questions.txt")));
String question = "";
String[] options;
String answer;
do{
String val = br.readLine();
question = val.split("|")[0];
answer = val.split("|")[1];
for(int i = 0; i < 4; i++){
options[i] = br.readLine();
}
mcq.add(new MCQ(question,options,answer));
}while(!val.equals(""));
br.close();
}
public void subMenu(){
System.out.println("1)List questions\n2)Add new question\n3)Edit question\n4)Delete question\n5)Exit\nYour choice:");
}
public Boolean findInList(String question){
Iterator itr = mcq.iterator();
while(itr.hasNext()){
MCQ obj = itr.next();
if(question.equals(obj.get_question()))
return true;
}
return false;
}
public void replace(String question,String newquestion, String[] options, String answer){
ListIterator litr = mcq.listIterator();
while(litr.hasNext()){
MCQ obj = litr.next();
if(question.equals(obj.get_question()))
litr.set(new MCQ(newquestion,options,answer));
}
}
public void remove(String question){
Iterator itr = mcq.iterator();
while(itr.hasNext()){
MCQ obj = itr.next();
if(question.equals(obj.get_question())){
itr.remove();
return;
}
}
}
public void makePaper(){
BufferedWriter fo = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("questions.txt")));
Iterator itr = mcq.iterator();
while(itr.hasNext()){
MCQ obj = itr.next();
fo.write(obj.get_question()+"\n");
String ops = obj.get_options();
int i = 1;
for(String val:ops){
fo.write(i+") "+val+"\n");
i++;
}
fo.write(obj.get_answer());
}
}
public void listQuestions(){
Iterator itr = mcq.iterator();
int i = 1;
while(itr.hasNext()){
MCQ obj = itr.next();
System.out.printf("Question %1$d :\n%2$s\nOptions",i,obj.get_question());
String[] options = obj.get_options();
for(String opt:options){
int j = 0;
System.out.printf("%1$) %2$s",j,opt);
}
}
}
public void addNewQuestion() throws IOException,FileNotFoundException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the new question");
String question = br.readLine();
if(findInList(question)){
System.out.println("Question already exists\nUse edit");
return;
}
}
System.out.println("Enter the answer");
String answer = br.readLine();
System.out.println("Enter the options\n");
String[] options;
for(int i = 0; i < 4; i++){
System.out.println("Option "+i);
options[i] = br.readLine();
}
mcq.add(new MCQ(question,options,answer);
System.out.println("Question added");
}
public void editQuestion(){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the question to edit");
if(!findInList(question)){
System.out.println("Question not found\nUse add");
return;
}
String question = br.readLine();
System.out.println("Enter new question");
String newquestion = br.readLine();
System.out.println("Enter new options");
String[] options;
for(int i = 0; i < 4; i++){
System.out.println("Option "+i);
options[i] = br.readLine();
}
System.out.println("Enter the answer");
String answer = br.readLine();
replace(question,newquestion,options,answer);
System.out.println("Question changed");
}
public void removeQuestion() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter question to remove");
String question = br.readLine();
if(!findInList(question)){
System.out.println("Question not present");
return;
}
remove(question);
System.out.println("Remove question");
}
public void runner() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
subMenu();
int choice = br.read();
while(choice != 5){
switch(choice){
case 1: listQuestions();break;
case 2: addNewQuestion();break;
case 3: editQuestion();break;
case 4: removeQuestion();break;
default: System.out.println("Invalid Choice");break;
}
choice = br.read();
}
if(choice == 5){
makePaper();
System.out.println("Returning");
}
}
}