- Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCourseRegistration.java
More file actions
Latest commit
165 lines (140 loc) · 5.21 KB
/
Copy pathCourseRegistration.java
File metadata and controls
165 lines (140 loc) · 5.21 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
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
importjava.util.*;
importjava.io.File;
importjava.io.FileWriter;
publicclassCourseRegistration{
staticScannerinput = newScanner(System.in);
staticMainmainClass = newMain(); // main class object
staticString[][] courseDetails;
staticStringsemester;
// public static void main(String[] args) {
// CourseRegistration ob = new CourseRegistration();
// ob.manage("SP21-BCS-066");
// }
staticvoidmanage(StringregNumber){
System.out.println("\n*******************************************************\n");
System.out.println("\tCourse Registration\n");
System.out.println("*******************************************************\n");
// Checking that courses registered or not.
try{
Filemyfile = newFile("students_data/"+ regNumber +".txt");
Scannersc = newScanner(myfile);
while(sc.hasNextLine()){
Stringline = sc.nextLine();
if(line.contains("Courses")){
System.out.print("Courses already registered!\n");
break;
}
}
}
catch(Exceptione){
System.out.println(e);
}
// Semester Entry and vallidation
while(true){
try{
System.out.print("Enter your semester(1 for first semester ..'1-8') : ");
semester = input.next();
if(Integer.parseInt(semester)<=8 && Integer.parseInt(semester)>0){
break;
}
else{
System.out.println("Invalid input, Try again!");
input.nextLine();
}
}
catch(Exceptione){
System.out.println("Invalid input, Try again!");
input.nextLine();
}
}
Filemyfile = newFile("courses/"+ semester +".txt");
// finding total_courses in file
inttotal_courses = 0;
try{
Scannersc = newScanner(myfile);
while(sc.hasNextLine()){
Stringline = sc.nextLine();
total_courses++;
}
}
catch(Exceptione){
System.out.print(e);
}
// Declaring the array size
courseDetails = newString[total_courses][4];
// Stroring data in a file.
int[] space = {15,30,15,30};
try{
Scannersc = newScanner(myfile);
inti=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);
Stringline = sc.nextLine();
String[] data = line.split(",",4);
for(Stringitem:data){
System.out.printf(("%-"+space[j]+"s"),item);
courseDetails[i][j] = item;
j++;
}
i++;
j=0;
System.out.println();
}
}
catch(Exceptione){
System.out.print(e);
}
// Course Registration
while(true){
try{
System.out.print("Enter the ID's of courses you want to register(comma separated) : ");
String[] ids = input.next().split(",");
for(Stringe:ids){
if(Integer.parseInt(e)>courseDetails.length-1 && Integer.parseInt(e)<0){
continue;
}
}
try{
FileWriterfileWriter = newFileWriter("students_data/"+ regNumber +".txt",true);
fileWriter.write("\nCourses:");
intj=0;
for(Stringid:ids){
inti=0;
for(Stringd:courseDetails[Integer.parseInt(id)]){
fileWriter.write(d);
if(i<3){
fileWriter.write(",");
}
i++;
}
if(j<ids.length-1){
fileWriter.write(" | ");
}
j++;
}
fileWriter.close();
System.out.println("Courses Registered Successfully!");
break;
}
catch(Exceptione){
System.out.print(e);
}
}
catch(Exceptione){
}
}
// System.out.println("5. Previous Menu");
// System.out.println();
// System.out.print("Enter your choice : ");
// int c = input.nextInt();
// switch(c){
// case 5 : {
// mainClass.management(regNumber);
// break;
// }
// }
}
}