Exemplo n.º 1
0
        public string CraftPresent(string presentName)
        {
            Workshop workshop          = new Workshop();
            IPresent present           = this.presentRepository.FindByName(presentName);
            var      dwarfsReadyToWork = this.dwarfRepository
                                         .Models
                                         .Where(x => x.Energy >= 50)
                                         .OrderByDescending(x => x.Energy)
                                         .ToList();

            if (!dwarfsReadyToWork.Any())
            {
                throw new InvalidOperationException(ExceptionMessages.DwarfsNotReady);
            }

            foreach (IDwarf dwarf in dwarfsReadyToWork)
            {
                workshop.Craft(present, dwarf);
                if (dwarf.Energy <= 0)
                {
                    this.dwarfRepository.Remove(dwarf);
                }
                if (present.IsDone())
                {
                    break;
                }
            }

            string output = string.Format(present.IsDone() ?
                                          OutputMessages.PresentIsDone :
                                          OutputMessages.PresentIsNotDone, presentName);

            return(output);
        }
        public string CraftPresent(string presentName)
        {
            var present = this.presents.FindByName(presentName);

            var suitable = this.dwarves.Models.Where(d => d.Energy >= 50).ToList();

            if (suitable.Count == 0)
            {
                throw new InvalidOperationException(ExceptionMessages.DwarfsNotReady);
            }

            var workshop = new Workshop();


            foreach (var dwarf in suitable)
            {
                workshop.Craft(present, dwarf);
                if (dwarf.Energy == 0)
                {
                    this.dwarves.Remove(dwarf);
                }
            }

            if (present.IsDone())
            {
                return(string.Format(OutputMessages.PresentIsDone, presentName));
            }
            else
            {
                return(string.Format(OutputMessages.PresentIsNotDone, presentName));
            }
        }
        public string CraftPresent(string presentName)
        {
            IPresent present     = presentRepository.FindByName(presentName);
            var      dwarfsReady = dwarfRepository.Models.OrderByDescending(x => x.Energy).Where(x => x.Energy >= 50).ToList();
            var      workShop    = new Workshop();

            if (!dwarfsReady.Any())
            {
                throw new InvalidOperationException(ExceptionMessages.DwarfsNotReady);
            }

            while (dwarfsReady.Any())
            {
                IDwarf currentDwarf = dwarfsReady.First();

                workShop.Craft(present, currentDwarf);

                dwarfsReady.Remove(currentDwarf);
            }

            if (present.IsDone())
            {
                return(string.Format(OutputMessages.PresentIsDone, present.Name));
            }

            else
            {
                return(string.Format(OutputMessages.PresentIsNotDone, present.Name));
            }
        }
Exemplo n.º 4
0
        public string CraftPresent(string presentName)
        {
            var workShop        = new Workshop();
            var targetPresent   = presents.FindByName(presentName);
            var availableDwarfs = dwarfs.Models.Where(x => x.Energy >= 50).OrderByDescending(x => x.Energy);

            if (availableDwarfs.Count() == 0)
            {
                throw new InvalidOperationException("There is no dwarf ready to start crafting!");
            }

            foreach (var dwarf in availableDwarfs)
            {
                workShop.Craft(targetPresent, dwarf);
                if (dwarf.Energy == 0)
                {
                    dwarfs.Remove(dwarf);
                }
                if (targetPresent.IsDone())
                {
                    craftedPresents++;
                    break;
                }
            }

            var isDone = "not done";

            if (targetPresent.IsDone())
            {
                isDone = "done";
            }
            return($"Present {presentName} is {isDone}.");
        }
