Beispiel #1
0
        static void Main(string[] args)
        {
            string[]      info;
            List <Animal> animals = new List <Animal>();

            while (true)
            {
                info = Console.ReadLine().Split(' ');

                if (info[0] == "End")
                {
                    break;
                }
                string[] food   = Console.ReadLine().Split(' ');
                Animal   animal = null;
                switch (info[0])
                {
                case "Cat":
                    animal = new Cat(info[1], Convert.ToDouble(info[2]), info[3], info[4], Convert.ToInt32(food[1]));
                    break;

                case "Tiger":
                    animal = new Tiger(info[1], Convert.ToDouble(info[2]), info[3], info[4], Convert.ToInt32(food[1]));
                    break;

                case "Hen":
                    Hen hen = new Hen(info[1], Convert.ToDouble(info[2]), Convert.ToDouble(info[3]), Convert.ToInt32(food[1]));
                    break;

                case "Owl":
                    Owl owl = new Owl(info[1], Convert.ToDouble(info[2]), Convert.ToDouble(info[3]), Convert.ToInt32(food[1]));
                    break;

                case "Mouse":
                    Mouse mouse = new Mouse(info[1], Convert.ToDouble(info[2]), info[3], Convert.ToInt32(food[1]));
                    break;

                case "Dog":
                    Dog dog = new Dog(info[1], Convert.ToDouble(info[2]), info[3], Convert.ToInt32(food[1]));;
                    break;

                default:
                    Console.WriteLine($"{info[0]} does not exist.");
                    break;
                }
                animal.Talk();
                animal.Eat(food[0], food[1]);
                animals.Add(animal);
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal.ToString());
            }
        }
