// Adds new estate to list private void AddEstate(Address address) { switch (selectedType) { case "Shop": estateManager.Add(new Shop(id, selectedCategory, selectedType, selectedLegalForm, address, selectedUniqueAttr)); break; case "Warehouse": estateManager.Add(new Warehouse(id, selectedCategory, selectedType, selectedLegalForm, address, selectedUniqueAttr)); break; case "House": estateManager.Add(new House(id, selectedCategory, selectedType, selectedLegalForm, address, selectedUniqueAttr)); break; case "Villa": estateManager.Add(new Villa(id, selectedCategory, selectedType, selectedLegalForm, address, selectedUniqueAttr)); break; case "Apartment": estateManager.Add(new Apartment(id, selectedCategory, selectedType, selectedLegalForm, address, selectedUniqueAttr)); break; case "Townhouse": estateManager.Add(new Townhouse(id, selectedCategory, selectedType, selectedLegalForm, address, selectedUniqueAttr)); break; } btnEdit.Enabled = true; btnDelete.Enabled = true; id++; }
//Update an Estate within the Estate list by creating a new one and replacing it, creates a new estate by adding it to the lists. private void EstateUpdateButtonClick(object sender, EventArgs e) { try { Estate res = null; int id = -1; int zip = -1; int rent = -1; int sqrft = -1; Address a = null; try { //Checks wether the ID is unique, or the currently selected estate has the same ID. If not it throws an InvalidIDException. id = VariablesCheck.AddNewId(this.EstateId.Text, ListManip.GetEstateFromList(EstateList.SelectedIndex, EstateListItems.ToArray(), Estates), Estates); } catch (DuplicateIDException) { //Informs the user of the issue. this.EditInfo.Text = "ID cannot be a duplicate. Changes not saved."; throw new DuplicateIDException(); } catch (StringNotIntException) { //Informs the user that an ID must only contain numbers. this.EditInfo.Text = "ID must only contain numbers 0 to 9"; throw new StringNotIntException(); } try { //Checks wether the ZIP contains only numbers. zip = VariablesCheck.CheckIfNumberFieldHasLetters(this.EstateZip.Text); } catch (StringNotIntException) { //Informs the user that a ZIP must only contain numbers. this.EditInfo.Text = "Zip must only contain numbers 0 to 9"; throw new StringNotIntException(); } try { //Checks wether the rent contains only numbers. rent = VariablesCheck.CheckIfNumberFieldHasLetters(this.EstateRent.Text); } catch (StringNotIntException) { //Informs the user that rent must only contain numbers. this.EditInfo.Text = "Rent must only contain numbers 0 to 9"; throw new StringNotIntException(); } try { //Checks wether the Square Feet attribute only contains numbers. sqrft = VariablesCheck.CheckIfNumberFieldHasLetters(this.EstateSqrft.Text); } catch (StringNotIntException) { //INforms the user that Square Feet must only contain numbers. this.EditInfo.Text = "Square Feet must only contain numbers 0 to 9"; throw new StringNotIntException(); } try { //Attempts to create a new Address attribute. If Street address contains any special characters the constructor throws a SpecialCharException. //If the City contains anything other than letters the constructor throws a SpecialCharException. a = new Address(this.EstateStreet.Text, zip, this.EstateCity.Text, EstateUtils.parseCountry(this.EstateCountryMenu.Text)); } catch (SpecialCharException) { //Informs the user that Street can only contain numbers and letters, and City can only contain letters. this.EditInfo.Text = "Street address must only contain letters A-Ö and numbers 0 to 9. And City can only contain letters A-Ö."; } //Saves the path and name of the selected image to a new variable. string imageLocation = this.DisplayImage.ImageLocation; //Checks wether the estate type is either Shop or Warehouse. if (EstateTypeMenu.Text == Estate.EstateType.Shop.ToString() || EstateTypeMenu.Text == Estate.EstateType.Warehouse.ToString()) { //Shop and Warehouse can't be of the legal type Tenement. if (EstateLegalMenu.Text == Estate.Legal.Tenement.ToString()) { //Informs the user of the issue mentioned above. this.EditInfo.Text = "Shops and Warehouses can't have Tenement as legal type."; throw new Exception(); } } //Depending on the type of estate the program creates different estates. if (this.EstateTypeMenu.Text == Estate.EstateType.House.ToString()) { int val = VariablesCheck.CheckIfNumberFieldHasLetters(UniqueEstateValue.Text); res = new House(id, sqrft, rent, new Address(this.EstateStreet.Text, zip, this.EstateCity.Text, EstateUtils.parseCountry(this.EstateCountryMenu.Text)), (Residential.Legal)Enum.Parse(typeof(Residential.Legal), this.EstateLegalMenu.Text), imageLocation, val); } else if (EstateTypeMenu.Text == Estate.EstateType.Apartment.ToString()) { int val = VariablesCheck.CheckIfNumberFieldHasLetters(UniqueEstateValue.Text); res = new Apartment(id, sqrft, rent, new Address(this.EstateStreet.Text, zip, this.EstateCity.Text, EstateUtils.parseCountry(this.EstateCountryMenu.Text)), (Residential.Legal)Enum.Parse(typeof(Residential.Legal), this.EstateLegalMenu.Text), imageLocation, val); } else if (EstateTypeMenu.Text == Estate.EstateType.Villa.ToString()) { int val = VariablesCheck.CheckIfNumberFieldHasLetters(UniqueEstateValue.Text); res = new Villa(id, sqrft, rent, new Address(this.EstateStreet.Text, zip, this.EstateCity.Text, EstateUtils.parseCountry(this.EstateCountryMenu.Text)), (Residential.Legal)Enum.Parse(typeof(Residential.Legal), this.EstateLegalMenu.Text), imageLocation, val); } else if (EstateTypeMenu.Text == Estate.EstateType.Rowhouse.ToString()) { int val = VariablesCheck.CheckIfNumberFieldHasLetters(UniqueEstateValue.Text); res = new Rowhouse(id, sqrft, rent, new Address(this.EstateStreet.Text, zip, this.EstateCity.Text, EstateUtils.parseCountry(this.EstateCountryMenu.Text)), (Residential.Legal)Enum.Parse(typeof(Residential.Legal), this.EstateLegalMenu.Text), imageLocation, val); } else if (EstateTypeMenu.Text == Estate.EstateType.Shop.ToString()) { int val = VariablesCheck.CheckIfNumberFieldHasLetters(UniqueEstateValue.Text); res = new Shop(id, sqrft, rent, new Address(this.EstateStreet.Text, zip, this.EstateCity.Text, EstateUtils.parseCountry(this.EstateCountryMenu.Text)), (Commercial.Legal)Enum.Parse(typeof(Commercial.Legal), this.EstateLegalMenu.Text), imageLocation, val); } else if (EstateTypeMenu.Text == Estate.EstateType.Warehouse.ToString()) { bool val = VariablesCheck.checkTrueFalse(UniqueEstateValue.Text); res = new Warehouse(id, sqrft, rent, new Address(this.EstateStreet.Text, zip, this.EstateCity.Text, EstateUtils.parseCountry(this.EstateCountryMenu.Text)), (Commercial.Legal)Enum.Parse(typeof(Commercial.Legal), this.EstateLegalMenu.Text), imageLocation, val); } //If the boolean createnew is set to false then the new estate replaces the selected item in the list. //If not, then it creates a new estate and adds it to the top of the list. if (!createNew) { Estates.ChangeAt(res, selectedEstate); EditInfo.Text = "Estate edited succesfully!"; } else { Estates.Add(res); selectedEstate = 0; EditInfo.Text = "Estate created succesfully!"; this.EstateUpdateButton.Text = "Update info"; createNew = false; } UpdateEstateList(); } catch (Exception _e) { Console.WriteLine(_e); } }