Tuesday, May 6, 2014

Class Containing List

namespace Class_Containing_List
{
    public class Course
    {
        //private fields

        private string _coursename;
        private string _courseID;
        private string _instructor;
        private List<int> _gradeList;

        //constructor
        public Course(string coursename, string courseID, string instructor)
        {
            _courseID = courseID;
            _coursename = coursename;
            _instructor = instructor;
            _gradeList = new List<int>();

        }
        //properties
        public string Coursename
        { get { return _coursename; } }
        public string CourseID
        { get { return _courseID; } }
        public string Instructor
        { get { return _instructor; } }
       
        //for the _gradeList, avoid returning a direct reference to the _gradeList
        //to avoid giving access to its content and allow its content to change.
        //should return a copy as an array.


        public int[] GradeList
        { get { return _gradeList.ToArray(); } }
       
        //methods
        public void AddGrade(int grade)
        {
            _gradeList.Add(grade);
        }
        public void RemoveLowestGrade()
        {
            int lowest = GetLowestGrade();
            _gradeList.Remove(lowest);
        }

        public double GetAverageGrade()
        {
            double avg = _gradeList.Average();
            return avg;
        }
        public int GetHighestGrade()
        {
            int highest = _gradeList.Max();
            return highest;
        }
        public int GetLowestGrade()
        {
            int lowest = _gradeList.Min();
            return lowest;
        }
        public bool IsPassing()
        {
            return true;
        }
    }
}
//============================
namespace Class_Containing_List
{
    public partial class Form1 : Form
    {
        List<Course> courseList = new List<Course>();
        Random rand = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        private void Displaycourses()
        {
            richTextBox1.Clear();
            foreach (Course course in courseList)
            {
            richTextBox1.AppendText(String.Format("{0} {1} {2} \n", course.CourseID, course.Coursename, course.Instructor));
            }
        }
        private void btnDisplayCourses_Click(object sender, EventArgs e)
        {
            Displaycourses();
        }

        private void btnCreateCourses_Click(object sender, EventArgs e)
        {
            Course course1 = new Course("C#", "CSI-154", "L. Zerrouki");
            Course course2 = new Course("Database", "CSI-156", "T. Culler");
            Course course3 = new Course("Database", "CSI-147", "T. Culler");

            //add to list
            for (int i = 1; i <= 7; i++)
            {
                course1.AddGrade(rand.Next(40, 101));
                course2.AddGrade(rand.Next(40, 101));
                course3.AddGrade(rand.Next(40, 101));

            }
            courseList.Add(course1);
            courseList.Add(course2);
            courseList.Add(course3);

            //disable the button
            btnCreateCourses.Enabled = false;
        }

        private void btnGetGrades_Click(object sender, EventArgs e)
        {
            //get course id
            string courseID = txtCourseID.Text;
            //get course object with the specified courseID
            Course course = GetCourseById(courseID);
            if (course != null)
            {
                int[] grades = course.GradeList;
                //display
                richTextBox1.Text = "Grades: \n";
                foreach (int grade in grades)
                    richTextBox1.AppendText(grade + "\n");

                //display average grade
                //display highest and lowest grade

                double average = course.GetAverageGrade();
                richTextBox1.AppendText("==========\nAverage: \n"
                    + average.ToString("f2") + "\n");
                double highest = course.GetHighestGrade();
                richTextBox1.AppendText("==========\nHighest: \n"
                    + highest.ToString("f2") + "\n");
                double lowest = course.GetLowestGrade();
                richTextBox1.AppendText("==========\nLowest: \n"
                    + lowest.ToString("f2") + "\n");
            }
        }
        //Method that takes a courseID
        //return the course object

        private Course GetCourseById(string courseID)
        {
            ///sequence through the courseList and search /return the course object
            ///with the given ID

            foreach (Course course in courseList)
            {
                if (course.CourseID == courseID)
                    return course;
            }
            //if the code gets to this point, means that
            //such course has not been found
            return null;
        }
    }
}
    //TO DO
    //ADD RICHTEXTBOX AND BUTTON
    //CREATE THREE COURSE OBJECT FOR C#, DATABASE,PHOTOSHOP
    //ADD ABOUT 5 GRADES TO EACH COURSE
    //DISPLAY THE AVERAGE, HIGHEST,LOWEST GRADES OF EACH COURSE


No comments:

Post a Comment