forked from getmubarak/JavaCleanCodeProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeTable.java
More file actions
381 lines (327 loc) · 11.7 KB
/
TimeTable.java
File metadata and controls
381 lines (327 loc) · 11.7 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package dynamicTT;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Stack;
public class TimeTable {
private ArrayList<ClassRoom> rooms = new ArrayList<ClassRoom>();
private int fittness;
private ArrayList<Lecture> classes=new ArrayList<>();
private ArrayList<StudentGroups> studentGroups=new ArrayList<>();
private ArrayList<ClassRoom> practicalRooms = new ArrayList<ClassRoom>();
private ArrayList<ClassRoom> theoryRooms = new ArrayList<ClassRoom>();
private ArrayList<StudentGroups> theoryStudentGroups=new ArrayList<>();
private ArrayList<StudentGroups> practicalStudentGroups=new ArrayList<>();
private HashMap<Combination, Week> personalTimeTable= new HashMap<Combination, Week>();
//private ArrayList<Professor> professors=new ArrayList<>();
//adds more rooms to timetable
public TimeTable(ArrayList<ClassRoom> classroom, ArrayList<Lecture> lectures){//, ArrayList<Professor> professors){
this.rooms=classroom;
this.classes=lectures;
this.fittness=999;
// this.professors=professors;
}
// public void initialization(ArrayList<ClassRoom> classroom, ArrayList<Lecture> lectures){
// this.rooms=classroom;
// this.classes=lectures;
// this.fittness=999;
// }
public int getFittness() {
return fittness;
}
public void setFittness(int fittness) {
this.fittness = fittness;
}
public void addStudentGroups(ArrayList<StudentGroups> studentgrps) {
// TODO Auto-generated method stub
studentGroups.addAll(studentgrps);
}
public void initializeTimeTable(){
for (Iterator<ClassRoom> roomsIterator = rooms.iterator(); roomsIterator.hasNext();) {
ClassRoom room = roomsIterator.next();
if(room.isLaboratory()){
practicalRooms.add(room);
}
else{
theoryRooms.add(room);
}
}
for (Iterator<StudentGroups> studentGroupIterator = studentGroups.iterator(); studentGroupIterator.hasNext();) {
StudentGroups studentGroup = studentGroupIterator.next();
if(studentGroup.isPractical()){
practicalStudentGroups.add(studentGroup);
}
else{
theoryStudentGroups.add(studentGroup);
}
}
rooms.clear();
//studentGroups.clear();
setTimeTable(practicalStudentGroups, practicalRooms, "practical");
setTimeTable(theoryStudentGroups, theoryRooms, "theory");
rooms.addAll(practicalRooms);
rooms.addAll(theoryRooms);
//studentGroups.addAll(practicalStudentGroups);
//studentGroups.addAll(theoryStudentGroups);
}
public void setTimeTable(ArrayList<StudentGroups> studentGroups2, ArrayList<ClassRoom> rooms2, String string) {
// TODO Auto-generated method stub
Collections.shuffle(studentGroups2);
Stack<Lecture> lecturesStack=new Stack<Lecture>();
for (Iterator<StudentGroups> sdtGrpIterator = studentGroups2.iterator(); sdtGrpIterator.hasNext();) {
StudentGroups studentGrp = sdtGrpIterator.next();
String subject = studentGrp.getSubjectName();
int noOfLectures = studentGrp.getNoOfLecturePerWeek();
for(int i=0; i<noOfLectures; i++){
Collections.shuffle(classes);
Iterator<Lecture> classIterator = classes.iterator();
while(classIterator.hasNext()){
Lecture lecture = classIterator.next();
if(lecture.getSubject().equalsIgnoreCase(subject)){
Lecture mainLecture=new Lecture(lecture.getProfessor(), lecture.getSubject());
mainLecture.setStudentGroup(studentGrp);
lecturesStack.push(mainLecture);
break;
}
}
}
}
while(!(lecturesStack.empty())){
Collections.shuffle(lecturesStack);
Lecture lecture2 = lecturesStack.pop();
if(string.equalsIgnoreCase("theory")){
placeTheoryLecture(lecture2, rooms2);
}
if(string.equalsIgnoreCase("practical")){
placePracticalLecture(lecture2, rooms2);
}
}
}
private void placePracticalLecture(Lecture lecture2, ArrayList<ClassRoom> rooms2) {
// TODO Auto-generated method stub
int size = lecture2.getStudentGroup().getSize();
String dept=lecture2.getStudentGroup().getDepartment();
int i=0;
boolean invalid=true;
ClassRoom room = null;
Collections.shuffle(rooms2);
while(invalid){
room=getBestRoom(size, rooms2);
if(room.getDepartment().equalsIgnoreCase(dept)){
invalid=false;
Collections.shuffle(rooms2);
}
else{
Collections.shuffle(rooms2);
}
}
ArrayList<Day> weekdays = room.getWeek().getWeekDays();
Iterator<Day> daysIterator=weekdays.iterator();
while(daysIterator.hasNext() && i<3){
Day day = daysIterator.next();
ArrayList<TimeSlot> timeslots = day.getTimeSlot();
Iterator<TimeSlot> timeslotIterator= timeslots.iterator();
while(timeslotIterator.hasNext() && i<3){
TimeSlot lecture3 = timeslotIterator.next();
if(lecture3.getLecture()==null){
lecture3.setLecture(lecture2);
i++;
}
}
}
}
private void placeTheoryLecture(Lecture lecture, ArrayList<ClassRoom> rooms2) {
// TODO Auto-generated method stub
int size = lecture.getStudentGroup().getSize();
String dept=lecture.getStudentGroup().getDepartment();
boolean invalid=true;
ClassRoom room = null;
Collections.shuffle(rooms2);
while(invalid){
room=getBestRoom(size, rooms2);
if(room.getDepartment().equalsIgnoreCase(dept)){
invalid=false;
Collections.shuffle(rooms2);
}
else{
Collections.shuffle(rooms2);
}
}
ArrayList<Day> weekdays = room.getWeek().getWeekDays();
Iterator<Day> daysIterator=weekdays.iterator();
while(daysIterator.hasNext()){
Day day = daysIterator.next();
ArrayList<TimeSlot> timeslots = day.getTimeSlot();
Iterator<TimeSlot> timeslotIterator= timeslots.iterator();
while(timeslotIterator.hasNext()){
TimeSlot lecture2 = timeslotIterator.next();
if(lecture2.getLecture()==null){
lecture2.setLecture(lecture);
return;
}
}
}
}
private boolean checkOccupiedRoom(ClassRoom tempRoom, ArrayList<ClassRoom> rooms2) {
// TODO Auto-generated method stub
for (Iterator<ClassRoom> roomsIterator = rooms2.iterator(); roomsIterator.hasNext();){
ClassRoom room = roomsIterator.next();
if(room.equals(tempRoom)){
ArrayList<Day> weekdays = room.getWeek().getWeekDays();
Iterator<Day> daysIterator=weekdays.iterator();
while(daysIterator.hasNext()){
Day day = daysIterator.next();
ArrayList<TimeSlot> timeslots = day.getTimeSlot();
Iterator<TimeSlot> timeslotIterator= timeslots.iterator();
while(timeslotIterator.hasNext()){
TimeSlot lecture = timeslotIterator.next();
if(lecture.getLecture()==null){
return false;
}
}
}
return true;
}
}
return false;
}
private ClassRoom getBestRoom(int size, ArrayList<ClassRoom> rooms2) {
// TODO Auto-generated method stub
int delta = 1000;
ClassRoom room = null;
for (Iterator<ClassRoom> roomsIterator = rooms2.iterator(); roomsIterator.hasNext();){
ClassRoom tempRoom = roomsIterator.next();
if(!checkOccupiedRoom(tempRoom, rooms2)){
int tmp = Math.abs(size - tempRoom.getSize());
if(tmp < delta){
delta = tmp;
room = tempRoom;
}
}
}
return room;
}
// public void createTimeTableGroups(ArrayList<Combination> combinations2){
//// ArrayList<Combination> combinations=new ArrayList<>();
////
//// for(Iterator<Combination> combItr = combinations2.iterator(); combItr.hasNext();){
//// Combination comb = combItr.next();
//// if(!combinations.contains(comb)){
//// combinations.add(comb);
//// }
//// }
//
//
//// for(Iterator<Combination> combIterator = combinations2.iterator(); combIterator.hasNext();){
//// Combination combtn = combIterator.next();
//// personalTimeTable.put(combtn, new Week());
//// }
//
// for(Iterator<Combination> combIterator = combinations2.iterator(); combIterator.hasNext();){
// Combination combtn = combIterator.next();
// for (Iterator<ClassRoom> roomsIterator = theoryRooms.iterator(); roomsIterator.hasNext();){
// ClassRoom room=roomsIterator.next();
// Iterator<Day> daysIterator = room.getWeek().getWeekDays().iterator();
// while(daysIterator.hasNext()){
// Day day = daysIterator.next();
// ArrayList<TimeSlot> timeslots = day.getTimeSlot();
// Iterator<TimeSlot> timeslotIterator= timeslots.iterator();
// while(timeslotIterator.hasNext()){
// TimeSlot lecture = timeslotIterator.next();
// if(lecture.getLecture()==null){
// System.out.print(" free ");
// }
// else if(lecture.getLecture().getStudentGroup().getCombination().contains(combtn)){
// System.out.print("###Room="+room.getRoomNo()/*+" Day="+day.getName()+" Time="+lecture.getSlotTime()*/+" Professor="+lecture.getLecture().getProfessor()+" Subject="+lecture.getLecture().getSubject());
// }
// else{
// System.out.print(" free ");
// }
// }
// System.out.print("\n");
// }
// }
// }
// }
// private void putInPersonalTimeTable(Combination combtn, String roomNo, String name, TimeSlot lecture) {
// // TODO Auto-generated method stub
// Week week = personalTimeTable.get(combtn);
// Iterator<Day> daysIterator=week.getWeekDays().iterator();
// while(daysIterator.hasNext()){
// Day day = daysIterator.next();
// if(day.getName().equalsIgnoreCase(name)){
// Iterator<TimeSlot> timeslotIterator=day.getTimeSlot().iterator();
// while(timeslotIterator.hasNext()){
// TimeSlot lecture2 = (TimeSlot) timeslotIterator.next();
// if(lecture2)
// }
// }
// }
// }
//creates random assignment of lecture using lecture objects, subjects and number of lectures per week to a room
// private ClassRoom randomTimetable(ClassRoom room, ArrayList<Subject> subjectsTaught, ArrayList<Lecture> lectureList) {
// Iterator subIterator=subjectsTaught.iterator();
// Stack<Lecture> lecturesStack=new Stack();
// while(subIterator.hasNext()){
// Subject subject = (Subject) subIterator.next();
// int noOfLecturesPerWeek = subject.getNumberOfLecturesPerWeek();
// for(int i=0; i<noOfLecturesPerWeek; i++){
// Collections.shuffle(lectureList);
// Iterator<Lecture> classIterator = lectureList.iterator();
// while(classIterator.hasNext()){
// Lecture getLecture = classIterator.next();
// if(getLecture.getSubject().equalsIgnoreCase(subject.getSubjectName())){
// lecturesStack.push(getLecture);
// break;
// }
// }
// }
// }
//
// Collections.shuffle(lecturesStack);
// ArrayList<Day> weekdays = room.getWeek().getWeekDays();
// Iterator<Day> daysIterator=weekdays.iterator();
// while(daysIterator.hasNext()){
// Day day = daysIterator.next();
// ArrayList<TimeSlot> timeslots = day.getTimeSlot();
// Iterator timeslotIterator= timeslots.iterator();
// while(timeslotIterator.hasNext() && !(lecturesStack.isEmpty())){
// TimeSlot lecture = (TimeSlot) timeslotIterator.next();
// lecture.setLecture(lecturesStack.pop());
// Collections.shuffle(lecturesStack);
// }
// }
// return room;
// }
public ArrayList<ClassRoom> getRoom() {
return rooms;
}
public void setRoom(ArrayList<ClassRoom> room) {
this.rooms = room;
}
public ArrayList<ClassRoom> getPracticalRooms() {
return practicalRooms;
}
public void setPracticalRooms(ArrayList<ClassRoom> practicalRooms) {
this.practicalRooms = practicalRooms;
}
public ArrayList<ClassRoom> getTheoryRooms() {
return theoryRooms;
}
public void setTheoryRooms(ArrayList<ClassRoom> theoryRooms) {
theoryRooms = theoryRooms;
}
public ArrayList<StudentGroups> getTheoryStudentGroups() {
return theoryStudentGroups;
}
public void setTheoryStudentGroups(ArrayList<StudentGroups> theoryStudentGroups) {
this.theoryStudentGroups = theoryStudentGroups;
}
public ArrayList<StudentGroups> getPracticalStudentGroups() {
return practicalStudentGroups;
}
public void setPracticalStudentGroups(ArrayList<StudentGroups> practicalStudentGroups) {
this.practicalStudentGroups = practicalStudentGroups;
}
}