-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstitution.java
270 lines (218 loc) · 8.62 KB
/
Institution.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
import java.util.Scanner;
public class Institution {
private static Course[] courses = new Course[20];
static Scanner input = new Scanner (System.in);
public static void main (String[] args){
addCourse("IT01" , "135-11-12", "12/02/21" , 3000);
addCourse("CS01" , "135-8-9", "13/02/21" , 2000);
addCourse("IS01" , "135-9-10", "11/02/21" , 1000);
addCourse("SW01" , "234-8-9", "14/02/21" , 2000);
addCourse("IT20" , "135-12-01", "20/02/21" , 1000);
registerCourse("IT01","Ali");
registerCourse("IT01","Sara");
registerCourse("CS01","Sara");
registerCourse("IS01","Sara");
registerCourse("IS01","Fay");
registerCourse("SW01","Ali");
int choice;
System.out.println(" **** Welcome to Institute Course Registration System ****");
do {
System.out.println("---------------------------------------");
System.out.println("Choose one of these actions:");
System.out.println("(1) Add a new course");
System.out.println("(2) Register a course for a trainee, given the trainee name and course code");
System.out.println("(3) Cancel Course Registration");
System.out.println("(4) Find and Display the information of a course, given the course code");
System.out.println("(5) List All courses");
System.out.println("(6) Find and Display list of registered courses for a given trainee");
System.out.println("(7) Compute and Display the total tuition for a given trainee");
System.out.println("(8) Display total number of added courses");
System.out.println("(9) Exit");
choice = input.nextInt();
switch (choice) {
case 1 :
System.out.println("Enter the course information:");
System.out.print("Course Code in format LLDD, L: letter, D: digit:");
String courseCode= input.next();
System.out.print("weeklyTimeSlots in format D-S-E:");
String weeklyTimeslots= input.next();
System.out.print("finalExamDate in format: DD/MM/YY:");
String finalExamdate= input.next();
System.out.print("Tution:");
double tuition = input.nextDouble();
if(addCourse(courseCode, weeklyTimeslots, finalExamdate, tuition))
System.out.println("The course is added successfully");
else
System.out.println("course is not added!");
break;
case 2 :
System.out.print("Please enter course code: ");
String inputCode = input.next();
System.out.print("Please enter trainee name: ");
String inputName = input.next();
if(registerCourse(inputCode, inputName))
System.out.println("Course is registered successfully. ");
else
System.out.println("Course is not registered.");
break;
case 3 :
System.out.println("Please enter course code: ");
inputCode= input.next();
System.out.println("Please enter trainee name: ");
String inputTrainee= input.next();
if ( cancelCourseRegistration( inputCode , inputTrainee) )
System.out.println("Course registration is cancelled successfully.");
else
System.out.println("Course is not cancelled.");
break;
case 4 :
System.out.println("Please enter course code: ");
inputCode = input.next();
int availability = findCourse(inputCode);
if (availability != -1)
courses[availability].DisplayCourseInfo();
else
System.out.println("This course cannot be found.");
break;
case 5 :
printAll();
break;
case 6 :
System.out.print("Please enter trainee name: ");
inputName = input.next();
Course [] traineeCourses = findRegisteredCourses(inputName);
for (int i = 0 ; i < traineeCourses.length ; i++)
System.out.println( traineeCourses[i].getCourseCode() );
break;
case 7 :
System.out.print("Please enter trainee name: ");
inputName = input.next();
double tuitionSum = 0;
Course [] tuitionList = findRegisteredCourses(inputName);
if (tuitionList.length > 0){
for (int i = 0 ; i < tuitionList.length ; i++)
tuitionSum += tuitionList[i].getTuition();
}
else
System.out.println("This trainee has no registered courses. ");
System.out.println("The total tuition is: "+ tuitionSum);
break;
case 8 :
System.out.println("Total number of courses is: "+ Course.totalCourses);
break;
case 9 :
System.out.println("Thank you");
break;
default :
System.out.println("incorrect input.");
}//end switch
}while (choice != 9);
}//end main
//--------------------------
public static boolean addCourse(String courseCode, String weeklyTimeSlots, String finalExamDate, double tuition) {
if (Course.totalCourses == 20) {
System.out.println("The list is full. ");
return false;}
int courseIndex = findCourse( courseCode );
if (courseIndex != -1){
System.out.println("Course code is not unique. ");
return false; }
Course obj1 = new Course (courseCode, weeklyTimeSlots, finalExamDate,tuition);
courses[Course.totalCourses]= obj1;
Course.totalCourses++;
return true;
}
//--------------------------
public static boolean registerCourse(String courseCode, String traineeName){
int courseIndex = findCourse( courseCode );
if (courseIndex == -1){
System.out.println("Course is not found. ");
return false; }
if (courses[courseIndex].numTrainees == 3 ){
System.out.println("Course is unavailable because there's no capacity. ");
return false; }
Course[] tempList = findRegisteredCourses(traineeName) ;
if (tempList.length == 3) {
System.out.println("Trainee schedule is full. ");
return false; }
String finalExam = courses[courseIndex].getFinalExamDate();
for (int i = 0 ; i < tempList.length ; i++){
if (finalExam.equals( tempList[i].getFinalExamDate() )) {
System.out.println("There's a conflict in final exam date. ");
return false ; }
}
String courseSlots = courses[courseIndex].getWeeklyTimeSlots();
for (int j = 0 ; j < tempList.length ; j++){
String traineeSlots = tempList[j].getWeeklyTimeSlots();
if ((courseSlots.charAt(0) == traineeSlots.charAt(0) ||
courseSlots.charAt(1) == traineeSlots.charAt(1) ||
courseSlots.charAt(2) == traineeSlots.charAt(2)) &&
courseSlots.substring(4).equals(traineeSlots.substring(4) )){
System.out.println("There's a conflict in weekly time slots for "+ traineeName + " during " + tempList[j].getWeeklyTimeSlots());
return false; }
}//close for loop
String[] newTraineeList = courses[courseIndex].getTraineeNames() ;
newTraineeList[courses[courseIndex].numTrainees]= traineeName;
courses[courseIndex].numTrainees++;
return true ;
}
//--------------------------
public static boolean cancelCourseRegistration(String courseCode, String TraineeName){
int courseIndex = findCourse( courseCode );
if (courseIndex == -1){
System.out.println("Course is not found. ");
return false; }
String[] tempList = courses[courseIndex].getTraineeNames();
for (int j = 0; j < courses[courseIndex].numTrainees ; j++)
if (tempList[j].equals(TraineeName)) {
for (int z = j ; z < tempList.length-1 ; z++)//removing the trainee name
tempList[z] = tempList[z+1];
courses[courseIndex].numTrainees--;
tempList[courses[courseIndex].numTrainees]="";
courses[courseIndex].setTraineeNames(tempList);
return true;
}//close if
return false;
}
//--------------------------
public static int findCourse (String courseCode) {
for (int i = 0; i < Course.totalCourses ; i++){
if (courses[i] != null) {
if (courses[i].getCourseCode().equals(courseCode))
return i;
}//close if
}//close for loop
return -1 ;
}
//--------------------------
public static Course[] findRegisteredCourses(String traineeName) {
int regCourses=0;
//loop to create an array of proper length
for (int x = 0 ; x < Course.totalCourses ; x++)
if (courses[x] != null)
{
for (int y = 0 ; y < courses[x].numTrainees ; y++)
if (courses[x].getTraineeNames()[y] != null)
if (courses[x].getTraineeNames()[y].equals(traineeName))
regCourses++;
}
Course [] registeredCourses = new Course [regCourses];
//loop to add the registered courses to the created array
int index = 0 ;
for (int x = 0 ; x < Course.totalCourses ; x++){
if (courses[x] != null)
{
for (int y = 0 ; y < courses[x].numTrainees ; y++)
if (courses[x].getTraineeNames()[y] != null)
if (courses[x].getTraineeNames()[y].equals(traineeName))
registeredCourses[index++] = courses[x] ;
}
}
return registeredCourses;
}
//--------------------------
public static void printAll(){
for (int i = 0; i< Course.totalCourses ; i++)
if (courses[i] != null)
System.out.println(courses[i].getCourseCode());}
}