-
Notifications
You must be signed in to change notification settings - Fork 8
/
CourseRegistration.java
165 lines (140 loc) · 5.21 KB
/
CourseRegistration.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
import java.util.*;
import java.io.File;
import java.io.FileWriter;
public class CourseRegistration{
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("\tCourse Registration\n");
System.out.println("*******************************************************\n");
// Checking that courses registered or not.
try{
File myfile = new File("students_data/"+ regNumber +".txt");
Scanner sc = new Scanner(myfile);
while(sc.hasNextLine()){
String line = sc.nextLine();
if(line.contains("Courses")){
System.out.print("Courses already registered!\n");
break;
}
}
}
catch(Exception e){
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(Exception e){
System.out.println("Invalid input, Try again!");
input.nextLine();
}
}
File myfile = new File("courses/"+ semester +".txt");
// finding total_courses in file
int total_courses = 0;
try{
Scanner sc = new Scanner(myfile);
while(sc.hasNextLine()){
String line = sc.nextLine();
total_courses++;
}
}
catch(Exception e){
System.out.print(e);
}
// Declaring the array size
courseDetails = new String[total_courses][4];
// Stroring data in a file.
int[] space = {15,30,15,30};
try{
Scanner sc = new Scanner(myfile);
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);
courseDetails[i][j] = item;
j++;
}
i++;
j=0;
System.out.println();
}
}
catch(Exception e){
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(String e:ids){
if(Integer.parseInt(e)>courseDetails.length-1 && Integer.parseInt(e)<0){
continue;
}
}
try{
FileWriter fileWriter = new FileWriter("students_data/"+ regNumber +".txt",true);
fileWriter.write("\nCourses:");
int j=0;
for(String id:ids){
int i=0;
for(String d: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(Exception e){
System.out.print(e);
}
}
catch(Exception e){
}
}
// 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;
// }
// }
}
}