} //************************************** //练习3 如果还没有section,直接添加,如果已有,添加判断,遍历Teaches已有的section,只有没有该时段内的Teaches才能添加. public void AgreeToTeach(Section s) { Boolean l = false; if (Teaches.Count == 0) { Teaches.Add(s); } else { for (int i = 0; i < Teaches.Count; i++) { if (Teaches[i].TimeOfDay == s.TimeOfDay && Teaches[i].DayOfWeek == s.DayOfWeek) { l = true; } break; } if (l = false) { Teaches.Add(s); } // We need to link this bidirectionally. s.Instructor = this; } }
//************************************** // public void AgreeToTeach(Section s) { Teaches.Add(s); // We need to link this bidirectionally. s.Instructor = this; }
//检查是否添加section,遍历已有的section,只有符合条件的才可以添加 public void AgreeToTeach(Section s) { Boolean l = false; if (Teaches.Count == 0) { Teaches.Add(s); } else { for (int i=0; i<Teaches.Count; i++) { if(Teaches[i].TimeOfDay==s.TimeOfDay &&Teaches[i].DayOfWeek==s.DayOfWeek) l =true; break; } } if(l=false) }
//************************************** // //题3:一位教授不能在同一天/同一时刻教两门课 public void AgreeToTeach(Section s) { foreach (Section s1 in Teaches) { if (s == s1) { Console.WriteLine("一位教授不能在同一天/同一时刻教两门课!"); } else { Teaches.Add(s); s.Instructor = this; } } // We need to link this bidirectionally. }
//************************************** //练习3 public void AgreeToTeach(Section s) { bool isSameTime = true; for (int i = 1; i < Teaches.Count; i++) { if (Teaches[i].OfferedIn == s.OfferedIn) { if (Teaches[i].DayOfWeek == s.DayOfWeek) { if (Teaches[i].TimeOfDay == s.TimeOfDay) { isSameTime = false; } else { continue; } } else { continue; } } else { continue; } } if (isSameTime) { Teaches.Add(s); // We need to link this bidirectionally. s.Instructor = this; } else { Console.WriteLine("一位教授不能再同一天/同一时刻教授两门课"); } }
//************************************** //第3题 一位教授不能同时教授两门课程 public void AgreeToTeach(Section s) { bool T = false; Course c1 = s.RepresentedCourse; while (c.MoveNext()) { Section s2 = (Section)c.Current; Course c2 = s2.RepresentedCourse; if (c1 == c2) { T = true; break; } Teaches.Add(s); } // We need to link this bidirectionally. s.Instructor = this; }
//************************************** // public void AgreeToTeach(Section s) { //练习14.3 bool theSameTime = true; for (int i = 0; i < Teaches.Count; i++) { if (Teaches[i].OfferedIn == s.OfferedIn) { if (Teaches[i].DayOfWeek == s.DayOfWeek) { if (Teaches[i].TimeOfDay == s.TimeOfDay) { theSameTime = false; } else { continue; } } else { continue; } } else { continue; } } if (theSameTime) { Teaches.Add(s); // We need to link this bidirectionally. s.Instructor = this; } else { Console.WriteLine("同一天、同一时间教授上课时间冲突!"); } }
//************************************** // public void AgreeToTeach(Section s) { int jieguo; //第三题 bool status; status = true; for (int i = 0; i < Teaches.Count; i++) { jieguo = String.Compare(s.TimeOfDay, Teaches[i].TimeOfDay); if (jieguo == 0) { status = false; Console.WriteLine(s + "和" + Teaches[i] + "时间冲突"); break; } } if (status == true) { Teaches.Add(s); s.Instructor = this; } // We need to link this bidirectionally. }
//************************************** // public void AgreeToTeach(Section s) { //练习14.3 Boolean r = true; for (int i = 0; i < Teaches.Count; i++) { Section s1 = Teaches[i]; if (s.TimeOfDay == s1.TimeOfDay && s.DayOfWeek == s1.DayOfWeek) { Console.Write("教师上课时间冲突"); r = false; break; } } if (r) { Teaches.Add(s); } // We need to link this bidirectionally. s.Instructor = this; }