示例#1
0
        /// <summary>
        /// Reads valid data to create a new student and save it to the database
        /// </summary>
        private static void CreateStudent()
        {
            StudentType type      = StudentType.Kinder;
            bool        validType = false;

            Console.WriteLine("Specify the student type (K)inder, (E)lementary, (H)igh, (U)niversity; introducing the letter enclosed in parenthesis and hitting enter: ");
            string answer = Console.ReadLine();

            if (!string.IsNullOrEmpty(answer))
            {
                answer = answer[0].ToString().ToUpper();
                switch (answer)
                {
                case "K":
                    validType = true;
                    break;

                case "E":
                    validType = true;
                    type      = StudentType.Elementary;
                    break;

                case "H":
                    validType = true;
                    type      = StudentType.High;
                    break;

                case "U":
                    validType = true;
                    type      = StudentType.University;
                    break;
                }
            }

            string name = string.Empty;

            Console.WriteLine("Please introduce the student's name, it must no be empty: ");
            name = Console.ReadLine();

            Console.WriteLine("Specify the student gender (M)ale, (F)emale; introducing the letter enclosed in parenthesis and hitting enter: ");

            string gender = string.Empty;

            gender = Console.ReadLine();
            if (!string.IsNullOrEmpty(gender))
            {
                gender = gender.Trim();
                if (gender.Length > 0)
                {
                    gender = gender[0].ToString().ToUpper();
                }
            }

            if (validType && !string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(gender) && (gender == "M" || gender == "F"))
            {
                Student student = new Student();
                student.Type      = type;
                student.Name      = name;
                student.Gender    = gender == "M" ? "Male" : "Female";
                student.Enabled   = true;
                student.UpdatedOn = DateTime.Now;
                if (!StudentMapper.Exists(student))
                {
                    if (StudentMapper.Insert(student))
                    {
                        ShowContinueMessage("The student was added successfully.");
                    }
                    else
                    {
                        ShowContinueMessage("We found and error adding the student please review logs and try again.");
                    }
                }
                else
                {
                    ShowContinueMessage("There is already a student with the same data added, we don't allow duplicates. You can search it for edit if you want.");
                }
            }
            else
            {
                ShowContinueMessage("There is an error with your data. Please verify that the Type is K for Kinder, E for Elementary, H for High, U for University, The name is not empty and Gender is M for Male or F for Female.");
            }
        }