Beispiel #2
0
        public static void Main()
        {
            string        command = string.Empty;
            List <Animal> animals = new List <Animal>();

            while ((command = Console.ReadLine()) != "End")
            {
                //TODO: try catch exeptions
                string[] args   = command.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                string   type   = args[0];
                string   name   = args[1];
                double   weight = double.Parse(args[2]);

                string[] foodInput   = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
                string   foodType    = foodInput[0];
                int      foodQuanity = int.Parse(foodInput[1]);

                //TODO: if food is null
                Food food = null;
                switch (foodType)
                {
                case "Vegetable":
                    food = new Vegetable(foodQuanity);
                    break;

                case "Fruit":
                    food = new Fruit(foodQuanity);
                    break;

                case "Meat":
                    food = new Meat(foodQuanity);
                    break;

                case "Seeds":
                    food = new Seeds(foodQuanity);
                    break;

                default:
                    break;
                }

                Animal animal = null;
                switch (type)
                {
                case "Owl":
                    double wingsSize = double.Parse(args[3]);
                    animal = new Owl(name, weight, wingsSize);
                    break;

                case "Hen":
                    wingsSize = double.Parse(args[3]);
                    animal    = new Hen(name, weight, wingsSize);
                    break;

                case "Mouse":
                    string livingRegion = args[3];
                    animal = new Mouse(name, weight, livingRegion);
                    break;

                case "Dog":
                    livingRegion = args[3];
                    animal       = new Dog(name, weight, livingRegion);
                    break;

                case "Cat":
                    livingRegion = args[3];
                    string breed = args[4];
                    animal = new Cat(name, weight, livingRegion, breed);
                    break;

                case "Tiger":
                    livingRegion = args[3];
                    breed        = args[4];
                    animal       = new Tiger(name, weight, livingRegion, breed);
                    break;
                }

                animal.Eat(food);
                Console.WriteLine();
                animals.Add(animal);
            }
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            var animals = new List <Animal>();

            while (true)
            {
                var command = Console.ReadLine();

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

                var animalInfo = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);

                var foodInfo = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
                var foodType = foodInfo[0];
                var quantity = int.Parse(foodInfo[1]);

                if (animalInfo[0] == "Owl")
                {
                    var name     = animalInfo[1];
                    var weight   = double.Parse(animalInfo[2]);
                    var wingSize = double.Parse(animalInfo[3]);

                    var animal = new Owl(name, weight, wingSize);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }

                else if (animalInfo[0] == "Hen")
                {
                    var name     = animalInfo[1];
                    var weight   = double.Parse(animalInfo[2]);
                    var wingSize = double.Parse(animalInfo[3]);

                    var animal = new Hen(name, weight, wingSize);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }

                else if (animalInfo[0] == "Mouse")
                {
                    var name         = animalInfo[1];
                    var weight       = double.Parse(animalInfo[2]);
                    var livingRegion = animalInfo[3];

                    var animal = new Mouse(name, weight, livingRegion);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }

                else if (animalInfo[0] == "Dog")
                {
                    var name         = animalInfo[1];
                    var weight       = double.Parse(animalInfo[2]);
                    var livingRegion = animalInfo[3];

                    var animal = new Dog(name, weight, livingRegion);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }

                else if (animalInfo[0] == "Cat")
                {
                    var name         = animalInfo[1];
                    var weight       = double.Parse(animalInfo[2]);
                    var livingRegion = animalInfo[3];
                    var breed        = animalInfo[4];

                    var animal = new Cat(name, weight, livingRegion, breed);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }

                else if (animalInfo[0] == "Tiger")
                {
                    var name         = animalInfo[1];
                    var weight       = double.Parse(animalInfo[2]);
                    var livingRegion = animalInfo[3];
                    var breed        = animalInfo[4];

                    var animal = new Tiger(name, weight, livingRegion, breed);
                    animal.ProduceSound();
                    animal.Eat(foodType, quantity);

                    animals.Add(animal);
                }
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
Beispiel #4
0
        public void Run()
        {
            while (true)
            {
                string input = Console.ReadLine();
                if (input == "End")
                {
                    break;
                }

                string[] tokens     = input.Split();
                string   typeAnimal = tokens[0];

                if (typeAnimal == "Cat")
                {
                    Animal   cat      = new Cat(tokens[1], double.Parse(tokens[2]), tokens[3], tokens[4]);
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    cat.Sound();
                    if (typeFood.ToLower() == "vegetable" || typeFood.ToLower() == "fruit")
                    {
                        cat.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Cat does not eat {typeFood}");
                    }
                    animals.Add(cat);
                }
                else if (typeAnimal == "Dog")
                {
                    Animal   dog      = new Dog(tokens[1], double.Parse(tokens[2]), tokens[3]);
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    dog.Sound();
                    if (typeFood.ToLower() == "meat")
                    {
                        dog.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Dog does not eat {typeFood}");
                    }
                    animals.Add(dog);
                }
                else if (typeAnimal == "Tiger")
                {
                    Animal   tiger    = new Tiger(tokens[1], double.Parse(tokens[2]), tokens[3], tokens[4]);
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    tiger.Sound();
                    if (typeFood.ToLower() == "meat")
                    {
                        tiger.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Tiger does not eat {typeFood}");
                    }
                    animals.Add(tiger);
                }
                else if (typeAnimal == "Mouse")
                {
                    Animal   mouse    = new Mouse(tokens[1], double.Parse(tokens[2]), tokens[3]);
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    mouse.Sound();
                    if (typeFood.ToLower() == "vegetable" || typeFood.ToLower() == "fruit")
                    {
                        mouse.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Mouse does not eat {typeFood}");
                    }
                    animals.Add(mouse);
                }
                else if (typeAnimal == "Owl")
                {
                    Animal   owl      = new Owl(tokens[1], double.Parse(tokens[2]), double.Parse(tokens[3]));
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    owl.Sound();
                    if (typeFood.ToLower() == "meat")
                    {
                        owl.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Mouse does not eat {typeFood}");
                    }
                    animals.Add(owl);
                }
                else if (typeAnimal == "Hen")
                {
                    Animal   hen      = new Hen(tokens[1], double.Parse(tokens[2]), double.Parse(tokens[3]));
                    string[] food     = Console.ReadLine().Split();
                    string   typeFood = food[0];
                    hen.Sound();
                    if (typeFood.ToLower() == "meat")
                    {
                        hen.Eat(int.Parse(food[1]));
                    }
                    else
                    {
                        Console.WriteLine($"Mouse does not eat {typeFood}");
                    }
                    animals.Add(hen);
                }
            }
            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
        static void Main(string[] args)
        {
            string         input   = String.Empty;
            List <IAnimal> animals = new List <IAnimal>();

            while ((input = Console.ReadLine()) != "End")
            {
                var inputTokens = input.Split();


                //ParseAnimal start
                var     animalType   = inputTokens[0];
                var     animalName   = inputTokens[1];
                var     animalWeight = double.Parse(inputTokens[2]);
                double  wingSize     = 0;
                string  livingRegion = String.Empty;
                string  breed        = String.Empty;
                IAnimal animal       = null;

                switch (animalType)
                {
                case "Owl":
                    wingSize = double.Parse(inputTokens[3]);
                    animal   = new Owl(animalName, animalWeight, wingSize);
                    break;

                case "Hen":
                    wingSize = double.Parse(inputTokens[3]);
                    animal   = new Hen(animalName, animalWeight, wingSize);
                    break;

                case "Mouse":
                    livingRegion = inputTokens[3];
                    animal       = new Mouse(animalName, animalWeight, livingRegion);
                    break;

                case "Dog":
                    livingRegion = inputTokens[3];
                    animal       = new Dog(animalName, animalWeight, livingRegion);
                    break;

                case "Cat":
                    livingRegion = inputTokens[3];
                    breed        = inputTokens[4];
                    animal       = new Cat(animalName, animalWeight, livingRegion, breed);
                    break;

                case "Tiger":
                    livingRegion = inputTokens[3];
                    breed        = inputTokens[4];
                    animal       = new Tiger(animalName, animalWeight, livingRegion, breed);
                    break;
                }
                animals.Add(animal);
                //ParseAnimal end

                //ParseFood start
                var foodTokens   = Console.ReadLine().Split();
                var foodType     = foodTokens[0];
                var foodQuantity = int.Parse(foodTokens[1]);

                animal.ProduceSound();
                animal.Eat(animal, foodType, foodQuantity);

                //ParseFood end
            }
            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            List <Animal> animals = new List <Animal>();

            string[] animalInput = Console.ReadLine().Split();
            string[] foodInput   = Console.ReadLine().Split();

            while (animalInput[0] != "End")
            {
                string type     = animalInput[0];
                string name     = animalInput[1];
                double weight   = double.Parse(animalInput[2]);
                string foodType = foodInput[0];
                int    quantity = int.Parse(foodInput[1]);

                if (type == nameof(Hen))
                {
                    double wingSize = double.Parse(animalInput[3]);

                    Bird hen = new Hen(name, weight, wingSize);
                    Console.WriteLine(hen.AskForFood());
                    hen.Eat(foodType, quantity);
                    animals.Add(hen);
                }
                else if (type == nameof(Owl))
                {
                    double wingSize = double.Parse(animalInput[3]);

                    Bird owl = new Owl(name, weight, wingSize);
                    Console.WriteLine(owl.AskForFood());
                    owl.Eat(foodType, quantity);
                    animals.Add(owl);
                }
                else if (type == nameof(Cat))
                {
                    string livingRegion = animalInput[3];
                    string breed        = animalInput[4];

                    Feline cat = new Cat(name, weight, livingRegion, breed);
                    Console.WriteLine(cat.AskForFood());
                    cat.Eat(foodType, quantity);
                    animals.Add(cat);
                }
                else if (type == nameof(Tiger))
                {
                    string livingRegion = animalInput[3];
                    string breed        = animalInput[4];

                    Feline tiger = new Tiger(name, weight, livingRegion, breed);
                    Console.WriteLine(tiger.AskForFood());
                    tiger.Eat(foodType, quantity);
                    animals.Add(tiger);
                }
                else if (type == nameof(Mouse))
                {
                    string livingRegion = animalInput[3];

                    Mammal mouse = new Mouse(name, weight, livingRegion);
                    Console.WriteLine(mouse.AskForFood());
                    mouse.Eat(foodType, quantity);
                    animals.Add(mouse);
                }
                else if (type == nameof(Dog))
                {
                    string livingRegion = animalInput[3];

                    Mammal dog = new Dog(name, weight, livingRegion);
                    Console.WriteLine(dog.AskForFood());
                    dog.Eat(foodType, quantity);
                    animals.Add(dog);
                }


                animalInput = Console.ReadLine().Split();
                if (animalInput[0] == "End")
                {
                    break;
                }
                foodInput = Console.ReadLine().Split();
            }

            foreach (var item in animals)
            {
                Console.WriteLine(item.ToString());
            }
        }
        static void Main()
        {
            var animalInfo = Console.ReadLine()
                             .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            while (!animalInfo[0].Equals("End"))
            {
                var foodInfo = Console.ReadLine()
                               .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                var animalName         = animalInfo[1];
                var animalWeight       = double.Parse(animalInfo[2]);
                var animalLivingRegion = animalInfo[3];

                var foodType     = foodInfo[0];
                var foodQuantity = int.Parse(foodInfo[1]);

                Food food;
                if (foodType.Equals("Vegetable"))
                {
                    food = new Vegetable(foodQuantity);
                }
                else
                {
                    food = new Meat(foodQuantity);
                }

                switch (animalInfo[0])
                {
                case "Cat":
                    var catBreed = animalInfo[4];

                    var cat = new Cat(animalName, animalWeight, animalLivingRegion, catBreed);
                    cat.MakeSound();
                    cat.Eat(food);
                    Console.WriteLine(cat);
                    break;

                case "Tiger":
                    var tiger = new Tiger(animalName, animalWeight, animalLivingRegion);
                    tiger.MakeSound();
                    tiger.Eat(food);
                    Console.WriteLine(tiger);
                    break;

                case "Zebra":
                    var zebra = new Zebra(animalName, animalWeight, animalLivingRegion);
                    zebra.MakeSound();
                    zebra.Eat(food);
                    Console.WriteLine(zebra);
                    break;

                case "Mouse":
                    var mouse = new Mouse(animalName, animalWeight, animalLivingRegion);
                    mouse.MakeSound();
                    mouse.Eat(food);
                    Console.WriteLine(mouse);
                    break;
                }

                animalInfo = Console.ReadLine()
                             .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            }
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            var animalList = new List <Animal>();

            while (true)
            {
                string[] input = Console.ReadLine().Split();
                if (input[0] == "End")
                {
                    break;
                }

                string animal = input[0];
                Food   food;
                switch (animal)
                {
                case nameof(Tiger):
                    var tiger = new Tiger(input[1], double.Parse(input[2]), input[3], input[4]);
                    Console.WriteLine(tiger.ProduceSound());

                    food = FoodFactory.Create(Console.ReadLine().Split());

                    try
                    {
                        tiger.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(tiger);
                    break;

                case "Cat":
                    var cat = new Cat(input[1], double.Parse(input[2]), input[3], input[4]);
                    Console.WriteLine(cat.ProduceSound());
                    food = FoodFactory.Create(Console.ReadLine().Split());

                    try
                    {
                        cat.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(cat);

                    break;

                case "Dog":
                    var dog = new Dog(input[1], double.Parse(input[2]), input[3]);
                    Console.WriteLine(dog.ProduceSound());
                    food = FoodFactory.Create(Console.ReadLine().Split());


                    try
                    {
                        dog.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(dog);

                    break;

                case "Mouse":
                    var mice = new Mouse(input[1], double.Parse(input[2]), input[3]);
                    Console.WriteLine(mice.ProduceSound());
                    food = FoodFactory.Create(Console.ReadLine().Split());


                    try
                    {
                        mice.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(mice);

                    break;

                case "Hen":
                    var hen = new Hen(input[1], double.Parse(input[2]), double.Parse(input[3]));
                    Console.WriteLine(hen.ProduceSound());
                    food = FoodFactory.Create(Console.ReadLine().Split());

                    try
                    {
                        hen.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(hen);

                    break;

                case "Owl":
                    var owl = new Owl(input[1], double.Parse(input[2]), double.Parse(input[3]));
                    Console.WriteLine(owl.ProduceSound());
                    food = FoodFactory.Create(Console.ReadLine().Split());

                    try
                    {
                        owl.Eat(food);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    animalList.Add(owl);

                    break;

                default:
                    break;
                }
            }
            animalList.ForEach(Console.WriteLine);

            //var hen = new Hen("GOGo", 2.5, 30);
            //Console.WriteLine(hen);
        }
        public static void Main()
        {
            var result = new StringBuilder();

            var animalList = new List <Animal>();

            var typeOfAnimals = new Dictionary <string, List <string> >();

            AddTypes(typeOfAnimals);

            while (true)
            {
                string input = Console.ReadLine();
                if (input == "End")
                {
                    break;
                }

                string[] animalToken = input.Split(' ');

                string type = animalToken[0];

                input = Console.ReadLine();

                string[] token = input.Split(' ');

                string typeOfFood = token[0];
                int    quantity   = int.Parse(token[1]);

                try
                {
                    // Birds
                    if (typeOfAnimals["Bird"].Contains(type))
                    {
                        string name         = animalToken[1];
                        double animalWeight = double.Parse(animalToken[2]);
                        double wingSize     = double.Parse(animalToken[3]);

                        switch (type)
                        {
                        case "Owl":
                            Owl owl = new Owl(name, animalWeight, wingSize);
                            result.AppendLine(owl.ProduceSound());
                            animalList.Add(owl);
                            owl.Eat(typeOfFood, quantity);
                            break;

                        case "Hen":
                            Hen hen = new Hen(name, animalWeight, wingSize);
                            result.AppendLine(hen.ProduceSound());
                            animalList.Add(hen);
                            hen.Eat(typeOfFood, quantity);
                            break;
                        }
                    }

                    // Mammals
                    if (typeOfAnimals["Mammal"].Contains(type))
                    {
                        string mammalName         = animalToken[1];
                        double mammalAnimalWeight = double.Parse(animalToken[2]);
                        string mammalLivingRegion = animalToken[3];

                        switch (type)
                        {
                        case "Mouse":
                            Mouse mouse = new Mouse(mammalName, mammalAnimalWeight, mammalLivingRegion);
                            animalList.Add(mouse);
                            result.AppendLine(mouse.ProduceSound());
                            mouse.Eat(typeOfFood, quantity);
                            break;

                        case "Dog":
                            Dog dog = new Dog(mammalName, mammalAnimalWeight, mammalLivingRegion);
                            result.AppendLine(dog.ProduceSound());
                            animalList.Add(dog);
                            dog.Eat(typeOfFood, quantity);
                            break;
                        }
                    }

                    // Feline
                    if (typeOfAnimals["Feline"].Contains(type))
                    {
                        string felineName         = animalToken[1];
                        double felineWeight       = double.Parse(animalToken[2]);
                        string felineLivingRegion = animalToken[3];
                        string felineBreed        = animalToken[4];

                        switch (type)
                        {
                        case "Cat":
                            Cat cat = new Cat(felineName, felineWeight, felineLivingRegion, felineBreed);
                            animalList.Add(cat);
                            result.AppendLine(cat.ProduceSound());
                            cat.Eat(typeOfFood, quantity);
                            break;

                        case "Tiger":
                            Tiger tiger = new Tiger(felineName, felineWeight, felineLivingRegion, felineBreed);
                            animalList.Add(tiger);
                            result.AppendLine(tiger.ProduceSound());
                            tiger.Eat(typeOfFood, quantity);
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    result.AppendLine(ex.Message);
                }
            }

            Console.Write(result.ToString());

            foreach (var animal in animalList)
            {
                Console.WriteLine(animal);
            }
        }
Beispiel #10
0
        public static void Main()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            string        inputAnimalInfo = Console.ReadLine();
            List <Animal> animals         = new List <Animal>();
            Stack <Food>  foods           = new Stack <Food>();

            while (inputAnimalInfo != "End")
            {
                string[] animalData = inputAnimalInfo.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                string animalType = animalData[0];
                string name       = animalData[1];
                double weight     = double.Parse(animalData[2]);
                string region     = animalData[3];

                string[] foodData = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                string   foodName = foodData[0];
                int      quantity = int.Parse(foodData[1]);

                if (foodName == "Meat")
                {
                    foods.Push(new Meat(quantity));
                }
                else
                {
                    foods.Push(new Vegatable(quantity));
                }

                if (animalType == "Cat")
                {
                    string cName = animalData[4];
                    string breed = animalData[1];

                    Animal cat = new Cat(cName, weight, region, breed);
                    cat.Eat(foods.Pop());
                    animals.Add(cat);
                }
                else
                {
                    switch (animalType)
                    {
                    case "Tiger":
                        Animal tiger = new Tiger(name, weight, region);
                        tiger.Eat(foods.Pop());
                        animals.Add(tiger);
                        break;

                    case "Zebra":
                        Animal zebra = new Zebra(name, weight, region);
                        zebra.Eat(foods.Pop());
                        animals.Add(zebra);
                        break;

                    case "Mouse":
                        Animal mouse = new Mouse(name, weight, region);
                        mouse.Eat(foods.Pop());
                        animals.Add(mouse);
                        break;
                    }
                }

                inputAnimalInfo = Console.ReadLine();
            }

            foreach (var animal in animals)
            {
                animal.MakeSound();

                Console.WriteLine(animal);
            }
        }