Beispiel #1
0
        /// <summary>
        /// Validate and add animal on Add animal button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddAnimal_Click(object sender, EventArgs e)
        {
            {
                //send animal info for validation and receive errorText.
                string errorText = AnimalMaker.ValidateInput(txtName.Text, txtAge.Text, lstGender.SelectedIndex,
                                                             lstSpecies.Text, txtAnimalCatInfo.Text, txtSpeciesInfo.Text);

                //if errorText is not empty, input is not valid.
                if (!string.IsNullOrEmpty(errorText))
                {
                    MessageBox.Show(errorText);
                }

                //if errorText is empty, input is valid.
                else
                {
                    bool notUsed = int.TryParse(txtAge.Text, out int ageInt);                                           //convert age from string to integer
                    var  animal  = AnimalMaker.MakeAnimal(txtName.Text, ageInt, lstGender.Text,
                                                          lstSpecies.Text, txtAnimalCatInfo.Text, txtSpeciesInfo.Text); //send info to AnimalMaker for creation of animal
                    bool addOK = animalManagerObj.AddAnimal(animal);                                                    //send animal to AnimalManager to be added
                    if (addOK)
                    {
                        UpdateAnimalList(); //display animal in Animal list
                        InitializeGui();    //clear form
                    }
                    else
                    {
                        MessageBox.Show("Animal not added");
                    }
                }
            }
        }