private void SaveStudentBtn_Click(object sender, EventArgs e)
        {
            //creates the new object and adds it to either the editted list or the new student list
            ErrorList    err            = new ErrorList();
            List <Grade> holdGrades     = new List <Grade>();
            Student      holdNewStu     = null;
            DormStudent  holdNewDormStu = null;
            bool         newStudent     = false;


            if (StudentsListBox.SelectedItem.ToString() == "New Student")
            {
                newStudent = true;
            }
            else
            {
                var hold = Dependency.CurrentStudents.Where(a => a.StudentID == StudentIDTextBox.Text);

                if (hold.Count() > 1)
                {
                    err.Errors.Add("Multiple Students found with the same ID.");
                }
                else if (hold.Count() < 1)
                {
                    err.Errors.Add("Error finding student with that ID.");
                    StudentsListBox.Items.Remove(StudentIDTextBox.Text);
                }
                else if (hold.Count() == 1)
                {
                    holdGrades = hold.First().Grades;
                }
            }


            if (err.hasErrors)
            {
                MessageBox.Show(err.CompileErrors(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }



            //Change to validation command
            CheckStudentDataGood(err, holdGrades);

            if (err.hasErrors)
            {
                MessageBox.Show(err.CompileErrors(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }

            try
            {
                if (NonDormRadio.Checked)
                {
                    holdNewStu        = new Student(StudentIDTextBox.Text, NameTextBox.Text);
                    holdNewStu.Grades = holdGrades;
                }
                else
                {
                    string mealPlan = "";

                    switch (MealPlanCombo.SelectedItem)
                    {
                    case ("B - Basic"):
                        mealPlan = "B";
                        break;

                    case ("M - Medium"):
                        mealPlan = "M";
                        break;

                    case ("H - High"):
                        mealPlan = "H";
                        break;

                    default:
                        break;
                    }

                    holdNewDormStu        = new DormStudent(StudentIDTextBox.Text, NameTextBox.Text, DormDropDown.SelectedItem.ToString(), mealPlan);
                    holdNewDormStu.Grades = holdGrades;
                }
            }
            catch (Exception ex)
            {
                err.Errors.Add(ex.Message);
            }


            if (err.hasErrors)
            {
                MessageBox.Show(err.CompileErrors(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }

            Student holdStu = null;

            if (DormRadio.Checked)
            {
                holdStu = holdNewDormStu;
            }
            else
            {
                holdStu = holdNewStu;
            }


            if (!newStudent)
            {
                Dependency.CurrentStudents.RemoveAll(a => a.StudentID == holdStu.StudentID);
                Dependency.CurrentStudents.Add(holdStu);

                if (Dependency.EditStudents.Any(a => a.StudentID == holdStu.StudentID))
                {
                    Dependency.EditStudents.RemoveAll(a => a.StudentID == holdStu.StudentID);
                    Dependency.EditStudents.Add(holdStu);
                }
                else
                {
                    Dependency.EditStudents.Add(holdStu);
                }

                Dependency.CurrentStudents.Sort();
                Dependency.EditStudents.Sort();
            }
            else
            {
                Dependency.CurrentStudents.Add(holdStu);
                Dependency.NewStudents.Add(holdStu);

                Dependency.CurrentStudents.Sort();
                Dependency.NewStudents.Sort();
            }

            ResetForm();
            ReturnBtn.Focus();
        }
示例#2
0
        //Update button
        private void updateBtn_Click(object sender, EventArgs e)
        {
            try
            {
                //If commuter button is checked, check that the name to update is not missing
                if (commBtn.Checked)
                {
                    if (nameBox.Text == "")
                    {
                        errorBox.Text = "Name is missing!";
                        return;
                    }

                    //Replace index found thru the Id or name value with the new updated object and re-print updated listbox
                    studentList[finalIndex] = new Student(id: Int32.Parse(idBox.Text), name: nameBox.Text);
                    stuListBox.Clear();
                    studentList.Sort();
                    foreach (var item in studentList)
                    {
                        stuListBox.Text += item.ToString() + Environment.NewLine;
                    }
                }

                //If dorm button is checked, check that the name to update is not missing
                if (dormBtn.Checked)
                {
                    if (nameBox.Text == "")
                    {
                        errorBox.Text = "Name is missing!";
                        return;
                    }

                    //Check that dorm and meal plan are selected before sending update
                    if (dorm == "" || meal == "")
                    {
                        errorBox.Text = "You must select the dorm and meal plan for this student!";
                        return;
                    }
                    else
                    {
                        //Replace index found thru the Id or name value with the new updated child object and re-print updated listbox
                        studentList[finalIndex] = new DormStudent(id: Int32.Parse(idBox.Text), name: nameBox.Text, dorm: dorm, mealplan: meal);
                        stuListBox.Clear();
                        studentList.Sort();
                        foreach (var item in studentList)
                        {
                            stuListBox.Text += item.ToString() + Environment.NewLine;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is ArgumentOutOfRangeException || ex is FormatException)
                {
                    errorBox.Text = "Student not found!";
                    return;
                }
            }

            errorBox.Text = "Student Updated!";

            //Reset index, textboxes and vars
            finalIndex   = -1;
            idBox.Text   = "";
            nameBox.Text = "";
            meal         = "";
            dorm         = "";

            //Clean radio buttons after update
            foreach (RadioButton btn in new RadioButton[] { oakBtn, trusteeBtn, wapelloBtn, appaBtn, mahaBtn, basicBtn, mediumBtn, highBtn })
            {
                btn.Checked = false;
            }
        }
        private void StudentsListBox_SelectedValueChanged(object sender, EventArgs e)
        {
            StudentIDTextBox.Enabled = true;
            ErrorList err = new ErrorList();

            if (StudentsListBox.SelectedItem.ToString() == "New Student")
            {
                StudentIDTextBox.Enabled   = true;
                NonDormRadio.Checked       = true;
                DormDropDown.Enabled       = false;
                MealPlanCombo.Enabled      = false;
                DormDropDown.SelectedItem  = "Waffle";
                MealPlanCombo.SelectedItem = "B - Basic";
                StudentIDTextBox.Text      = "";
                NameTextBox.Text           = "";
            }
            else
            {
                //Need to search the current students list for that

                if (Dependency.CurrentStudents.Any(x => x.StudentID == StudentsListBox.SelectedItem.ToString()))
                {
                    try
                    {
                        var holdList = Dependency.CurrentStudents.Where(x => x.StudentID == StudentsListBox.SelectedItem.ToString());

                        if (holdList.Count() != 1)
                        {
                            if (holdList.Count() < 1)
                            {
                                throw new StudentIDNotFoundException("No student found with that ID.");
                            }
                            else if (holdList.Count() > 1)
                            {
                                throw new InvalidStudentIDException("Two students found with the same ID. Please contact IT immediately.");
                            }
                        }
                        else if (holdList.Count() == 1)
                        {
                            var hold = holdList.First();

                            if (hold is DormStudent)
                            {
                                DormStudent stu = (DormStudent)hold;

                                StudentIDTextBox.Enabled = false;
                                StudentIDTextBox.Text    = stu.StudentID;
                                NameTextBox.Text         = stu.StudentName;

                                DormRadio.Checked         = true;
                                NonDormRadio.Checked      = false;
                                DormDropDown.SelectedItem = stu.DormLocation;

                                switch (stu.MealPlan)
                                {
                                case ("B"):
                                    MealPlanCombo.SelectedItem = "B - Basic";
                                    break;

                                case ("M"):
                                    MealPlanCombo.SelectedItem = "M - Medium";
                                    break;

                                case ("H"):
                                    MealPlanCombo.SelectedItem = "H - High";
                                    break;

                                default:
                                    MealPlanCombo.SelectedItem = "B - Basic";
                                    break;
                                }
                            }
                            else if (hold is Student)
                            {
                                Student stu = (Student)hold;

                                StudentIDTextBox.Enabled = false;
                                StudentIDTextBox.Text    = stu.StudentID;
                                NameTextBox.Text         = stu.StudentName;

                                DormRadio.Checked          = false;
                                NonDormRadio.Checked       = true;
                                DormDropDown.SelectedItem  = "Waffle";
                                MealPlanCombo.SelectedItem = "B - Basic";
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        err.Errors.Add(ex.Message);
                    }
                }
            }
        }