static void Main(string[] args)
        {
            BusinessLayer BL = new BusinessLayer();
            int           choice, option, id;
            string        answer;
            bool          exit   = false;
            Teacher       teach  = new Teacher();
            Course        course = new Course();

            Console.WriteLine("Repository Application Menu");

            while (exit == false)
            {
                Console.WriteLine("\n1. Add a Teacher\n" +
                                  "2. Update a Teacher\n" +
                                  "3. Delete a Teacher\n" +
                                  "4. Courses taught by Teacher(input ID)\n" +
                                  "5. Display all Teachers\n\n" +
                                  "6. Add a Course\n" +
                                  "7. Update a Course\n" +
                                  "8. Delete a Course\n" +
                                  "9. Display all Courses\n" +
                                  "0. Exit\n");
                choice = Convert.ToInt32(Console.ReadLine());
                try
                {
                    switch (choice)
                    {
                    case 0:     //exit
                        exit = true;
                        break;

                    case 1:     //add teach
                        Console.WriteLine("add a Teacher's name: ");
                        teach.TeacherName = Console.ReadLine();
                        BL.AddTeacher(teach);
                        break;

                    case 2:     //update teach
                        Console.WriteLine("Search by:\n1. ID\n2. name");
                        option = Convert.ToInt32(Console.ReadLine());
                        if (option == 1)     //search by ID
                        {
                            Console.WriteLine("Enter ID: ");
                            id    = Convert.ToInt32(Console.ReadLine());
                            teach = BL.GetTeacherByID(id);
                        }
                        else if (option == 2)     //search by name
                        {
                            Console.WriteLine("Enter Name: ");
                            string name = Console.ReadLine();
                            teach = BL.GetTeacherByName(name);
                        }
                        Console.WriteLine("Current name: {0} with id: {1}", teach.TeacherName, teach.TeacherId);
                        Console.WriteLine("Change name: ");
                        teach.TeacherName = Console.ReadLine();
                        BL.UpdateTeacher(teach);
                        break;

                    case 3:     //delete teach
                        Console.WriteLine("Search by:\n1. ID\n2. name");
                        option = Convert.ToInt32(Console.ReadLine());
                        if (option == 1)     //search by ID
                        {
                            Console.WriteLine("Enter ID: ");
                            id    = Convert.ToInt32(Console.ReadLine());
                            teach = BL.GetTeacherByID(id);
                        }
                        else if (option == 2)     //search by name
                        {
                            Console.WriteLine("Enter Name: ");
                            string name = Console.ReadLine();
                            teach = BL.GetTeacherByName(name);
                        }
                        Console.WriteLine("Current name: {0} with id: {1}", teach.TeacherName, teach.TeacherId);
                        Console.WriteLine("Delete this? (Y/N)");
                        while (exit == false)     //confirm deletion
                        {
                            answer = Console.ReadLine();
                            if (answer == "y" || answer == "Y")
                            {
                                BL.RemoveTeacher(teach);
                                exit = true;
                            }
                            else if (answer == "n" || answer == "N")
                            {
                                exit = true;
                            }
                            else
                            {
                                Console.WriteLine("only y or n");
                            }
                        }
                        exit = false;
                        break;

                    case 4:     //courses taught by teacher
                        BL.GetAllCourse();
                        Console.WriteLine("Enter a teacher ID: ");
                        id    = Convert.ToInt32(Console.ReadLine());
                        teach = BL.GetTeacherByID(id);
                        Console.WriteLine("Teacher name: {0}\nCourse(s) teaching:\n", teach.TeacherName);
                        foreach (Course c in teach.Courses)
                        {
                            Console.WriteLine(c.CourseName);
                        }
                        break;

                    case 5:     //all teachers
                        Console.WriteLine("{0}\t{1}", "Teacher Name", "Teacher ID");
                        Console.WriteLine("___________________________");
                        foreach (Teacher t in BL.GetAllTeacher())
                        {
                            Console.WriteLine("{0}\t{1}", t.TeacherName, t.TeacherId);
                        }
                        break;

                    case 6:     //add course
                        Console.WriteLine("add a Course name: ");
                        course.CourseName = Console.ReadLine();
                        Console.WriteLine("Who teaches this course?(Enter ID)\n");
                        course.TeacherId = Convert.ToInt32(Console.ReadLine());
                        BL.AddCourse(course);
                        break;

                    case 7:     //update course
                        Console.WriteLine("Search by:\n1. ID\n2. name");
                        option = Convert.ToInt32(Console.ReadLine());
                        if (option == 1)     //search by ID
                        {
                            Console.WriteLine("Enter ID: ");
                            id = Convert.ToInt32(Console.ReadLine());
                            BL.GetCourseByID(id);;
                        }
                        else if (option == 2)     //search by name
                        {
                            Console.WriteLine("Enter Name: ");
                            string name = Console.ReadLine();
                            BL.GetCourseByName(name);
                        }
                        Console.WriteLine("Current name: {0} with id: {1}", course.CourseName, course.CourseId);
                        Console.WriteLine("Change name: ");
                        course.CourseName = Console.ReadLine();
                        BL.UpdateCourse(course);
                        break;

                    case 8:     //delete course
                        Console.WriteLine("Search by:\n1. ID\n2. name");
                        option = Convert.ToInt32(Console.ReadLine());
                        if (option == 1)     //search by ID
                        {
                            Console.WriteLine("Enter ID: ");
                            id     = Convert.ToInt32(Console.ReadLine());
                            course = BL.GetCourseByID(id);
                        }
                        else if (option == 2)     //search by name
                        {
                            Console.WriteLine("Enter Name: ");
                            string name = Console.ReadLine();
                            course = BL.GetCourseByName(name);
                        }
                        Console.WriteLine("Current name: {0} with id: {1}", course.CourseName, course.CourseId);
                        Console.WriteLine("Delete this? (Y/N)");
                        while (exit == false)     //confirm deletion
                        {
                            answer = Console.ReadLine();
                            if (answer == "y" || answer == "Y")
                            {
                                BL.RemoveCourse(course);
                                exit = true;
                            }
                            else if (answer == "n" || answer == "N")
                            {
                                exit = true;
                            }
                            else
                            {
                                Console.WriteLine("only y or n");
                            }
                        }
                        exit = false;
                        break;

                    case 9:     //all courses
                        Console.WriteLine("{0}\t{1}", "Course Name", "Course ID");
                        Console.WriteLine("___________________________");
                        foreach (Course c in BL.GetAllCourse())
                        {
                            Console.WriteLine("{0}\t\t{1}", c.CourseName, c.CourseId);
                        }
                        break;
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine("Item does not exist");
                }
            }
            Console.WriteLine("Press any key to continue . . .");
            Console.ReadKey();
        }
Example #2
0
        static void Main(string[] args)
        {
            BusinessLayer.BusinessLayer bl = new BusinessLayer.BusinessLayer();
            bool run = true;

            while (run)
            {
                Console.WriteLine("");
                Console.WriteLine("----- Respository Program -----");
                Console.WriteLine("[          Teacher           ]");
                Console.WriteLine("1. Add Teacher");
                Console.WriteLine("2. Update Teacher");
                Console.WriteLine("3. Remove Teacher");
                Console.WriteLine("4. Display all courses associated with a Teacher ID");
                Console.WriteLine("5. Display all teachers");
                Console.WriteLine("");
                Console.WriteLine("[          Course           ]");
                Console.WriteLine("6. Add Course");
                Console.WriteLine("7. Update Course");
                Console.WriteLine("8. Remove Course");
                Console.WriteLine("9. Display all courses");
                Console.WriteLine("-------------------------------");
                Console.WriteLine("0. Exit");
                Console.WriteLine("Select an option: ");
                Console.WriteLine("");

                int optionInt = Convert.ToInt32(Console.ReadLine());

                if (optionInt == 1)
                {
                    Console.WriteLine("Teacher Name ?");
                    string name = Console.ReadLine();

                    Teacher temp = new Teacher
                    {
                        TeacherName = name
                    };

                    bl.AddTeacher(temp);
                }
                else if (optionInt == 2)
                {
                    Console.WriteLine("1. Update Teacher by teacher id");
                    Console.WriteLine("2. Update Teacher by teacher name");
                    Console.WriteLine("Select an option: ");
                    int subOption = Convert.ToInt32(Console.ReadLine());
                    if (subOption == 1)
                    {
                        Console.WriteLine("Teacher ID ?");
                        int     id      = Convert.ToInt32(Console.ReadLine());
                        Teacher teacher = bl.GetTeacherByID(id);
                        if (teacher == null)
                        {
                            Console.WriteLine("Teacher not found!");
                        }
                        else
                        {
                            Console.WriteLine("Enter new teacher name: ");
                            teacher.TeacherName = Console.ReadLine();
                            bl.UpdateTeacher(teacher);
                        }
                    }
                    else if (subOption == 2)
                    {
                        Console.WriteLine("Teacher Name ?");
                        string  name = Console.ReadLine();
                        Teacher temp = bl.GetTeacherByName(name);
                        if (temp == null)
                        {
                            Console.WriteLine("Teacher not found!");
                        }
                        else
                        {
                            int     id      = temp.TeacherId;
                            Teacher teacher = bl.GetTeacherByID(id);
                            if (teacher == null)
                            {
                                Console.WriteLine("Teacher not found!");
                            }
                            else
                            {
                                Console.WriteLine("Enter new teacher name: ");
                                teacher.TeacherName = Console.ReadLine();
                                bl.UpdateTeacher(teacher);
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Input is invalid");
                    }
                }
                else if (optionInt == 3)
                {
                    Console.WriteLine("Teacher ID to be deleted?");
                    int     id      = Convert.ToInt16(Console.ReadLine());
                    Teacher teacher = bl.GetTeacherByID(id);
                    if (teacher == null)
                    {
                        Console.WriteLine("Teacher not found !");
                    }
                    else
                    {
                        Teacher temp = bl.GetCoursesByTeacherID(id);
                        foreach (var i in temp.Courses)
                        {
                            Course c = bl.GetCourseByID(i.CourseId);
                            c.TeacherId = null;
                            bl.UpdateCourse(c);
                        }

                        bl.RemoveTeacher(teacher);
                    }
                }
                else if (optionInt == 4)
                {
                    Console.WriteLine("Teacher ID ?");
                    int     id      = Convert.ToInt32(Console.ReadLine());
                    Teacher teacher = bl.GetCoursesByTeacherID(id);
                    if (teacher == null || teacher.Courses.Count == 0)
                    {
                        Console.WriteLine("No courses associated with this Teacher ID");
                    }
                    else
                    {
                        foreach (Course c in teacher.Courses)
                        {
                            Console.WriteLine("- " + c.CourseName);
                        }
                    }
                }
                else if (optionInt == 5)
                {
                    IEnumerable <Teacher> allTeachers = bl.GetAllTeachers();
                    Console.WriteLine();
                    Console.WriteLine("ID Teacher Name");
                    foreach (Teacher temp in allTeachers)
                    {
                        Console.WriteLine(temp.TeacherId + "  " + temp.TeacherName);
                    }
                    Console.WriteLine();
                }
                else if (optionInt == 6)
                {
                    Console.WriteLine("Teacher ID ?");
                    int     id      = Convert.ToInt32(Console.ReadLine());
                    Teacher teacher = bl.GetTeacherByID(id);
                    if (teacher == null)
                    {
                        Console.WriteLine("Teacher not found!");
                    }
                    else
                    {
                        Console.WriteLine("Course Name? ");
                        string cName = Console.ReadLine();
                        //Console.WriteLine("Course Location? ");
                        //string cLocation = Console.ReadLine();
                        Course temp = new Course()
                        {
                            CourseName = cName,
                            TeacherId  = id
                        };
                        bl.AddCourse(temp);
                    }
                }
                else if (optionInt == 7)
                {
                    Console.WriteLine("1. Update Course by Course id");
                    Console.WriteLine("2. Update Course by Course name");
                    Console.WriteLine("Select an option: ");
                    int subOption = Convert.ToInt32(Console.ReadLine());
                    if (subOption == 1)
                    {
                        Console.WriteLine("Course ID ?");
                        int    id     = Convert.ToInt32(Console.ReadLine());
                        Course course = bl.GetCourseByID(id);
                        if (course == null)
                        {
                            Console.WriteLine("Course not found");
                        }
                        else
                        {
                            Console.WriteLine("Enter new course name: ");
                            course.CourseName = Console.ReadLine();

                            IEnumerable <Teacher> allTeachers = bl.GetAllTeachers();
                            Console.WriteLine("");
                            Console.WriteLine("Current available Teachers");
                            foreach (Teacher temp in allTeachers)
                            {
                                Console.WriteLine(temp.TeacherId + "  " + temp.TeacherName);
                            }

                            Console.WriteLine("Current teacher id is " + course.TeacherId + " . Please enter new teacher id: ");
                            int  idTemp = Convert.ToInt32(Console.ReadLine());
                            bool flag   = false;
                            foreach (Teacher temp in allTeachers)
                            {
                                if (idTemp == temp.TeacherId)
                                {
                                    flag = true;
                                }
                            }
                            if (flag == true)
                            {
                                course.TeacherId = idTemp;
                                bl.UpdateCourse(course);
                            }
                            else
                            {
                                Console.WriteLine("Teacher ID is invalid");
                            }
                        }
                    }
                    else if (subOption == 2)
                    {
                        Console.WriteLine("Course Name ?");
                        string name       = Console.ReadLine();
                        Course tempCourse = bl.GetCourseByName(name);
                        if (tempCourse == null)
                        {
                            Console.WriteLine("Course not found!");
                        }
                        else
                        {
                            int    id     = tempCourse.CourseId;
                            Course course = bl.GetCourseByID(id);
                            if (course == null)
                            {
                                Console.WriteLine("Course not found!");
                            }
                            else
                            {
                                Console.WriteLine("Enter new course name: ");
                                course.CourseName = Console.ReadLine();
                                IEnumerable <Teacher> allTeachers = bl.GetAllTeachers();
                                Console.WriteLine("");
                                Console.WriteLine("Current available Teachers");
                                foreach (Teacher temp in allTeachers)
                                {
                                    Console.WriteLine(temp.TeacherId + "  " + temp.TeacherName);
                                }

                                Console.WriteLine("Current teacher id is " + course.TeacherId + " . Please enter new teacher id: ");
                                int  idTemp = Convert.ToInt32(Console.ReadLine());
                                bool flag   = false;
                                foreach (Teacher temp in allTeachers)
                                {
                                    if (idTemp == temp.TeacherId)
                                    {
                                        flag = true;
                                    }
                                }
                                if (flag == true)
                                {
                                    course.TeacherId = idTemp;
                                    bl.UpdateCourse(course);
                                }
                                else
                                {
                                    Console.WriteLine("Teacher ID is invalid");
                                }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Input is invalid");
                    }
                }
                else if (optionInt == 8)
                {
                    Console.WriteLine("Course ID to be deleted?");
                    int id = Convert.ToInt32(Console.ReadLine());

                    Course course = bl.GetCourseByID(id);
                    if (course == null)
                    {
                        Console.WriteLine("Course not found!");
                    }
                    else
                    {
                        bl.RemoveCourse(course);
                    }
                }
                else if (optionInt == 9)
                {
                    IEnumerable <Course> allCourses = bl.GetAllCourses();
                    Console.WriteLine();
                    Console.WriteLine($"\t  {"ID",-5}" + $"{"Course name",-15}" + $"{"TeacherID",-15}");
                    //Console.WriteLine("ID Course Name  TeacherID");
                    foreach (Course temp in allCourses)
                    {
                        Console.WriteLine($"\t  {temp.CourseId,-5}" + $"{temp.CourseName,-15}" + $"{temp.TeacherId,-15}");
                        //Console.WriteLine(temp.CourseId + "  " + temp.CourseName+ "       " + temp.TeacherId);
                    }
                    Console.WriteLine();
                }
                else if (optionInt == 0)
                {
                    run = false;
                }
                else
                {
                    Console.WriteLine("Input is invalid, please re-enter the option from 0-9");
                }
            } //end while
        }