Exemplo n.º 1
0
        private void BtnSaveEdits_Click(object sender, EventArgs e)
        {
            if (ValidatedUserEnteredData())
            {
                string confirmationPrompt = "Are you sure you want to save the changes you made to the selected animal from the system?\n\n";

                DialogResult confirmation = MessageBox.Show(confirmationPrompt, "Important!!!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (confirmation == DialogResult.Yes)
                {
                    StreamWriter outStream = new StreamWriter(AnimalDataFileLocation(selectedGroup), false);

                    switch (selectedGroup)
                    {
                    case GroupSelection.Birds:
                        Bird editedBird = new Bird
                        {
                            Name              = txtBxName.Text,
                            Age               = int.Parse(txtBxAge.Text),
                            Gender            = cmbBxGender.SelectedItem.ToString(),
                            ZooLocation       = txtBxLocation.Text,
                            Type              = txtBxType.Text,
                            DietType          = cmbBxDietType.SelectedItem.ToString(),
                            HowTheAnimalMoves = txtBxMovement.Text,
                            IsTagged          = Reptile.ConvertYesNoIntoBool(cmbBxIsTagged.SelectedItem.ToString())
                        };

                        theAnimals[lstBxAnimalNames.SelectedIndex] = editedBird;

                        foreach (Bird aBird in theAnimals)
                        {
                            outStream.WriteLine(aBird.Name);
                            outStream.WriteLine(aBird.Age);
                            outStream.WriteLine(aBird.Gender);
                            outStream.WriteLine(aBird.HowTheAnimalMoves);
                            outStream.WriteLine(aBird.ZooLocation);
                            outStream.WriteLine(aBird.Type);
                            outStream.WriteLine(aBird.DietType);
                            outStream.WriteLine(Reptile.ConvertBoolIntoYesNo(aBird.IsTagged));
                            outStream.WriteLine();
                        }
                        break;

                    case GroupSelection.Reptiles:
                        Reptile editedReptile = new Reptile
                        {
                            Name              = txtBxName.Text,
                            Age               = int.Parse(txtBxAge.Text),
                            Gender            = cmbBxGender.SelectedItem.ToString(),
                            ZooLocation       = txtBxLocation.Text,
                            Type              = txtBxType.Text,
                            DietType          = cmbBxDietType.SelectedItem.ToString(),
                            HowTheAnimalMoves = txtBxMovement.Text,
                            HasAShell         = Reptile.ConvertYesNoIntoBool(cmbBxHasAShell.SelectedItem.ToString()),
                            LivesIndoors      = Reptile.ConvertYesNoIntoBool(cmbBxLivesIndoors.SelectedItem.ToString())
                        };

                        theAnimals[lstBxAnimalNames.SelectedIndex] = editedReptile;

                        foreach (Reptile aReptile in theAnimals)
                        {
                            outStream.WriteLine(aReptile.Name);
                            outStream.WriteLine(aReptile.Age);
                            outStream.WriteLine(aReptile.Gender);
                            outStream.WriteLine(aReptile.HowTheAnimalMoves);
                            outStream.WriteLine(aReptile.ZooLocation);
                            outStream.WriteLine(aReptile.Type);
                            outStream.WriteLine(aReptile.DietType);
                            outStream.WriteLine(Reptile.ConvertBoolIntoYesNo(aReptile.HasAShell));
                            outStream.WriteLine(Reptile.ConvertBoolIntoYesNo(aReptile.LivesIndoors));
                            outStream.WriteLine();
                        }
                        break;

                    case GroupSelection.Mammals:
                        Mammal editedMammal = new Mammal
                        {
                            Name              = txtBxName.Text,
                            Age               = int.Parse(txtBxAge.Text),
                            Gender            = cmbBxGender.SelectedItem.ToString(),
                            ZooLocation       = txtBxLocation.Text,
                            Type              = txtBxType.Text,
                            DietType          = cmbBxDietType.SelectedItem.ToString(),
                            HowTheAnimalMoves = txtBxMovement.Text,
                            Weight            = double.Parse(txtBxWeight.Text),
                            DailyMealCount    = int.Parse(cmbBxDailyMeal.Text)
                        };

                        theAnimals[lstBxAnimalNames.SelectedIndex] = editedMammal;

                        foreach (Mammal aMammal in theAnimals)
                        {
                            outStream.WriteLine(aMammal.Name);
                            outStream.WriteLine(aMammal.Age);
                            outStream.WriteLine(aMammal.Gender);
                            outStream.WriteLine(aMammal.HowTheAnimalMoves);
                            outStream.WriteLine(aMammal.ZooLocation);
                            outStream.WriteLine(aMammal.Type);
                            outStream.WriteLine(aMammal.DietType);
                            outStream.WriteLine(aMammal.Weight);
                            outStream.WriteLine(aMammal.DailyMealCount);
                            outStream.WriteLine();
                        }
                        break;

                    default:
                        Animal editedAnimal = new Animal
                        {
                            Name              = txtBxName.Text,
                            Age               = int.Parse(txtBxAge.Text),
                            Gender            = cmbBxGender.SelectedItem.ToString(),
                            ZooLocation       = txtBxLocation.Text,
                            Type              = txtBxType.Text,
                            DietType          = cmbBxDietType.SelectedItem.ToString(),
                            HowTheAnimalMoves = txtBxMovement.Text
                        };

                        theAnimals[lstBxAnimalNames.SelectedIndex] = editedAnimal;

                        foreach (Animal anAnimal in theAnimals)
                        {
                            outStream.WriteLine(anAnimal.Name);
                            outStream.WriteLine(anAnimal.Age);
                            outStream.WriteLine(anAnimal.Gender);
                            outStream.WriteLine(anAnimal.HowTheAnimalMoves);
                            outStream.WriteLine(anAnimal.ZooLocation);
                            outStream.WriteLine(anAnimal.Type);
                            outStream.WriteLine(anAnimal.DietType);
                            outStream.WriteLine();
                        }
                        break;
                    }

                    outStream.Close();
                }
            }
        }
        private void BtnDelete_Click(object sender, EventArgs e)
        {
            if (lstBxAnimalNames.SelectedIndex == -1)
            {
                MessageBox.Show("No animal was selected for deletion.", "Nothing Selected", MessageBoxButtons.OK, MessageBoxIcon.None);
            }
            else
            {
                string confirmationPrompt = "Are you sure you want to delete the selected animal from the system?\n\n" +
                                            "THIS ACTION IS IRREVERSIBLE!!!";

                DialogResult confirmation = MessageBox.Show(confirmationPrompt, "Important!!!", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);

                if (confirmation == DialogResult.Yes)
                {
                    int indexHolder = lstBxAnimalNames.SelectedIndex;
                    lstBxAnimalNames.Items.RemoveAt(indexHolder);
                    theAnimals.RemoveAt(indexHolder);
                    lstBxAnimalNames.SelectedIndex = -1;
                    txtBxAnimalInfo.Text           = "";

                    StreamWriter outStream = new StreamWriter(AnimalDataFileLocation(selectedGroup), false);

                    switch (selectedGroup)
                    {
                    case GroupSelection.Birds:
                        foreach (Bird aBird in theAnimals)
                        {
                            outStream.WriteLine(aBird.Name);
                            outStream.WriteLine(aBird.Age);
                            outStream.WriteLine(aBird.Gender);
                            outStream.WriteLine(aBird.HowTheAnimalMoves);
                            outStream.WriteLine(aBird.ZooLocation);
                            outStream.WriteLine(aBird.Type);
                            outStream.WriteLine(aBird.DietType);
                            outStream.WriteLine(Reptile.ConvertBoolIntoYesNo(aBird.IsTagged));
                            outStream.WriteLine();
                        }
                        break;

                    case GroupSelection.Reptiles:
                        foreach (Reptile aReptile in theAnimals)
                        {
                            outStream.WriteLine(aReptile.Name);
                            outStream.WriteLine(aReptile.Age);
                            outStream.WriteLine(aReptile.Gender);
                            outStream.WriteLine(aReptile.HowTheAnimalMoves);
                            outStream.WriteLine(aReptile.ZooLocation);
                            outStream.WriteLine(aReptile.Type);
                            outStream.WriteLine(aReptile.DietType);
                            outStream.WriteLine(Reptile.ConvertBoolIntoYesNo(aReptile.HasAShell));
                            outStream.WriteLine(Reptile.ConvertBoolIntoYesNo(aReptile.LivesIndoors));
                            outStream.WriteLine();
                        }
                        break;

                    case GroupSelection.Mammals:
                        foreach (Mammal aMammal in theAnimals)
                        {
                            outStream.WriteLine(aMammal.Name);
                            outStream.WriteLine(aMammal.Age);
                            outStream.WriteLine(aMammal.Gender);
                            outStream.WriteLine(aMammal.HowTheAnimalMoves);
                            outStream.WriteLine(aMammal.ZooLocation);
                            outStream.WriteLine(aMammal.Type);
                            outStream.WriteLine(aMammal.DietType);
                            outStream.WriteLine(aMammal.Weight);
                            outStream.WriteLine(aMammal.DailyMealCount);
                            outStream.WriteLine();
                        }
                        break;

                    default:
                        foreach (Animal anAnimal in theAnimals)
                        {
                            outStream.WriteLine(anAnimal.Name);
                            outStream.WriteLine(anAnimal.Age);
                            outStream.WriteLine(anAnimal.Gender);
                            outStream.WriteLine(anAnimal.HowTheAnimalMoves);
                            outStream.WriteLine(anAnimal.ZooLocation);
                            outStream.WriteLine(anAnimal.Type);
                            outStream.WriteLine(anAnimal.DietType);
                            outStream.WriteLine();
                        }
                        break;
                    }

                    outStream.Close();
                }
            }
        }
Exemplo n.º 3
0
        private void LstBxAnimalNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstBxAnimalNames.SelectedIndex == -1)
            {
                txtBxName.Text             = null;
                txtBxAge.Text              = null;
                cmbBxGender.SelectedItem   = null;
                txtBxLocation.Text         = null;
                txtBxType.Text             = null;
                cmbBxDietType.SelectedItem = null;
                txtBxMovement.Text         = null;

                switch (selectedGroup)
                {
                case GroupSelection.Birds:
                    cmbBxIsTagged.SelectedItem = null;
                    break;

                case GroupSelection.Reptiles:
                    cmbBxHasAShell.SelectedItem    = null;
                    cmbBxLivesIndoors.SelectedItem = null;
                    break;

                case GroupSelection.Mammals:
                    txtBxWeight.Text            = null;
                    cmbBxDailyMeal.SelectedItem = null;
                    break;
                }
            }
            else
            {
                Animal selectedAnimal = (Animal)theAnimals[lstBxAnimalNames.SelectedIndex];
                txtBxName.Text             = selectedAnimal.Name;
                txtBxAge.Text              = selectedAnimal.Age.ToString();
                cmbBxGender.SelectedItem   = selectedAnimal.Gender;
                txtBxLocation.Text         = selectedAnimal.ZooLocation;
                txtBxType.Text             = selectedAnimal.Type;
                cmbBxDietType.SelectedItem = selectedAnimal.DietType;
                txtBxMovement.Text         = selectedAnimal.HowTheAnimalMoves;

                switch (selectedGroup)
                {
                case GroupSelection.Birds:
                    Bird selectedBird = (Bird)selectedAnimal;
                    cmbBxIsTagged.SelectedItem = Reptile.ConvertBoolIntoYesNo(selectedBird.IsTagged);
                    break;

                case GroupSelection.Reptiles:
                    Reptile selectedReptile = (Reptile)selectedAnimal;
                    cmbBxHasAShell.SelectedItem    = Reptile.ConvertBoolIntoYesNo(selectedReptile.HasAShell);
                    cmbBxLivesIndoors.SelectedItem = Reptile.ConvertBoolIntoYesNo(selectedReptile.LivesIndoors);
                    break;

                case GroupSelection.Mammals:
                    Mammal selectedMammal = (Mammal)selectedAnimal;
                    txtBxWeight.Text            = selectedMammal.Weight.ToString();
                    cmbBxDailyMeal.SelectedItem = selectedMammal.DailyMealCount.ToString();
                    break;
                }
            }
        }