private void BtnAdd_Click(object sender, EventArgs e) { Employee careTaker = new CareTaker(); Animal animal = null; bool canCreate = false; try { // Split IDAndName property and take only ID of Care Taker string IDAndName = cboCareTaker.Text; string[] caretakerID = IDAndName.Split(null); foreach (Employee t in Employee.Employees) { if (t.ID == caretakerID[0]) { careTaker = (CareTaker)t; break; } } var tID = MyString.FirstLetterToUpper(txtID.Text); var tName = MyString.FirstLetterToUpper(txtName.Text); var tGender = (Gender._Gender)cboGender.SelectedItem; var tAge = new Age() { Year = int.Parse(txtAgeYear.Text), Month = int.Parse(cboAgeMonth.Text) }; var tWeight = double.Parse(txtWeight.Text); var tTypeOfFood = MyString.FirstLetterToUpper(txtTypeOfFood.Text); var tRegion = (MyRegion.From)cboRegion.SelectedItem; var tCageType = (Cage.Type)cboCageType.SelectedItem; var tCareTaker = (CareTaker)careTaker; // If ID already have foreach (Animal aml in Animal.Animals) { if (aml.ID.Equals(tID)) { throw new Exception(string.Format("Duplicate ID: {0}", tID)); } } canCreate = true; if (cboSpecies.Text == "Lion") { animal = new Lion(); } if (cboSpecies.Text == "Tiger") { animal = new Tiger(); } if (cboSpecies.Text == "Elephant") { animal = new Elephant(); } if (cboSpecies.Text == "Penguin") { animal = new Penguin(); } if (cboSpecies.Text == "Peocock") { animal = new Peocock(); } if (cboSpecies.Text == "Cichild") { animal = new Cichild(); } animal.ID = tID; animal.Name = tName; animal.Gender = tGender; animal._Age = tAge; animal._Weight = tWeight; animal.TypeOfFood = tTypeOfFood; animal._Region = tRegion; animal.CageType = tCageType; animal.CareTaker = tCareTaker; } catch (Exception ex) { MessageBox.Show(ex.Message); } if (canCreate) { if (Create != null) { Create(this, animal); } MessageBox.Show("Record Added"); ResetControl(); } }
private void BtnUpdate_Click(object sender, EventArgs e) { Employee careTaker = new CareTaker(); Animal animal = null; bool canUpdate = false; try { // Split IDAndName property and take only ID of Care Taker string IDAndName = cboCareTaker.Text; string[] caretakerID = IDAndName.Split(null); foreach (Employee t in Employee.Employees) { if (t.ID == caretakerID[0]) { careTaker = (CareTaker)t; break; } } var tID = MyString.FirstLetterToUpper(txtID.Text); var tName = MyString.FirstLetterToUpper(txtName.Text); var tGender = (Gender._Gender)cboGender.SelectedItem; var tAge = new Age() { Year = int.Parse(txtAgeYear.Text), Month = int.Parse(cboAgeMonth.Text) }; var tWeight = double.Parse(txtWeight.Text); var tTypeOfFood = MyString.FirstLetterToUpper(txtTypeOfFood.Text); var tRegion = (MyRegion.From)cboRegion.SelectedItem; var tCageType = (Cage.Type)cboCageType.SelectedItem; var tCareTaker = (CareTaker)careTaker; var tConservationStatus = (Conservation.Status)cboConservationStatus.SelectedIndex; canUpdate = true; if (cboSpecies.Text == "Lion") { animal = new Lion(); } if (cboSpecies.Text == "Tiger") { animal = new Tiger(); } if (cboSpecies.Text == "Elephant") { animal = new Elephant(); } if (cboSpecies.Text == "Penguin") { animal = new Penguin(); } if (cboSpecies.Text == "Peocock") { animal = new Peocock(); } if (cboSpecies.Text == "Cichild") { animal = new Cichild(); } animal.ID = tID; animal.Name = tName; animal.Gender = tGender; animal._Age = tAge; animal._Weight = tWeight; animal.TypeOfFood = tTypeOfFood; animal.ConservationStatus = tConservationStatus; animal._Region = tRegion; animal.CageType = tCageType; animal.CareTaker = tCareTaker; } catch (Exception ex) { MessageBox.Show(ex.Message); } if (canUpdate) { if (Modified != null) { Modified(this, animal); } MessageBox.Show("Record Updated"); } }