Exemplo n.º 5
0
        public string CraftPresent(string presentName)
        {
            Workshop workshop = new Workshop();

            IPresent present = this.presents.FindByName(presentName); // намираме подаръка

            ICollection <IDwarf> dwarves = this.dwarfs.Models.Where(dw => dw.Energy >= 50).OrderByDescending(dw => dw.Energy).ToList();

            // намираме тези дуорфи, които имат енергия над 50 и ги подреждаме по нея

            if (!dwarves.Any()) // ако е празна колекцията
            {
                throw new InvalidOperationException(ExceptionMessages.DwarfsNotReady);
            }

            while (dwarves.Any())
            {
                IDwarf currDwarf = dwarves.First();

                workshop.Craft(present, currDwarf);

                if (!currDwarf.Instruments.Any()) // ако му свършат инструментите
                {
                    dwarves.Remove(currDwarf);    //махаме го само от локалната колекция !!!
                }

                if (currDwarf.Energy == 0) //ако му свърши енергията
                {
                    dwarves.Remove(currDwarf);
                    this.dwarfs.Remove(currDwarf); // премахваме го от двете колекции
                }

                if (present.IsDone())
                {
                    break;
                }
            }



            //string result;

            //if (present.IsDone())
            //{
            //    result = string.Format(OutputMessages.PresentIsDone, presentName);
            //}
            //else
            //{
            //    result = string.Format(OutputMessages.PresentIsNotDone, presentName);
            //}

            //return result;

            string result = string.Format(present.IsDone() ? OutputMessages.PresentIsDone : OutputMessages.PresentIsNotDone, presentName);

            return(result);
        }
Exemplo n.º 6
0
        public string CraftPresent(string presentName)
        {
            IPresent present = this.presents.FindByName(presentName);
            IDwarf   dwarf   = dwarves.Models
                               .Where(d => d.Energy >= 50)
                               .OrderByDescending(d => d.Energy)
                               .FirstOrDefault(d => d.Instruments.Count > 0);

            Workshop workshop = new Workshop();

            while (dwarves.Models.Any(d => d.Energy >= 50))
            {
                if (dwarf == null)
                {
                    throw new InvalidOperationException(ExceptionMessages.DwarfsNotReady);
                }

                workshop.Craft(present, dwarf);

                if (dwarf.Energy == 0)
                {
                    this.dwarves.Remove(dwarf);
                }

                if (present.IsDone())
                {
                    break;
                }

                if (!dwarf.Instruments.Any(i => i.Power > 0))
                {
                    break;
                }
            }

            string result;

            if (present.IsDone())
            {
                result = string.Format(OutputMessages.PresentIsDone, presentName);
            }
            else
            {
                result = string.Format(OutputMessages.PresentIsNotDone, presentName);
            }

            return(result);
        }
Exemplo n.º 7
0
        public string CraftPresent(string presentName)
        {
            var workshop = new Workshop();

            var present = this.presents.FindByName(presentName);

            var readyDwarfs = this.dwarfs.Models.
                              Where(d => d.Energy >= 50)
                              .OrderByDescending(d => d.Energy).ToList();

            if (!readyDwarfs.Any())
            {
                var msg = ExceptionMessages.DwarfsNotReady;
                throw new InvalidOperationException(msg);
            }

            while (readyDwarfs.Any())
            {
                var currentDwarf = readyDwarfs.First();

                workshop.Craft(present, currentDwarf);

                if (!currentDwarf.Instruments.Any())
                {
                    readyDwarfs.Remove(currentDwarf);
                }

                if (currentDwarf.Energy == 0)
                {
                    this.dwarfs.Remove(currentDwarf);
                    readyDwarfs.Remove(currentDwarf);
                }

                if (present.IsDone())
                {
                    break;
                }
            }


            var outputMsg =
                string.Format(present.IsDone() ?
                              OutputMessages.PresentIsDone :
                              OutputMessages.PresentIsNotDone, presentName);

            return(outputMsg);
        }
Exemplo n.º 8
0
        public string CraftPresent(string presentName)
        {
            Workshop workshop = new Workshop();

            IPresent present = this.presents.FindByName(presentName);

            ICollection <IDwarf> dwarves = this.dwarfs
                                           .Models
                                           .Where(x => x.Energy >= 50)
                                           .OrderByDescending(x => x.Energy)
                                           .ToList();

            if (!dwarves.Any())
            {
                throw new InvalidOperationException(ExceptionMessages.DwarfsNotReady);
            }

            while (dwarves.Any())
            {
                IDwarf currentDwarf = dwarves.First();

                workshop.Craft(present, currentDwarf);

                if (!currentDwarf.Instruments.Any())
                {
                    dwarves.Remove(currentDwarf);
                }

                if (currentDwarf.Energy == 0)
                {
                    dwarves.Remove(currentDwarf);
                    this.dwarfs.Remove(currentDwarf);
                }

                if (present.IsDone())
                {
                    break;
                }
            }

            string output = string.Format(present.IsDone() ? OutputMessages.PresentIsDone : OutputMessages.PresentIsNotDone, presentName);

            return(output);
        }
