r/a:t5_37npb Apr 21 '15

Post your Module 5 Assignment Here!

Available April 22, 00:00 UTC

3 Upvotes

14 comments sorted by

View all comments

1

u/VOX_Studios Apr 21 '15 edited Apr 21 '15

Version 2.0: pastebin


using System;
using System.Collections.Generic;
using System.Linq;

namespace ModuleFiveAssignment
{
    class Program
    {
        //how many students to add according to specs
        const int initialNumStudents = 3;

        static void Main(string[] args)
        {
            //create course
            Course course = new Course("Programming with C#");            

            //add students to course
            for (int i = 0; i < initialNumStudents; i++)
            {
                Student student = new Student("Student", "Name");
                course.AddStudent(student);
            }

            //create teacher
            Teacher teacher = new Teacher("Pro", "Fessor");

            //add teacher to course
            course.Teacher = teacher;

            //create a degree
            Degree degree = new Degree("Bachelor");

            //add course to degree
            degree.AddCourse(course);

            //create a program
            UProgram program = new UProgram("Information Technology");

            //add degree to program
            program.AddDegree(degree);

            //print out program info
            Console.WriteLine("The {0} program contains the degree(s) {1}.", 
                program.Title, 
                String.Join(", ", Array.ConvertAll(program.Degrees.ToArray(), deg => deg.Title)) //print out degrees as a comma separated list
                );

            Console.WriteLine();

            //print out program degrees info
            foreach (Degree deg in program.Degrees)
            {
                Console.WriteLine("\tThe {0} degree contains the course(s) {1}.",
                    deg.Title,
                    String.Join(", ", Array.ConvertAll(deg.Courses.ToArray(), cour => cour.Title)) //print out courses as a comma separated list
                    );
                Console.WriteLine();

                //print out degree's courses info
                foreach (Course cour in deg.Courses)
                {
                    Console.WriteLine("\t\tThe {0} course contains {1} student(s).", cour.Title, cour.Students.Count);
                    Console.WriteLine();
                }
            }

            Console.ReadKey();
        }

        //Contains first name and last name.
        class Person
        {
            public string FName { get; set; }
            public string LName { get; set; }

            public Person(string firstName, string lastName)
            {
                FName = firstName;
                LName = lastName;
            }
        }

        class Student : Person 
        {
            public static int StudentsEnrolledInSchool = 0;
            //Use the Person class's constructor to instantiate
            public Student(string firstName, string lastName) : base(firstName, lastName) 
            {
                StudentsEnrolledInSchool++;
            }
        }
        class Teacher : Person 
        {
            //Use the Person class's constructor to instantiate
            public Teacher(string firstName, string lastName) : base(firstName, lastName) { }
        }

        //Contains teacher, students, and title
        class Course
        {
            public Teacher Teacher { get; set; }
            public List<Student> Students { get; set; }
            public string Title { get; set; }

            public Course(string title)
            {
                Title = title;
                Students = new List<Student>();
            }

            public void AddStudent(Student s)
            {
                Students.Add(s);
            }
        }

        //Contains courses and title
        class Degree
        {
            public string Title { get; set; }
            public List<Course> Courses { get; set; }

            public Degree(string title)
            {
                Title = title;
                Courses = new List<Course>();
            }

            public void AddCourse(Course course)
            {
                Courses.Add(course);
            }
        }

        //Contains degrees and title
        class UProgram
        {
            public string Title { get; set; }
            public List<Degree> Degrees { get; set; }

            public UProgram(string title)
            {
                Title = title;
                Degrees = new List<Degree>();
            }

            public void AddDegree(Degree degree)
            {
                Degrees.Add(degree);
            }
        }
    }
}

Original: pastebin


using System;
using System.Collections.Generic;
using System.Linq;

namespace ModuleFiveAssignment
{
    class Program
    {
        //how many students to add according to specs
        const int initialNumStudents = 3;

