コード例 #1
0
        public static void Main(string[] args)
        {
            IList <IBirthday> birthdateLists = new List <IBirthday>();

            var       input     = Console.ReadLine();
            IBirthday birthdate = null;

            while (input != "End")
            {
                var tokens = input.Split().ToArray();

                if (tokens[0] == "Citizen")
                {
                    birthdate = new Citizen(tokens[1], int.Parse(tokens[2]), tokens[3], tokens[4]);
                }
                else
                {
                    birthdate = new Pet(tokens[1], tokens[2]);
                }
                birthdateLists.Add(birthdate);
                input = Console.ReadLine();
            }

            string pattern = Console.ReadLine().Trim();

            var result = birthdateLists.Where(s => s.Birthdate.EndsWith(pattern)).Select(s => s.Birthdate).ToList();

            if (result.Count == 0)
            {
                Console.WriteLine(string.Empty);
            }
            Console.WriteLine(string.Join(Environment.NewLine, result.ToString()));
        }
コード例 #2
0
        public void AddParticipant(IBirthday p)
        {
            DateTime now     = DateTime.Now;
            var      newItem = new KeyValuePair <TimeSpan, IBirthday>(p.DateTimeBirth.TimeOfDay, p);
            var      index   = _participants.Add(newItem);

            while (--index >= 0 && _participants[index].Key == p.DateTimeBirth.TimeOfDay)
            {
            }
            index++;
            if (_nextIndex == index)
            {
                if (index == 0)
                {
                    TimeSpan interval = newItem.Key - now.TimeOfDay;
                    if (interval.Ticks < 0)
                    {
                        interval += TimeSpan.FromDays(1);
                    }
                    if (_participants.Count == 1)
                    {
                        _nextIndex = 0;
                        StartTimer(interval);
                    }
                }
                else if (newItem.Key >= now.TimeOfDay)
                {
                    var      rightNow     = DateTime.Now;
                    TimeSpan nextInterval = newItem.Key - rightNow.TimeOfDay;
                    if (nextInterval.Ticks <= 0)
                    {
                        //logically the only way to get here ie newitem time > now[start of function] & newitem time < now[right now]
                        //is for time to have elapsed while function was executing
                        OnTick(this, null);
                        _nextIndex++;
                        return;
                    }
                    _timer.Interval = nextInterval;
                }
                else
                {
                    _nextIndex++;
                }
            }
            else if (_nextIndex == 0 && index == _participants.Count - 1)
            {
                if (now.TimeOfDay < newItem.Key)
                {
                    _nextIndex      = index;
                    _timer.Interval = newItem.Key - now.TimeOfDay;
                }
            }
            else if (_nextIndex > index)
            {
                _nextIndex++;
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: MartiHr/Softuni
        static void Main(string[] args)
        {
            string command = string.Empty;

            List <IId>       ids       = new List <IId>();
            List <IBirthday> birthdays = new List <IBirthday>();

            while ((command = Console.ReadLine()) != "End")
            {
                string[] elements = command
                                    .Split(" ");

                string objectType = elements[0];

                if (objectType == "Robot")
                {
                    string model = elements[0];
                    string id    = elements[1];

                    IId currentId = new Robot(model, id);
                    ids.Add(currentId);
                }
                else if (objectType == "Citizen")
                {
                    string name      = elements[1];
                    int    age       = int.Parse(elements[2]);
                    string id        = elements[3];
                    string birthDate = elements[4];

                    Citizen currentCitizen = new Citizen(name, age, id, birthDate);
                    IId     currentId      = currentCitizen;
                    ids.Add(currentId);
                    IBirthday birthday = currentCitizen;
                    birthdays.Add(birthday);
                }
                else
                {
                    string name      = elements[1];
                    string birthDate = elements[2];

                    IBirthday birthday = new Pet(name, birthDate);
                    birthdays.Add(birthday);
                }
            }

            string filterYear = Console.ReadLine();

            List <IBirthday> filteredBirthdays = birthdays
                                                 .Where(x => x.Birthdate.EndsWith(filterYear))
                                                 .ToList();

            Console.WriteLine(string.Join(Environment.NewLine, filteredBirthdays.Select(x => x.Birthdate)));
        }
コード例 #4
0
        void OnTick(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;

#if DEBUG
            var moqArgs = e as MoqTimerEventArgs;
            if (moqArgs == null) //ie not running a test
            {
                System.Diagnostics.Debug.WriteLine("tick at:{0} interval had been:{1}", now, _timer.Interval);
            }
            else
            {
                now = moqArgs.Now;
            }
#endif
            TimeSpan nowTime = now.TimeOfDay.Add(_adjustIntoFuture);
            //if (_participants.Count == 0) { _timer.Stop(); return; }
            do
            {
                if (OnAgeIncrement == null)
                {
                    _nextIndex++;
                }
                else
                {
                    IBirthday p = _participants[_nextIndex].Value;
                    p.AgeDays = (now - p.DateTimeBirth).Days;
                    AgeIncrementingEventArgs arg = new AgeIncrementingEventArgs(p);
                    OnAgeIncrement(this, arg);
                    if (arg.Remove)
                    {
                        _participants.RemoveAt(_nextIndex);
                        if (_participants.Count == 0)
                        {
                            _timer.Stop();
                            return;
                        }
                    }
                    else
                    {
                        _nextIndex++;
                    }
                }
                if (_nextIndex >= _participants.Count)
                {
                    _nextIndex = 0;
                    break;
                }
            } while(_participants[_nextIndex].Key <= nowTime);
            nowTime = DateTime.Now.TimeOfDay;
#if DEBUG
            if (moqArgs != null)
            {
                nowTime = moqArgs.Now.TimeOfDay;
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("next interval set at:{0}", _participants[_nextIndex].Key - nowTime);
            }
#endif
            TimeSpan nextInterval = _participants[_nextIndex].Key - nowTime;
            if (nextInterval.Ticks < 0)
            {
                nextInterval += TimeSpan.FromDays(1);
            }
            _timer.Interval = nextInterval;
        }
コード例 #5
0
 internal AgeIncrementingEventArgs(IBirthday p)
 {
     Participant = p;
 }