Пример #1
0
        public void RandomUsZipWithRespectToPopTest()
        {
            var testResult = UsCityStateZip.RandomAmericanPartialZipCode();

            Assert.IsNotNull(testResult);

            for (var i = 0; i < 100; i++)
            {
                testResult = UsCityStateZip.RandomAmericanPartialZipCode();
                Assert.IsNotNull(testResult);
                Console.WriteLine(testResult);
            }
        }
Пример #2
0
        public static UsCityStateZip RandomAmericanCity(string zipCodePrefix = null, bool pickSuburbAtRandom = true)
        {
            const string HAS_HIGH_SCHOOL = "has-high-school";
            const string VALUE           = "value";
            //set defaults
            var ctz = new AddressData
            {
                PostalCode   = $"{UsCityStateZip.DF_ZIPCODE_PREFIX}{Etx.RandomInteger(1, 99):00}",
                RegionAbbrev = UsCityStateZip.DF_STATE_ABBREV
            };

            //pick a zip code prefix at random
            if (String.IsNullOrWhiteSpace(zipCodePrefix))
            {
                zipCodePrefix = UsCityStateZip.RandomAmericanPartialZipCode() ?? UsCityStateZip.DF_ZIPCODE_PREFIX;
            }

            //x-ref it to the zip code data
            var xpathString = $"//{UsCityStateZip.ZIP_CODE_PLURAL}//{UsCityStateZip.ZIP_CODE_SINGULAR}[@{PREFIX}='{zipCodePrefix}']";

            UsCityStateZip.UsZipCodeXml = UsCityStateZip.UsZipCodeXml ??
                                          XmlDocXrefIdentifier.GetEmbeddedXmlDoc(UsCityStateZip.US_ZIP_CODE_DATA, Assembly.GetExecutingAssembly());
            if (UsCityStateZip.UsZipCodeXml == null)
            {
                return(null);
            }
            var randZipCode = UsCityStateZip.UsZipCodeXml.SelectSingleNode(xpathString);

            if (randZipCode?.ParentNode?.Attributes?[NAME] == null)
            {
                ctz.Locality = UsCityStateZip.DF_CITY_NAME;
                return(new UsCityStateZip(ctz));
            }

            //get the containing us state
            ctz.RegionName = randZipCode.ParentNode.Attributes[NAME].Value;
            var nfState = UsState.GetStateByName(ctz.RegionName) ??
                          UsState.GetStateByPostalCode(UsCityStateZip.DF_STATE_ABBREV);

            ctz.RegionAbbrev = nfState.StateAbbrev ?? UsCityStateZip.DF_STATE_ABBREV;
            ctz.SortingCode  = $"{Etx.MyRand.Next(1, 9999):0000}";

            if (!randZipCode.HasChildNodes)
            {
                ctz.PostalCode = $"{zipCodePrefix}{Etx.RandomInteger(1, 99):00}";
            }
            else
            {
                //pick a particular zip-code (ZIP5)
                var zipCodes =
                    randZipCode.ChildNodes.Cast <XmlElement>()
                    .Where(
                        x =>
                        x.Attributes[HAS_HIGH_SCHOOL] != null &&
                        x.Attributes[HAS_HIGH_SCHOOL].Value == Boolean.TrueString)
                    .Select(x => x.Attributes[VALUE].Value).ToArray();
                if (zipCodes.Length <= 0)
                {
                    return(new UsCityStateZip(ctz));
                }
                var pickNum = Etx.RandomInteger(0, zipCodes.Length - 1);
                ctz.PostalCode = zipCodes[pickNum];
            }
            var rr = new UsCityStateZip(ctz);

            rr.SetAddrDataToXmlValues(pickSuburbAtRandom);
            return(rr);
        }