Here is my solution: The comments in the source code points out the interesting details.using System;
using System.Collections.Generic;
public abstract class Course{
private string name;
public Course(string name){
this.name = name;
}
public abstract bool Passed();
}
public class BooleanCourse: Course{
private bool grade;
public BooleanCourse(string name, bool grade): base(name){
this.grade = grade;
}
public override bool Passed(){
return grade;
}
}
public class GradedCourse: Course{
private int grade;
public GradedCourse(string name, int grade): base(name){
this.grade = grade;
}
public override bool Passed(){
return grade >= 2;
}
}
public class Project{
private List<Course> courses; // Now a list of courses instead of
// four variables of type Course
public Project(Course c1, Course c2, Course c3, Course c4){
// Could/should be generalized to one parameter of type List<Course>
courses = new List<Course>();
courses.Add(c1); courses.Add(c2);
courses.Add(c3); courses.Add(c4);
}
public bool Passed(){ // Reimplemented. Counts the number
// of passed courses
List<Course> passedCourses =
courses.FindAll(delegate(Course c){return c.Passed();});
return passedCourses.Count >= 3;
}
}
public class Program {
public static void Main(){
Course c1 = new BooleanCourse("Math", true),
c2 = new BooleanCourse("Geography", true),
c3 = new GradedCourse("Programming", 2),
c4 = new GradedCourse("Algorithms", 4);
Project p = new Project(c1, c2, c3, c4);
Console.WriteLine("Project Passed: {0}", p.Passed());
}
}