Exemplo n.º 9
0
        public string CraftPresent(string presentName)
        {
            var team = this.dwarfs.Models
                       .Where(d => d.Energy >= 50)
                       .OrderByDescending(x => x.Energy)
                       .ToList();

            if (team.Count() == 0)
            {
                throw new InvalidOperationException(ExceptionMessages.DwarfsNotReady);
            }

            Workshop workshop = new Workshop();

            var presentToCraft = this.presents.FindByName(presentName);

            foreach (var d in team)
            {
                workshop.Craft(presentToCraft, d);

                if (d.Energy == 0)
                {
                    this.dwarfs.Remove(d);
                    team.Remove(d);
                }

                if (!d.Instruments.Any())
                {
                    team.Remove(d);
                    //this._dwarfs.Remove(d);
                }

                if (presentToCraft.IsDone())
                {
                    string msg = string.Format(OutputMessages.PresentIsDone, presentName);

                    return(msg);
                }
            }

            string msgNotDone = string.Format(OutputMessages.PresentIsNotDone, presentName);

            return(msgNotDone);
        }
Exemplo n.º 10
0
        public string CraftPresent(string presentName)
        {
            IPresent present = this.presents.FindByName(presentName);



            ICollection <IDwarf> sortedDwarfs = this.dwarfs.Models.Where(x => x.Energy >= 0).OrderByDescending(x => x.Energy).ToList();

            if (!sortedDwarfs.Any())
            {
                throw new InvalidOperationException(ExceptionMessages.DwarfsNotReady);
            }

            while (sortedDwarfs.Any())
            {
                var dwarfThatCraft = sortedDwarfs.FirstOrDefault();

                Workshop workshop = new Workshop();

                workshop.Craft(present, dwarfThatCraft);

                if (!dwarfThatCraft.Instruments.Any())
                {
                    sortedDwarfs.Remove(dwarfThatCraft);
                }

                if (dwarfThatCraft.Energy == 0)
                {
                    sortedDwarfs.Remove(dwarfThatCraft);
                    this.dwarfs.Remove(dwarfThatCraft);
                }

                if (present.IsDone())
                {
                    break;
                }
            }
            var result = present.IsDone() ? string.Format(OutputMessages.PresentIsDone, presentName) : string.Format(OutputMessages.PresentIsNotDone, presentName);

            return(result);
        }
        public string CraftPresent(string presentName)
        {
            IWorkshop workshop    = new Workshop();
            IPresent  present     = this.presents.FindByName(presentName);
            var       readyDwarfs = this.dwarfs.Models.Where(d => d.Energy >= 50).OrderByDescending(a => a.Energy).ToList();

            if (readyDwarfs.Count > 0)
            {
                for (int i = 0; i < readyDwarfs.Count;)
                {
                    workshop.Craft(present, readyDwarfs[i]);

                    if (readyDwarfs[i].Energy == 0)
                    {
                        this.dwarfs.Remove(readyDwarfs[i]);
                        i++;
                    }
                    if (present.IsDone())
                    {
                        break;
                    }
                    if (!present.IsDone() || readyDwarfs[i].Instruments.Count == 0)
                    {
                        i++;
                    }
                }
            }
            else
            {
                throw new InvalidOperationException(ExceptionMessages.DwarfsNotReady);
            }

            if (present.IsDone())
            {
                return($"Present {presentName} is done.");
            }
            else
            {
                return($"Present {presentName} is not done.");
            }
        }
