-
Notifications
You must be signed in to change notification settings - Fork 8
/
Courses.java
275 lines (240 loc) · 9.98 KB
/
Courses.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import java.util.*;
import java.io.File;
import java.io.FileWriter;
public class Courses{
static Scanner input = new Scanner(System.in);
static Main mainClass = new Main(); // main class object
static String[] courseDetails;
static String semester;
// public static void main(String[] args) {
// CourseRegistration ob = new CourseRegistration();
// ob.manage("SP21-BCS-066");
// }
static void manage(String regNumber){
System.out.println("\n*******************************************************\n");
System.out.println("\tManage Courses\n");
System.out.println("*******************************************************\n\n");
while(true){
System.out.print("*****************************************************\n");
System.out.print("1. Register Courses\n");
System.out.print("2. View Registered Courses\n");
System.out.print("3. Previous Menu\n");
System.out.print("4. Exit\n");
System.out.print("\nEnter your choice : ");
try{
int c = input.nextInt();
switch(c){
case 1 : {
registerCourse(regNumber);
break;
}
case 2 : {
viewCourse(regNumber);
break;
}
case 3 : {
Main.management(regNumber);
break;
}
case 4 : {
System.exit(0);
break;
}
default : {
System.out.print("Invalid input! Try again!\n");
input.nextLine();
}
}
}
catch(Exception e){
System.out.print("Invalid input! Try again!\n");
input.nextLine();
}
}
}
public static void registerCourse(String regNumber){
int semester = 0;
String sub = "";
try{
File myfile = new File("students_data/courses/"+ regNumber +".txt");
if(myfile.exists()){
System.out.print("Course already registered!\n");
}
else{
sub = regNumber.substring(0,4);
// Eliminating the semester from the st. ID
if(sub.compareTo("SP18")==0){
semester = 8;
}
else if(sub.compareTo("FA18")==0){
semester = 7;
}
else if(sub.compareTo("SP19")==0){
semester = 6;
}
else if(sub.compareTo("FA19")==0){
semester = 5;
}
else if(sub.compareTo("SP20")==0){
semester = 4;
}
else if(sub.compareTo("SP20")==0){
semester = 3;
}
else if(sub.compareTo("SP21")==0){
semester = 2;
}
else if(sub.compareTo("FA21")==0){
semester = 1;
}
// finding total_courses in file
int total_courses = 0;
try{
File file = new File("courses/"+ semester +".txt");
Scanner sc = new Scanner(file);
while(sc.hasNextLine()){
String line = sc.nextLine();
total_courses++;
}
sc.close();
}
catch(Exception e){
System.out.print(e);
}
// Declaring the array size
courseDetails = new String[total_courses];
// Stroring data in a file.
int[] space = {15,30,15,30};
try{
File file = new File("courses/"+ semester +".txt");
Scanner sc = new Scanner(file);
int i=0,j=0;
System.out.print("\n\n");
System.out.printf("%-5s%-15s%-30s%-15s%-30s%n","ID","Course_Code","Course_Title","Credit_Hours","Instructor");
System.out.println("------------------------------------------------------------------------------------------");
while(sc.hasNextLine()){
System.out.printf("%-5d",i);
String line = sc.nextLine();
courseDetails[i] = line;
String[] data = line.split(",",4);
for(String item:data){
System.out.printf(("%-"+space[j]+"s"),item);
j++;
}
i++;
j=0;
System.out.println();
}
sc.close();
System.out.print("At least register 4 courses\n");
}
catch(Exception e){
System.out.print(e);
}
// Course Registration
while(true){
try{
System.out.print("\nHow many course you want to register:");
int amount = input.nextInt();
if(amount<4){
System.out.print("At least registered 4 courses\n");
continue;
}
else if(amount>courseDetails.length){
System.out.print("Only "+ courseDetails.length +" courses are being offered!\n");
continue;
}
// Storing course ids in array..
int x=0;
int[] ids = new int[amount];
boolean alreadyRegristered = false;
while(true){
try{
alreadyRegristered = false;
System.out.print("\n*************course"+(x+1)+"*******************\n");
System.out.print("Enter choice :");
int choice = input.nextInt();
for(int z=0;z<x;z++){
if(ids[z]==choice){
alreadyRegristered = true;
break;
}
}
if(alreadyRegristered){
System.out.print("Already Registered!\n");
continue;
}
else if(choice<courseDetails.length && choice>=0){
ids[x] = choice;
x++;
if(x==amount){
break;
}
}
else{
System.out.print("Invalid Id, Try again!\n");
continue;
}
}
catch(Exception e){
System.out.print("Invalid input! Try again...");
input.nextLine();
}
}
try{
FileWriter fileWriter = new FileWriter("students_data/courses/"+ regNumber +".txt",true);
int j=0;
for(int id:ids){
int i=0;
fileWriter.write(courseDetails[id]+"\n");
}
fileWriter.close();
System.out.println("Courses Registered Successfully!");
break;
}
catch(Exception e){
System.out.print(e);
}
}
catch(Exception e){
System.out.print("Invalid input! Try again");
input.nextLine();
}
}
}
// else ends
}
catch(Exception e){
System.out.print("Error occured!\n");
}
}
public static void viewCourse(String regNumber){
System.out.print("You are registered in following courses : \n");
int[] space = {15,30,15,30};
try{
File file = new File("students_data/courses/"+ regNumber +".txt");
Scanner sc = new Scanner(file);
int i=0,j=0;
System.out.print("\n\n");
System.out.printf("%-5s%-15s%-30s%-15s%-30s%n","ID","Course_Code","Course_Title","Credit_Hours","Instructor");
System.out.println("------------------------------------------------------------------------------------------");
while(sc.hasNextLine()){
System.out.printf("%-5d",i);
String line = sc.nextLine();
String[] data = line.split(",",4);
for(String item:data){
System.out.printf(("%-"+space[j]+"s"),item);
j++;
}
i++;
j=0;
System.out.println();
}
sc.close();
}
catch(Exception e){
System.out.print(e);
}
}
//Class Brackets
}