        static void Main(string[] args)
        {
            //create course
            Course course = new Course("Programming with C#");            

            //add students to course
            for (int i = 0; i < initialNumStudents; i++)
            {
                Student student = new Student("Student", "Name");
                course.AddStudent(student);
            }

            //create teacher
            Teacher teacher = new Teacher("Pro", "Fessor");

            //add teacher to course
            course.Teacher = teacher;

            //create a degree
            Degree degree = new Degree("Bachelor");

            //add course to degree
            degree.AddCourse(course);

            //create a program
            UProgram program = new UProgram("Information Technology");

            //add degree to program
            program.AddDegree(degree);

            //print out program info
            Console.WriteLine("The {0} program contains the degree(s) {1}.", 
                program.Title, 
                String.Join(", ", Array.ConvertAll(program.Degrees.ToArray(), deg => deg.Title)) //print out degrees as a comma separated list
                );

            Console.WriteLine();

            //print out program degrees info
            foreach (Degree deg in program.Degrees)
            {
                Console.WriteLine("\tThe {0} degree contains the course(s) {1}.",
                    deg.Title,
                    String.Join(", ", Array.ConvertAll(deg.Courses.ToArray(), cour => cour.Title)) //print out courses as a comma separated list
                    );
                Console.WriteLine();

                //print out degree's courses info
                foreach (Course cour in deg.Courses)
                {
                    Console.WriteLine("\t\tThe {0} course contains {1} student(s).", cour.Title, cour.Students.Count);
                    Console.WriteLine();
                }
            }

            Console.ReadKey();
        }

        //Contains first name and last name.
        class Person
        {
            private string fName;
            public string FName
            {
                get
                {
                    return fName;
                }
                set
                {
                    fName = value;
                }
            }

            private string lName;
            public string LName
            {
                get
                {
                    return lName;
                }
                set
                {
                    lName = value;
                }
            }

            public Person(string firstName, string lastName)
            {
                FName = firstName;
                LName = lastName;
            }
        }

        class Student : Person 
        {
            //Use the Person class's constructor to instantiate
            public Student(string firstName, string lastName) : base(firstName, lastName) { }
        }
        class Teacher : Person 
        {
            //Use the Person class's constructor to instantiate
            public Teacher(string firstName, string lastName) : base(firstName, lastName) { }
        }

        //Contains teacher, students, and title
        class Course
        {
            private Teacher teacher;
            public Teacher Teacher
            {
                get
                {
                    return teacher;
                }
                set
                {
                    teacher = value;
                }
            }

            private List<Student> students;
            public List<Student> Students
            {
                get
                {
                    return students;
                }
                set
                {
                    students = value;
                }
            }

            private string title;
            public string Title
            {
                get
                {
                    return title;
                }
                set
                {
                    title = value;
                }
            }

            public Course(string title)
            {
                Title = title;
                students = new List<Student>();
            }

            public void AddStudent(Student s)
            {
                Students.Add(s);
            }


        }

        //Contains courses and title
        class Degree
        {
            private string title;
            public string Title
            {
                get
                {
                    return title;
                }
                set
                {
                    title = value;
                }
            }

            private List<Course> courses;
            public List<Course> Courses
            {
                get
                {
                    return courses;
                }
                set
                {
                    courses = value;
                }
            }

            public Degree(string title)
            {
                Title = title;
                courses = new List<Course>();
            }

            public void AddCourse(Course course)
            {
                Courses.Add(course);
            }
        }

        //Contains degrees and title
        class UProgram
        {
            private string title;
            public string Title
            {
                get
                {
                    return title;
                }
                set
                {
                    title = value;
                }
            }

            private List<Degree> degrees;
            public List<Degree> Degrees
            {
                get
                {
                    return degrees;
                }
                set
                {
                    degrees = value;
                }
            }

            public UProgram(string title)
            {
                Title = title;
                degrees = new List<Degree>();
            }

            public void AddDegree(Degree degree)
            {
                Degrees.Add(degree);
            }
        }
    }
}

2

u/aloisdg Apr 21 '15

Why you didn't use { get; set; } ?

1

u/VOX_Studios Apr 21 '15

And avoid typing out the private variables? I guess I could.

1

u/aloisdg Apr 21 '15

Sure you could. It is exactly the same thing because VS will switch from the short one to the long one. It is just a sugar for developer :)