Download PlanIt

Transcript
3.2 Important Methods
1
2
3
4
5
p r i v a t e b o o l WeekSchedule ( i n t w e e k S t a r t , i n t s t a r t D a y )
{
uint nFailed = 0;
/ / The number o f a c t i v i t i e s t h a t c o u l d n o t be p l a c e d
L i s t < Lesson > l e s s o n C a n d i d a t e s = s e m e s t e r . GetWeekLessons ( ) ;
o n e s t h a t s h o u l d be p l a c e d
6
7
L i s t < Lesson > u s e d L e s s o n s = new L i s t < Lesson > ( ) ;
have been p l a c e d .
L i s t < Lesson > u n u s e d L e s s o n s = new L i s t < Lesson > ( ) ;
failed .
8
9
10
/ / The l e s s o n s t h a t
/ / The l e s s o n s t h a t
l e s s o n C a n d i d a t e s . AddRange ( l e f t o v e r L e s s o n s ) ; / / Add t h e u n u s e d l e s s o n s f r o m
t h e week b e f o r e
11
12
/ / S o r t them by a t t e n d a n c e , s o we g i v e p r i o r i t y t o t h o s e w i t h t h e m o s t
students
lessonCandidates . Sort (
d e l e g a t e ( Lesson a , Lesson b )
{
r e t u r n −a . Owner . G e t N u m b e r O f S t u d e n t s ( ) . CompareTo ( b . Owner .
GetNumberOfStudents ( ) ) ;
}
);
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
/ / T r y t o p l a c e them .
foreach ( Lesson l in l e s s o n C a n d i d a t e s )
{
i f ( TryToPlace ( l , weekStart , s t a r t D a y ) )
{
u s e d L e s s o n s . Add ( l ) ;
}
else
{
u n u s e d L e s s o n s . Add ( l ) ;
}
}
/ / A b o r t i f t o o many f a i l e d .
i f ( u n u s e d L e s s o n s . Count > 2 )
{
return f a l s e ;
}
/ / E l s e , i f t h e r e i s a s m a l l number o f u n p l a c e d l e s s o n s l e f t , s a v e them f o r
t h e n e x t week .
leftoverLessons . Clear () ;
l e f t o v e r L e s s o n s . AddRange ( u n u s e d L e s s o n s ) ;
40
41
42
43
44
45
46
47
48
/ / Get t h e new
//
I f we s u c c e e d i n p l a c i n g them , i n c r e m e n t t h e number o f p l a c e d l e s s o n s o f
the courses
foreach ( Lesson l in usedLessons )
l . Placed ( ) ;
return true ;
}
It starts out with retrieving a list of lessons that are to be placed in the coming week from the
Semester instance, and adds in any leftover lessons from the preceding week. As the method
tries to place them, they are placed in one of either the usedLessons or unusedLessons lists.
The list of lesson candidates is sorted to place the lessons with the most attendants first (the
3. Implementation
61