Esempio n. 1
0
        /// <summary>
        ///     Simplify address
        /// </summary>
        /// <remarks>
        ///     This basically is intended to remove empty lines from the address fields and push the remainder up.
        ///     It can mess up the consistency for named fields but that is always a risk with user input
        ///     Postcode is ignored - previously it may have been added to the last line
        /// </remarks>
        public void Simplify()
        {
            var dummyLine = "##DUMMY ADDRESS LINE##";

            // first tidy up
            Address1 = Address1 == null ? "" : Address1.Trim();
            Address2 = Address2 == null ? "" : Address2.Trim();
            Address3 = Address3 == null ? "" : Address3.Trim();
            Address4 = Address4 == null ? "" : Address4.Trim();
            Address5 = Address5 == null ? "" : Address5.Trim();
            Address6 = Address6 == null ? "" : Address6.Trim();

            // check for house number
            // we only check the first two addresses and deal if needed

            // house number is intended to be in address 2 but it could be swapped so we check here

            var houseNameIsNumeric          = Address1.Trim().IsLong();
            var houseNameIsPartialNumeric   = Address1.Trim().FirstWord().IsLong();
            var houseNumberIsNumeric        = Address2.Trim().IsLong();
            var houseNumberIsPartialNumeric = Address2.Trim().FirstWord().IsLong();

            if (houseNameIsNumeric)
            {
                long.TryParse(Address1, out var houseNumber);
                // address1 is fully numeric so we swap
                Address1 = Address2 == null || Address2.Trim() == "" ? dummyLine : Address2;
                Address2 = houseNumber.ToString();
            }
            else
            {
                if (houseNameIsPartialNumeric)
                {
                    // address1 is partial numeric, if address2 is empty we move the number
                    if (Address2 == null || Address2.Trim() == "")
                    {
                        long.TryParse(Address1.Trim().FirstWord(), out var houseNumber);
                        Address1 = Address1.Substring(houseNumber.ToString().Length);
                        Address2 = houseNumber.ToString();
                    }
                }
                else
                {
                    if (houseNumberIsNumeric &&
                        (Address1 == null || Address1.Trim() == ""))
                    {
                        // housenumber is numeric and house nameis empty so we add dummy line to prevent losing empty line which will throw out the order
                        Address1 = dummyLine;
                    }
                    else
                    {
                        if (houseNumberIsPartialNumeric &&
                            (Address1 == null || Address1.Trim() == ""))
                        {
                            // housenumber contains a partial numeric e.g. 32 High street so we split it out
                            long.TryParse(Address2.Trim().FirstWord(),
                                          out var house2Number);
                            Address1 = Address2.Substring(house2Number.ToString().Length);
                            Address2 = house2Number.ToString();
                        }
                    }
                }
            }

            var tempFullAddress = FullAddress(Address1, Address2, Address3, Address4, Address5, Address6,
                                              false);

            // remove any redundancy so that we have a comma separated string
            tempFullAddress.ReplaceAllMid(",,", "", 0, tempFullAddress.Length);

            // now split on comma
            var tempAddress = tempFullAddress.Split(',');

            var tempAddressLen = tempAddress.Length;

            switch (tempAddressLen)
            {
            case 1:
                Address1 = tempAddress[0].Trim();
                Address2 = "";
                Address3 = "";
                Address4 = "";
                Address5 = "";
                Address6 = "";
                break;

            case 2:
                Address1 = tempAddress[0].Trim();
                Address2 = tempAddress[1].Trim();

                Address3 = "";
                Address4 = "";
                Address5 = "";
                Address6 = "";
                break;

            case 3:
                Address1 = tempAddress[0].Trim();
                Address2 = tempAddress[1].Trim();
                Address3 = tempAddress[2].Trim();
                Address4 = "";
                Address5 = "";
                Address6 = "";
                break;

            case 4:
                Address1 = tempAddress[0].Trim();
                Address2 = tempAddress[1].Trim();
                Address3 = tempAddress[2].Trim();
                Address4 = tempAddress[3].Trim();
                Address5 = "";
                Address6 = "";
                break;


            case 5:
                Address1 = tempAddress[0].Trim();
                Address2 = tempAddress[1].Trim();
                Address3 = tempAddress[2].Trim();
                Address4 = tempAddress[3].Trim();
                Address5 = tempAddress[4].Trim();
                Address6 = "";

                break;

            case 6:
                Address1 = tempAddress[0].Trim();
                Address2 = tempAddress[1].Trim();
                Address3 = tempAddress[2].Trim();
                Address4 = tempAddress[3].Trim();
                Address5 = tempAddress[4].Trim();
                Address6 = tempAddress[5].Trim();

                break;

            default:
                Address1 = tempAddress[0].Trim();
                Address2 = tempAddress[1].Trim();
                Address3 = tempAddress[2].Trim();
                Address4 = tempAddress[3].Trim();
                Address5 = tempAddress[4].Trim();
                Address6 = tempAddress[5].Trim();

                break;
            }

            // now remove dummy if it exists
            if (Address1 == dummyLine)
            {
                Address1 = "";
            }
        }