コード例 #1
0
        public Citizen(string firstName, string lastName, DateTime dateOfBirth, Gender gender)
        {
            this.firstName = firstName.Transform(To.LowerCase, To.TitleCase);
            this.lastName  = lastName.Transform(To.LowerCase, To.TitleCase);

            TimeSpan liveTime = SystemDateTime.Now() - dateOfBirth;

            if (liveTime.Days >= 0)
            {
                this.dateOfBirth = dateOfBirth.Date;
            }
            else
            {
                throw new ArgumentException(String.Format("Person who was not born cannot be added. The passed date is {0}", dateOfBirth), "dateOfBirth");
            }

            if (Enum.IsDefined(typeof(Gender), gender))
            {
                this.gender = gender;
            }
            else
            {
                throw new ArgumentOutOfRangeException("gender", "Passed improper enum element");
            }
        }
コード例 #2
0
        public void Register(ICitizen citizen)
        {
            if (this.citizens.Contains(citizen, new Helpers.CitizenEqualsComparer()))
            {
                throw new InvalidOperationException("You already add this citizen in registry.");
            }

            var newCitizen = new Citizen(citizen.FirstName, citizen.LastName, citizen.BirthDate, citizen.Gender);

            if (string.IsNullOrEmpty(citizen.VatId))
            {
                citizen.VatId = newCitizen.VatId = this.CreateId(newCitizen);
            }
            else
            {
                newCitizen.VatId = citizen.VatId;
            }

            for (int i = 0; i < this.citizens.Length; ++i)
            {
                if (this.citizens[i] == null)
                {
                    this.citizens[i] = newCitizen;
                    this.genderCounts[(int)newCitizen.Gender]++;
                    this.lastRegistrationDateTime = SystemDateTime.Now();
                    break;
                }
            }

            if (this.citizens[this.citizens.Length - 1] != null)
            {
                this.DoubleCitizens();
            }
        }
コード例 #3
0
        public CitizenOfUkraine(string firstName, string lastName, DateTime birthDate, Gender gender)
        {
            if (string.IsNullOrEmpty(firstName))
            {
                throw new ArgumentException("First name is invalid!");
            }

            this.firstName = char.ToUpper(firstName[0]) + firstName.Substring(1).ToLower();

            if (string.IsNullOrEmpty(lastName))
            {
                throw new ArgumentException("Second name is invalid!");
            }

            this.lastName = char.ToUpper(lastName[0]) + lastName.Substring(1).ToLower();

            if (DateTime.Compare(birthDate.Date, SystemDateTime.Now().Date) == 1)
            {
                throw new ArgumentException("Date is inavalid");
            }

            this.birthDate = birthDate.Date;

            if (!gender.Equals(Gender.Female) && !gender.Equals(Gender.Male))
            {
                throw new ArgumentOutOfRangeException("Gender of this citizen is invalid!");
            }

            this.gender = gender;
        }
コード例 #4
0
        public string Stats()
        {
            int      maleN   = 0;
            int      femaleN = 0;
            string   retStr;
            TimeSpan lastRegistrationOccur;

            foreach (ICitizen ob in this.citizenArray)
            {
                if (ob != null)
                {
                    if (int.Parse(ob.VatId[8].ToString()) % 2 == 0)
                    {
                        femaleN++;
                    }
                    else
                    {
                        maleN++;
                    }
                }
            }

            retStr = String.Join(" ", "man".ToQuantity(maleN), "and", "woman".ToQuantity(femaleN));

            lastRegistrationOccur = this.lastRegDate - SystemDateTime.Now();

            if (maleN > 0 || femaleN > 0)
            {
                retStr = string.Join(". ", retStr, "Last registration was " +
                                     SystemDateTime.Now().AddDays(lastRegistrationOccur.Days).Humanize(utcDate: false, culture: new CultureInfo("en-US")));
            }
            return(retStr);
        }