Exemplo n.º 12
0
        public string CraftPresent(string presentName)
        {
            IDwarf dwarf = this.dwarfs.Models
                           .OrderByDescending(x => x.Energy)
                           .FirstOrDefault(x => x.Energy >= 50 && x.Instruments
                                           .Any(x => x.IsBroken() == false));

            var      present  = this.presents.FindByName(presentName);
            Workshop workshop = new Workshop();

            if (dwarf == null)
            {
                dwarf = this.dwarfs.Models
                        .OrderByDescending(x => x.Energy)
                        .FirstOrDefault(x => x.Energy >= 50);
            }

            if (dwarf == null)
            {
                throw new InvalidOperationException(ExceptionMessages.DwarfsNotReady);
            }

            workshop.Craft(present, dwarf);

            if (dwarf.Energy == 0)
            {
                this.dwarfs.Remove(dwarf);
            }

            if (present.IsDone() == false)
            {
                return(string.Format(OutputMessages.PresentIsNotDone, presentName));
            }
            else
            {
                return(string.Format(OutputMessages.PresentIsDone, presentName));
            }
        }
Exemplo n.º 13
0
        public string CraftPresent(string presentName)
        {
            List <IDwarf> readyToWorkDwarves = dwarfs.Models.Where(el => el.Energy >= 50).OrderByDescending(el => el.Energy).ToList();
            IPresent      present            = presents.FindByName(presentName);

            if (readyToWorkDwarves.Count == 0)
            {
                throw new InvalidOperationException(ExceptionMessages.DwarfsNotReady);
            }
            foreach (var dwarf in readyToWorkDwarves)
            {
                workshop.Craft(present, dwarf);
                if (dwarf.Energy == 0)
                {
                    dwarfs.Remove(dwarf);
                }
                if (present.IsDone())
                {
                    return(string.Format(OutputMessages.PresentIsDone, presentName));
                }
            }
            return(string.Format(OutputMessages.PresentIsNotDone, presentName));
        }
Exemplo n.º 14
0
        public string CraftPresent(string presentName)
        {
            var sortedDwarfs = this.dwarfs.Models.Where(x => x.Energy >= 50).OrderByDescending(x => x.Energy).ToList();

            if (sortedDwarfs.Count == 0)
            {
                throw new InvalidOperationException(ExceptionMessages.DwarfsNotReady);
            }
            var workshop = new Workshop();
            var present  = this.presents.Models.FirstOrDefault(x => x.Name == presentName);

            while (sortedDwarfs.Count > 0)
            {
                var currentDwarf = sortedDwarfs.FirstOrDefault(x => x.Energy >= 50);

                workshop.Craft(present, currentDwarf);



                if (currentDwarf.Energy == 0)
                {
                    sortedDwarfs.Remove(currentDwarf);
                    this.dwarfs.Remove(currentDwarf);
                }
                if (!currentDwarf.Instruments.Any(x => x.Power > 0))
                {
                    sortedDwarfs.Remove(currentDwarf);
                }
                if (present.IsDone())
                {
                    break;
                }
            }
            var outputMsg = present.IsDone() ? $"Present {presentName} is done." : $"Present {presentName} is not done.";

            return(outputMsg);
        }
Exemplo n.º 15
0
        public string CraftPresent(string presentName)
        {
            IPresent             present         = this.presents.Models.FirstOrDefault(p => p.Name == presentName);
            ICollection <IDwarf> suitableDwarves = this.dwarfs.Models.Where(d => d.Energy >= 50).OrderByDescending(d => d.Energy).ToList();

            if (suitableDwarves.Count == 0)
            {
                throw new InvalidOperationException(ExceptionMessages.DwarfsNotReady);
            }
            Workshop workshop = new Workshop();

            while (suitableDwarves.Any())
            {
                IDwarf currentDwarf = suitableDwarves.First();
                workshop.Craft(present, currentDwarf);
                if (currentDwarf.Instruments.Count == 0)
                {
                    suitableDwarves.Remove(currentDwarf);
                }
                if (currentDwarf.Energy == 0)
                {
                    suitableDwarves.Remove(currentDwarf);
                    this.dwarfs.Remove(currentDwarf);
                }
                if (present.IsDone())
                {
                    break;
                }
            }
            if (present.IsDone())
            {
                craftedPresentsCount++;
                return(String.Format(OutputMessages.PresentIsDone, presentName));
            }
            return(String.Format(OutputMessages.PresentIsNotDone, presentName));
        }