コード例 #1
0
        public static void Main()
        {
            List <Animal> animals = new List <Animal>();
            List <Food>   foods   = new List <Food>();

            int lineCounter = -1;

            while (true)
            {
                lineCounter++;//N.B. += 1
                string cmd = Console.ReadLine();
                if (cmd == "End")
                {
                    break;
                }

                string[] cmdArgs = cmd.Split(' ', StringSplitOptions.RemoveEmptyEntries);

                switch (cmdArgs[0])
                {
                case "Owl":
                    var owl = new Owl(cmdArgs[1], double.Parse(cmdArgs[2]), double.Parse(cmdArgs[3]));
                    animals.Add(owl);
                    break;

                case "Hen":
                    var hen = new Hen(cmdArgs[1], double.Parse(cmdArgs[2]), double.Parse(cmdArgs[3]));
                    animals.Add(hen);
                    break;

                case "Mouse":
                    var mouse = new Mouse(cmdArgs[1], double.Parse(cmdArgs[2]), cmdArgs[3]);
                    animals.Add(mouse);
                    break;

                case "Dog":
                    var dog = new Dog(cmdArgs[1], double.Parse(cmdArgs[2]), cmdArgs[3]);
                    animals.Add(dog);
                    break;

                case "Cat":
                    var cat = new Cat(cmdArgs[1], double.Parse(cmdArgs[2]), cmdArgs[3], cmdArgs[4]);
                    animals.Add(cat);
                    break;

                case "Tiger":
                    var tiger = new Tiger(cmdArgs[1], double.Parse(cmdArgs[2]), cmdArgs[3], cmdArgs[4]);
                    animals.Add(tiger);
                    break;

                case "Vegetable":
                    var vegetable = new Vegetable(int.Parse(cmdArgs[1]));
                    foods.Add(vegetable);
                    break;

                case "Fruit":
                    var fruit = new Fruit(int.Parse(cmdArgs[1]));
                    foods.Add(fruit);
                    break;

                case "Meat":
                    var meat = new Meat(int.Parse(cmdArgs[1]));
                    foods.Add(meat);
                    break;

                case "Seeds":
                    var seeds = new Seeds(int.Parse(cmdArgs[1]));
                    foods.Add(seeds);
                    break;
                }
            }

            for (int i = 0; i < animals.Count; i++)
            {
                try
                {
                    Console.WriteLine(animals[i].AskForFood());
                    animals[i].Eat(foods[i]);
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            animals.ForEach(Console.WriteLine);
        }
コード例 #2
0
ファイル: StartUp.cs プロジェクト: kirilsimeonov/OOP
        static void Main(string[] args)
        {
            var animals = new List <Animal>();

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

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

                Animal animal = null;
                Food   food   = null;

                string type   = animalInput[0];
                string name   = animalInput[1];
                double weight = double.Parse(animalInput[2]);

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

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

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

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

                case "Cat":
                    animal = new Cat(name, weight, animalInput[3], animalInput[4]);
                    break;

                case "Tiger":
                    animal = new Tiger(name, weight, animalInput[3], animalInput[4]);
                    break;

                default:
                    break;
                }

                string foodType     = foodInput[0];
                int    foodQuantity = int.Parse(foodInput[1]);

                switch (foodType)
                {
                case "Fruit":
                    food = new Fruit(foodQuantity);
                    break;

                case "Vegetable":
                    food = new Vegetable(foodQuantity);
                    break;

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

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

                default:
                    break;
                }

                Console.WriteLine(animal.AskForFood());
                animal.Feed(food);

                animals.Add(animal);
            }

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

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

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

                var    data       = cmd.Split(" ", StringSplitOptions.RemoveEmptyEntries);
                string animalType = data[0];
                string name       = data[1];
                double weight     = double.Parse(data[2]);

                Animal animal = null;

                switch (animalType)
                {
                case "Owl":
                {
                    int wingSize = int.Parse(data[3]);

                    animal = new Owl(name, weight, wingSize);
                    break;
                }

                case "Hen":
                {
                    int wingSize = int.Parse(data[3]);

                    animal = new Hen(name, weight, wingSize);
                    break;
                }

                case "Mouse":
                {
                    string region = data[3];

                    animal = new Mouse(name, weight, region);
                    break;
                }

                case "Dog":
                {
                    string region = data[3];

                    animal = new Dog(name, weight, region);
                    break;
                }

                case "Cat":
                {
                    string region = data[3];
                    string breed  = data[4];

                    animal = new Cat(name, weight, region, breed);
                    break;
                }

                case "Tiger":
                {
                    string region = data[3];
                    string breed  = data[4];

                    animal = new Tiger(name, weight, region, breed);
                    break;
                }

                default:
                    break;
                }

                Console.WriteLine(animal.ProduceSound());

                var foodData = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);

                string foodName = foodData[0];
                int    foodNum  = int.Parse(foodData[1]);

                Food food = null;

                switch (foodName)
                {
                case "Fruit":
                {
                    food = new Fruit(foodNum);

                    break;
                }

                case "Meat":
                {
                    food = new Meat(foodNum);
                    break;
                }

                case "Seeds":
                {
                    food = new Seeds(foodNum);
                    break;
                }

                case "Vegetable":
                {
                    food = new Vegetable(foodNum);
                    break;
                }

                default:
                    break;
                }

                animal.Feed(food);
                animals.Add(animal);
            }

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

            while (true)
            {
                string input = Console.ReadLine();
                if (input == "End")
                {
                    break;
                }
                string[] currentAnimal = input.Split(" ", StringSplitOptions.RemoveEmptyEntries);
                string[] currentFood   = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries);
                try
                {
                    Animal animal       = null;
                    string typeOfAnimal = currentAnimal[0];
                    string animalName   = currentAnimal[1];
                    double animalWeight = double.Parse(currentAnimal[2]);
                    double wingSize;
                    string livingRegion;
                    string breed;
                    switch (typeOfAnimal)
                    {
                    case "Hen":
                        wingSize = double.Parse(currentAnimal[3]);
                        animal   = new Hen(animalName, animalWeight, wingSize);
                        break;

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

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

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

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

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

                    Food   food         = null;
                    string typeOfFood   = currentFood[0];
                    int    foodQuantity = int.Parse(currentFood[1]);
                    switch (typeOfFood)
                    {
                    case "Vegetable":
                        food = new Vegetable(foodQuantity);
                        break;

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

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

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

                    default:
                        break;
                    }

                    Console.WriteLine(animal.ProduceSound());
                    animal.ValidateFood(food);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            foreach (Animal animal in animals)
            {
                Console.WriteLine(animal.ToString());
            }
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: BalthazzarCorr/Vehicles
        static void Main(string[] args)
        {
            var input = string.Empty;

            while ((input = Console.ReadLine()) != "End")
            {
                var animalInformation = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var foodInformation   = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                Animal currentAnimal = null;
                Food   currentFood   = null;

                var animalType   = animalInformation[0];
                var animalName   = animalInformation[1];
                var animalWeight = double.Parse(animalInformation[2]);
                var animalRegion = animalInformation[3];
                var animalBreed  = string.Empty;

                if (animalType == "Cat")
                {
                    animalBreed   = animalInformation[4];
                    currentAnimal = new Cat(animalName, animalType, animalWeight, animalRegion, animalBreed);
                }
                else
                {
                    switch (animalType)
                    {
                    case "Tiger":
                        currentAnimal = new Tiger(animalName, animalType, animalWeight, animalRegion);
                        break;

                    case "Zebra":
                        currentAnimal = new Zebra(animalName, animalType, animalWeight, animalRegion);
                        break;

                    case "Mouse":
                        currentAnimal = new Mouse(animalName, animalType, animalWeight, animalRegion);
                        break;
                    }
                }

                var foodType   = foodInformation[0];
                var foodAmount = int.Parse(foodInformation[1]);

                if (foodType == "Vegetable")
                {
                    currentFood = new Vegetable(foodAmount);
                }
                else if (foodType == "Meat")
                {
                    currentFood = new Meat(foodAmount);
                }

                Console.WriteLine(currentAnimal.MakeSound());

                try
                {
                    currentAnimal.Eat(currentFood);
                }
                catch (InvalidOperationException ioe)
                {
                    Console.WriteLine(ioe.Message);
                }

                Console.WriteLine(currentAnimal);
            }
        }
コード例 #6
0
        static void Main(string[] args)
        {
            var animalsCollect = new List <Animal>();

            string input;

            while ((input = Console.ReadLine()) != "End")
            {
                string[] animal = input.Split();
                string   type   = animal[0];
                string   name   = animal[1];
                double   weight = double.Parse(animal[2]);

                string[] foods    = Console.ReadLine().Split();
                string   typeFood = foods[0];
                int      quantity = int.Parse(foods[1]);

                Animal animals = null;
                switch (type)
                {
                case "Cat":
                    string livingRegionCat = animal[3];
                    string breedCat        = animal[4];
                    animals = new Cat(name, weight, livingRegionCat, breedCat);
                    break;

                case "Tiger":
                    string livingRegionTiger = animal[3];
                    string breedTiger        = animal[4];
                    animals = new Tiger(name, weight, livingRegionTiger, breedTiger);
                    break;

                case "Dog":
                    string livingRegionDog = animal[3];
                    animals = new Dog(name, weight, livingRegionDog);
                    break;

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

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

                case "Owl":
                    double wingSizeOwl = double.Parse(animal[3]);
                    animals = new Owl(name, weight, wingSizeOwl);
                    break;

                default:
                    break;
                }
                Console.WriteLine(animals.Sound());
                animalsCollect.Add(animals);

                Food food = null;
                try
                {
                    switch (typeFood)
                    {
                    case "Vegetable":
                        food = new Vegetable(quantity);
                        break;

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

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

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

                    default:
                        break;
                    }
                    animals.EatFood(food);
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            foreach (var animal in animalsCollect)
            {
                Console.WriteLine(animal);
            }
        }
コード例 #7
0
        static void Main(string[] args)
        {
            List <Animal> animals = new List <Animal>();

            string[] animalInput = Console.ReadLine().Split();
            while (animalInput[0].ToLower() != "end")
            {
                string animalType   = animalInput[0].ToLower();
                string animalName   = animalInput[1];
                double animalWeight = double.Parse(animalInput[2]);

                Animal animal = null;

                if (animalType == "hen")
                {
                    double wingSize = double.Parse(animalInput[3]);

                    animal = new Hen(animalName, animalWeight, wingSize);
                }
                else if (animalType == "owl")
                {
                    double wingSize = double.Parse(animalInput[3]);

                    animal = new Owl(animalName, animalWeight, wingSize);
                }
                else if (animalType == "mouse")
                {
                    string livingRegion = animalInput[3];

                    animal = new Mouse(animalName, animalWeight, livingRegion);
                }
                else if (animalType == "cat")
                {
                    string livingRegion = animalInput[3];
                    string breed        = animalInput[4];

                    animal = new Cat(animalName, animalWeight, livingRegion, breed);
                }
                else if (animalType == "dog")
                {
                    string livingRegion = animalInput[3];

                    animal = new Dog(animalName, animalWeight, livingRegion);
                }
                else if (animalType == "tiger")
                {
                    string livingRegion = animalInput[3];
                    string breed        = animalInput[4];

                    animal = new Tiger(animalName, animalWeight, livingRegion, breed);
                }
                Console.WriteLine(animal.ProduceSound());

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

                string foodType     = foodInput[0].ToLower();
                int    foodQuantity = int.Parse(foodInput[1]);

                Food food = null;

                if (foodType == "vegetable")
                {
                    food = new Vegetable(foodQuantity);
                }
                else if (foodType == "fruit")
                {
                    food = new Fruit(foodQuantity);
                }
                else if (foodType == "meat")
                {
                    food = new Meat(foodQuantity);
                }
                else if (foodType == "seeds")
                {
                    food = new Seeds(foodQuantity);
                }

                animals.Add(animal);

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

                animalInput = Console.ReadLine().Split();
            }
            foreach (var animal in animals)
            {
                Console.WriteLine(animal);
            }
        }