コード例 #5
0
        public string Stats()
        {
            int maleCount   = 0;
            int femaleCount = 0;

            for (int i = 0; i < citizenCount; i++)
            {
                if (citizens[i].Gender == Gender.Male)
                {
                    maleCount++;
                }
                else
                {
                    femaleCount++;
                }
            }

            var maleCountHuman   = "man".ToQuantity(maleCount);
            var femaleCountHuman = "woman".ToQuantity(femaleCount);

            if (maleCount == 0 && femaleCount == 0)
            {
                return(String.Format("{0} and {1}", maleCountHuman, femaleCountHuman));
            }
            else
            {
                var lastRegHuman = lastRegistrationTime.Humanize(true, SystemDateTime.Now());
                return(String.Format("{0} and {1}. Last registration was {2}", maleCountHuman, femaleCountHuman, lastRegHuman));
            }
        }
コード例 #6
0
        public void Register(ICitizen citizen)
        {
            if (citizen.VatId == null || CitizenRegistryHelper.FindCitizenById(citizens, citizenCount, citizen.VatId) == null)
            {
                if (String.IsNullOrWhiteSpace(citizen.VatId))
                {
                    int    birthNumber;
                    string id;

                    do
                    {
                        birthNumber = CitizenRegistryHelper.GetBirthNumber(citizen.Gender);
                        id          = CitizenRegistryHelper.GenerateVatId(citizen.BirthDate, birthNumber, citizen.Gender);
                    } while (CitizenRegistryHelper.FindCitizenById(citizens, citizenCount, id) != null);
                    citizen.VatId = id;
                }
                if (citizenCount == citizens.Length)
                {
                    Array.Resize(ref citizens, citizens.Length * 2);
                }
                citizens[citizenCount] = citizen.Clone() as Citizen;
                citizenCount++;
                lastRegistrationTime = SystemDateTime.Now();
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
コード例 #7
0
        public Citizen(string firstName, string lastName, DateTime birthDate, Gender gender)
        {
            if (birthDate > SystemDateTime.Now())
            {
                throw new ArgumentException("BirthDate");
            }

            if (!Enum.IsDefined(typeof(Gender), gender))
            {
                throw new ArgumentOutOfRangeException("Gender");
            }

            if (String.IsNullOrWhiteSpace(firstName))
            {
                throw new ArgumentNullException("FirstName");
            }

            if (String.IsNullOrWhiteSpace(lastName))
            {
                throw new ArgumentNullException("LastName");
            }

            this.firstName = firstName.Transform(To.LowerCase, To.TitleCase);
            this.lastName  = lastName.Transform(To.LowerCase, To.TitleCase);
            this.birthDate = birthDate.Date;
            this.gender    = gender;
        }
コード例 #8
0
        public void Register(ICitizen citizen)
        {
            if (checkIfExist(citizen.VatId))
            {
                throw new InvalidOperationException();
            }

            if (string.IsNullOrEmpty(citizen.VatId))
            {
                citizen.VatId = registerVatID(citizen);
            }

            citizens[countNow++] = citizen;
            lastDate             = SystemDateTime.Now();
        }
コード例 #9
0
        public string Stats()
        {
            int countOfMen = this.registry.Count((x) => x.Gender == Gender.Male);

            string res = string.Format(
                "{0} and {1}",
                "man".ToQuantity(countOfMen),
                "woman".ToQuantity(this.registry.Length - countOfMen));

            if (this.lastUpdate != null)
            {
                res += string.Format(
                    ". Last registration was {0}",
                    this.lastUpdate.Humanize(true, SystemDateTime.Now()));
            }

            return(res);
        }
コード例 #10
0
        public void Register(ICitizen citizen)
        {
            this.CheckForRepeats(citizen);
            if (this.Count == this.registry.Length)
            {
                Array.Resize(ref this.registry, this.FindNewCapacity());
            }

            if (string.IsNullOrWhiteSpace(citizen.VatId))
            {
                citizen.VatId = this.CalculateValidVatID(citizen);
            }

            this.registry[this.Count] =
                new Citizen(citizen.FirstName, citizen.LastName, citizen.BirthDate, citizen.Gender)
            {
                VatId = citizen.VatId
            };
            this.lastUpdate = SystemDateTime.Now();
            this.Count++;
        }
コード例 #11
0
        private void AddCitizenToArray(ICitizen citizen)
        {
            if (citizen == null)
            {
                throw new ArgumentNullException("citizen");
            }

            if (allCitizens.Length == 0)
            {
                Array.Resize<ICitizen>(ref allCitizens, 1);
            }

            if (registeredCitizensCount == allCitizens.Length)
            {
                Array.Resize<ICitizen>(ref allCitizens, allCitizens.Length * 2);
            }

            allCitizens[registeredCitizensCount] = new Citizen(citizen.FirstName, citizen.LastName, citizen.BirthDate, citizen.Gender);
            allCitizens[registeredCitizensCount].VatId = citizen.VatId;
            lastRegisterTime = SystemDateTime.Now();
            registeredCitizensCount++;
        }
コード例 #12
0
        public void Register(ICitizen citizen)
        {
            if (count >= length)
            {
                throw new IndexOutOfRangeException("Registry is full!");
            }

            if (Contains(citizen.VatId))
            {
                throw new InvalidOperationException("This citizen is already exist!");
            }
            else
            {
                if (string.IsNullOrEmpty(citizen.VatId))
                {
                    citizen.VatId = GenerateVatId(citizen.BirthDate, citizen.Gender);
                }

                citizens[count++]    = (ICitizen)citizen.Clone();
                lastRegistrationTime = SystemDateTime.Now();
            }
        }
コード例 #13
0
        public string Stats()
        {
            int female = 0;
            int male = 0;
            for (int i = 0; i < registeredCitizensCount; i++)
            {
                if (allCitizens[i].Gender == Gender.Female)
                {
                    female++;
                }
                else
                {
                    male++;
                }
            }

            if (female == 0 && male == 0)
            {
                return "0 men and 0 women";
            }

            return "man".ToQuantity(male) + " and " + "woman".ToQuantity(female)
            + ". Last registration was " + lastRegisterTime.Humanize(dateToCompareAgainst: SystemDateTime.Now());
        }
コード例 #14
0
        public string Stats()
        {
            var s = this.lastRegistrationDateTime.HasValue ? ". Last registration was " + this.lastRegistrationDateTime.Value.Humanize(dateToCompareAgainst: SystemDateTime.Now()) : string.Empty;

            return($"{this.genderCounts[(int)Gender.Male]} m{(char)(97 + (Convert.ToInt32(this.genderCounts[(int)Gender.Male] != 1) * 4))}n and {this.genderCounts[(int)Gender.Female]} wom{(char)(97 + (Convert.ToInt32(this.genderCounts[(int)Gender.Female] != 1) * 4))}n" + s);
        }
コード例 #15
0
ファイル: Citizen.cs プロジェクト: Potapy4/KottansHomeworks
        private bool isValidDateOfBirth(DateTime dateOfBirth)
        {
            DateTime dtNow = SystemDateTime.Now(); // Not compare without this

            return(dateOfBirth.CompareTo(dtNow) > 0);
        }
コード例 #16
0
        public void Register(ICitizen citizen)
        {
            int sum = 0;
            int sn  = 0;

            int[] mul = { -1, 5, 7, 9, 4, 6, 10, 5, 7 };

            string serNString;
            string checkNumber;
            string dateString;

            TimeSpan livedPeriod;

            if (String.IsNullOrEmpty(citizen.VatId))
            {
                switch (citizen.Gender)
                {
                case Gender.Female:
                {
                    sn = 0;
                    break;
                }

                case Gender.Male:
                {
                    sn = 1;
                    break;
                }
                }

                livedPeriod = citizen.BirthDate - this.refDate;

                dateString = livedPeriod.Days.ToString().PadLeft(5, '0');

                do
                {
                    serNString = sn.ToString().PadLeft(4, '0');

                    var charArr = string.Concat(dateString, serNString);

                    for (int i = 0; i < charArr.Length; i++)
                    {
                        sum += int.Parse(charArr[i].ToString()) * mul[i];
                    }
                    checkNumber = ((sum % 11) % 10).ToString();
                    sn         += 2;
                }while (this[string.Concat(dateString, serNString, checkNumber)] != null);

                citizen.VatId = string.Concat(dateString, serNString, checkNumber);
            }

            if (this[citizen.VatId] == null)
            {
                for (int i = 0; i < this.citizenArray.Length; i++)
                {
                    var item = this.citizenArray[i];
                    if (item == null)
                    {
                        item             = new Citizen(citizen);
                        this.lastRegDate = SystemDateTime.Now();
                        break;
                    }
                }
            }
            else
            {
                throw new InvalidOperationException("Same person cannot be added twice");
            }
        }