Beispiel #1
0
        //Update the estate list. Triggered when program starts and when information has been updated. Also triggered when search parameters change.
        private void UpdateEstateList()
        {
            String        street = this.SearchBarStreet.Text;
            List <Estate> res    = Estates.ToList();

            string zip         = "";
            int    hiddenCount = 0;

            if (SearchBarZip.Text != null)
            {
                zip = this.SearchBarZip.Text;
            }
            String city    = this.SearchBarCity.Text;
            String country = "";

            if (this.SearchBoxCountry.SelectedIndex > 0)
            {
                country = this.SearchBoxCountry.Text;
                //Console.WriteLine(this.SearchBoxCountry.SelectedIndex);
                Countries c = EstateUtils.parseCountry(country);
                if (Estates.GetCountryDictionary().Contains(c))
                {
                    res = Estates.GetCountryDictionary().Get(c);
                }
            }
            String legal = "";

            if (this.SearchBoxLegal.SelectedIndex > 0)
            {
                legal = this.SearchBoxLegal.Text;
            }
            String type = "";

            if (this.SearchBoxType.SelectedIndex > 0)
            {
                type = this.SearchBoxType.Text;
            }


            EstateList.Items.Clear();
            EstateListItems = new LinkedList <string>();
            //This for-loop goes through every Estate in the list of Estates and checks wether the filter-attributes correspond to the attributes of the estate.
            //If they do they are added to the list, if not they are hidden from view.
            for (int i = 0; i < res.Count; i++)
            {
                Estate e = res[i];
                if (e.Address.Street.ToLower().Contains(street.ToLower()) && e.Address.Zip.ToString().Contains(zip) && e.Address.City.ToLower().Contains(city.ToLower()) && e.Address.country.ToString().Contains(country) && e.type.ToString().Contains(type) && e.GetLegalType().Contains(legal) && e.Sqrft >= EstateSqrftSlider.Value && e.Rent <= EstateRentSlider.Value)
                {
                    //Create a string representation of an Estate as such: ID : Street, City, Country.
                    EstateListItems.AddLast($"{e.Id} : {e.Address.Street} {e.Address.City} {e.Address.country}");
                    EstateList.Items.AddRange(new Object[]
                    {
                        EstateListItems.Last()
                    });
                }
                else
                {
                    hiddenCount++;
                }
            }
            if (Estates.Count == 0)
            {
                DeleteButton.Enabled = false;
            }
            else
            {
                DeleteButton.Enabled = true;
            }
            //Shows the user how many Estates are hidden from the list, so there's no confusion.
            NumberOfHiddenItems.Text = hiddenCount + " hidden items";
            //Resets the selected estate, because the order of estates have been shifted around.
            selectedEstate = -1;
            EnableInfoBoxes(false);
        }
Beispiel #2
0
        //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);
            }
        }