コード例 #1
0
        public static void Main(string[] args)
        {
            IReader reader = new Reader();
            IWriter writer = new Writer();

            IAnimalFactory animalFactory = new AnimalFactory();
            IFoodFactory   foodFactory   = new FoodFactory();

            Engine engine = new Engine(reader, writer, animalFactory, foodFactory);

            engine.Proceed();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: eognianov/CShap-Projects
        private static void ReadAndParseInput(List <Animal> animals, List <Food> foods)
        {
            string input = Console.ReadLine();

            while (input != "End")
            {
                string[] animalParts = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                string[] foodParts   = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                Animal animal = AnimalFactory.GetAnimal(animalParts);
                Food   food   = FoodFactory.GetFood(foodParts);
                animals.Add(animal);
                foods.Add(food);
                input = Console.ReadLine();
            }
        }
コード例 #3
0
        static void Main()
        {
            var    animalsFactory = new AnimalFactory();
            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                var    tokens        = input.Split();
                Animal currentAnimal = null;
                currentAnimal = animalsFactory.CreateAnimal(tokens);
                if (currentAnimal != null)
                {
                    list.Add(currentAnimal);
                    Console.WriteLine(currentAnimal.MakeSound());
                    var  foodTokens  = Console.ReadLine().Split();
                    Food currentFood = null;
                    switch (foodTokens[0])
                    {
                    case "Vegetable":
                        currentFood = new Vegetable(int.Parse(foodTokens[1]));
                        break;

                    case "Fruit":
                        currentFood = new Fruit(int.Parse(foodTokens[1]));
                        break;

                    case "Meat":
                        currentFood = new Meat(int.Parse(foodTokens[1]));
                        break;

                    case "Seeds":
                        currentFood = new Seeds(int.Parse(foodTokens[1]));
                        break;
                    }
                    try
                    {
                        currentAnimal.Feed(currentFood);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }
            Console.WriteLine(string.Join(Environment.NewLine, list));
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: radomirKrastev/OOP
        public static void Main()
        {
            var counter = 0;

            var command = Console.ReadLine();
            var animals = new List <Animal>();

            while (command != "End")
            {
                var commandParts = command.Split(" ");

                if (counter++ % 2 == 0)
                {
                    animals.Add(AnimalFactory.InitializeAnimal(commandParts));
                    var currentAnimal = animals.Where(x => x.GetType().Name == commandParts[0]).LastOrDefault();

                    Console.WriteLine(currentAnimal.Speak());
                }
                else
                {
                    var foodType     = commandParts[0];
                    var foodQuantity = int.Parse(commandParts[1]);

                    var food = new Food(foodQuantity, foodType);

                    try
                    {
                        var currentAnimal = animals.LastOrDefault();

                        currentAnimal?.Eat(food);
                    }
                    catch (ArgumentException ae)
                    {
                        Console.WriteLine(ae.Message);
                    }
                }

                command = Console.ReadLine();
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
コード例 #5
0
        public static void Main(string[] args)
        {
            List <Animal> animals = new List <Animal>();

            while (true)
            {
                string input = Console.ReadLine();

                if (input == "End")
                {
                    break;
                }

                string[] animalArgs = input.Split();

                Animal animal = AnimalFactory.Create(animalArgs);

                string[] foodArgs = Console.ReadLine().Split();

                Food food = FoodFactory.Create(foodArgs);

                Console.WriteLine(animal.ProduceSound());

                animals.Add(animal);

                try
                {
                    animal.Eat(food);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
コード例 #6
0
ファイル: StartUp.cs プロジェクト: Retterath/FarmersLife
        public static void Main()
        {
            List <Animal> animals = new List <Animal>();

            while (true)
            {
                string   line           = Console.ReadLine();
                string[] elementsAnimal = line.Split();

                if (line == "End")
                {
                    break;
                }

                string[]      elementsFood = Console.ReadLine().Split();
                AnimalFactory factory      = new AnimalFactory();
                Animal        animal       = factory.Create(elementsAnimal[0], elementsAnimal);
                Console.WriteLine(animal.ProducingSound());

                FoodFactory foodFactory = new FoodFactory();
                Food        food        = foodFactory.Create(elementsFood[0], elementsFood);

                try
                {
                    animal.Food(food);
                    animal.IncreasWeight(food);
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }

                animals.Add(animal);
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal.ToString());
            }
        }
コード例 #7
0
        static void Main(string[] args)
        {
            var    animals = new List <Animal>();
            string inputLine;

            while ((inputLine = Console.ReadLine()) != "End")
            {
                try
                {
                    var input = inputLine.Split();
                    if (input.Length == 2)
                    {
                        var foodName = input[0];
                        var quantity = int.Parse(input[1]);
                        var food     = FoodFactory.GetFood(foodName, quantity);
                        var animal   = animals.Last();
                        animal.Eate(food);
                        continue;
                    }
                    var    animalType   = input[0];
                    var    animalName   = input[1];
                    var    animalWeight = double.Parse(input[2]);
                    var    livingRegion = input[3];
                    string breed        = null;
                    if (input.Length == 5)
                    {
                        breed = input[4];
                    }
                    animals.Add(AnimalFactory.GetAnimal(animalType, animalName, animalWeight, livingRegion, breed));
                }
                catch (ArgumentException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
コード例 #8
0
        public static void Main()
        {
            List <Animal> animals = new List <Animal>();

            while (true)
            {
                string[] info = Console.ReadLine().Split();

                if (info[0] == "End")
                {
                    break;
                }

                Animal animal = AnimalFactory.CreateAnimal(info);

                info = Console.ReadLine().Split();
                Food food = FoodFactory.CreateFood(info);

                Console.WriteLine(animal.MakeSound());

                try
                {
                    animal.Feed(food);
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                }

                animals.Add(animal);
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
コード例 #9
0
        static void Main(string[] args)
        {
            string inputLine;

            while ((inputLine = Console.ReadLine()) != "End")
            {
                var animalTokens = inputLine.Split(new[] { '\t', ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                var foodTokens   = Console.ReadLine().Split(new[] { '\t', ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries);

                Animal animal = AnimalFactory.GetAnimal(animalTokens);
                Food   food   = FoodFactory.GetFood(foodTokens);

                Console.WriteLine(animal.MakeSound());
                try
                {
                    animal.Eat(food);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                Console.WriteLine(animal);
            }
        }