Пример #1
0
 public void GoToPark(ZooPark park)
 {
     while (true)
     {
         foreach (AnimalContainer container in park.Animals)
         {
             contentment /= container.Shits.Count;
         }
         Thread.Sleep(1000);
     }
 }
Пример #2
0
        static void Main(string[] args)
        {
            ZooPark zoo   = new ZooPark();
            Lione   lion  = new Lione(zoo.GetManager(), 1, 10);
            Tiger   tiger = new Tiger(zoo.GetManager(), 3, 12);
            Cage    cage  = new Cage();

            cage.AddAnimal("Diego", tiger);
            cage.AddAnimal("Sam", lion);



            zoo.StartWork();
        }
Пример #3
0
        static void Main(string[] args)
        {
            List <Guest>    guests            = new List <Guest>();
            List <Thread>   threads           = new List <Thread>();
            AnimalContainer elephantContainer = new AnimalContainer("Elephants");
            List <Elephant> elephants         = new List <Elephant>();
            float           timeWarp          = 0.0001f;

            for (int i = 0; i < 6; i++)
            {
                elephants.Add(new Elephant());
                elephantContainer.PlaceAnimal(elephants[i]);
                Elephant elephant = elephants[i];
                Thread   thread   = new Thread(() => elephant.Exist(timeWarp));
                threads.Add(thread);
            }
            AnimalContainer foxContainer = new AnimalContainer("Fox");
            List <Fox>      foxes        = new List <Fox>();

            for (int i = 0; i < 6; i++)
            {
                foxes.Add(new Fox());
                foxContainer.PlaceAnimal(foxes[i]);
                Fox    fox    = foxes[i];
                Thread thread = new Thread(() => fox.Exist(timeWarp));
                threads.Add(thread);
            }
            List <AnimalContainer> animalContainers = new List <AnimalContainer>
            {
                elephantContainer,
                foxContainer,
            };
            ZooPark zoo = new ZooPark(animalContainers);

            for (int i = 0; i < 300; i++)
            {
                Guest guest = new Guest();
                guests.Add(guest);
                Thread thread = new Thread(() => guest.GoToPark(zoo));
                threads.Add(thread);
            }
            EventWaitHandle[] handles = new EventWaitHandle[animalContainers.Count];
            for (int i = 0; i < animalContainers.Count; i++)
            {
                handles[i] = animalContainers[i].hasShit;
            }
            List <Worker> workers = new List <Worker>();

            for (int i = 0; i < 2; i++)
            {
                Worker worker = new Worker(zoo);
                Thread thread = new Thread(() => worker.Work(timeWarp));
                threads.Add(thread);
            }
            for (int i = 0; i < threads.Count; i++)
            {
                threads[i].Start();
            }
            ConsoleColor originalColor = Console.BackgroundColor;

            while (true)
            {
                int maxLenth = 0;
                foreach (AnimalContainer container in animalContainers)
                {
                    // Find max length of all containers
                    maxLenth = maxLenth < container.Name.Length ? container.Name.Length : maxLenth;
                }
                Console.WriteLine();
                for (int i = 0; i < animalContainers.Count; i++)
                {
                    Console.SetCursorPosition(0, i);
                    Console.Write(animalContainers[i].Name);
                    Console.SetCursorPosition(maxLenth + 1, i);
                    float poopAmount = animalContainers[i].Shits.Count / 500.0f;
                    poopAmount = poopAmount > 1 ? 1 : poopAmount;
                    Console.BackgroundColor = ConsoleColor.DarkRed;
                    // Calculate amount of space left
                    float ratio = (Console.BufferWidth - maxLenth - 2) * poopAmount;
                    for (int j = 0; j < (int)(ratio); j++)
                    {
                        Console.Write(' ');
                    }
                    Console.BackgroundColor = originalColor;
                }
                Console.WriteLine();
                int index = WaitHandle.WaitAny(handles);
                handles[index].Reset();
                Console.WriteLine("Container " + animalContainers[index].Name + " has shit");
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            try
            {
                string         url           = @"ZooInfo.txt";
                ZooPark        zoo           = new ZooPark();
                IGetService    getService    = new GetConsole();
                INotifyService notifyService = new NotifyConsole();

                Dictionary <string, IParser> dict = new Dictionary <string, IParser>();
                dict.Add("Chiken", new ToChikenParser());
                dict.Add("Stork", new ToStorkParser());
                dict.Add("Wolf", new ToWolfParser());
                dict.Add("Tiger", new ToTigerParser());
                dict.Add("Cat", new ToCatParser());
                dict.Add("Dog", new ToDogParser());
                ToAnimalParser toAnimalParser = new ToAnimalParser(dict);


                Dictionary <string, IFabric> fabrics = new Dictionary <string, IFabric>();
                fabrics.Add("1", new GetChiken(getService, notifyService));
                fabrics.Add("2", new GetStork(getService, notifyService));
                fabrics.Add("3", new GetWolf(getService, notifyService));
                fabrics.Add("4", new GetTiger(getService, notifyService));
                fabrics.Add("5", new GetCat());
                fabrics.Add("6", new GetDog(getService, notifyService));

                Dictionary <string, ICommand> commands = new Dictionary <string, ICommand>();
                commands.Add("stop", new StopCommand());
                commands.Add("1", new AddAnimalCommand(zoo, new AnimalFabric(fabrics, getService, notifyService)));
                commands.Add("2", new DeleteCommand(zoo, notifyService, getService));
                commands.Add("3", new InfoCommand(zoo, notifyService, getService));
                commands.Add("4", new InfoAllComand(zoo));
                commands.Add("5", new SoundCommand(zoo, notifyService, getService));
                commands.Add("6", new SoundAllCommand(zoo));
                commands.Add("7", new GetZooCommand(zoo, new FileReaderService(url, toAnimalParser)));
                commands.Add("8", new SaveCommand(zoo, new FileWriterService(url)));
                Menu menu = new Menu(commands);

                string variant = "";
                menu.Choose("7");
                do
                {
                    PrintMenu();
                    variant = Console.ReadLine();
                    Console.Clear();
                    if (menu.IsThere(variant))
                    {
                        Console.WriteLine(menu.Choose(variant));
                    }
                    else
                    {
                        Console.WriteLine("Неверный ввод");
                    }
                } while (variant != "stop");
            }
            catch (Exception a)
            {
                Console.WriteLine(a.Message);
            }
        }
Пример #5
0
 public Worker(ZooPark workPlace)
 {
     this.workPlace = workPlace;
 }