static void Main() { var animals = new List <Animal>(); string animalArgs = string.Empty; while ((animalArgs = Console.ReadLine()) != "End") { var args = animalArgs.Split(); var animalType = args[0]; Animal animal; var feeding = Console.ReadLine().Split(); var foodType = feeding[0]; var quantity = int.Parse(feeding[1]); try { switch (animalType) { case "Cat": animal = new Cat(args[1], double.Parse(args[2]), args[3], args[4]); animals.Add(animal); Console.WriteLine(animal.ProduceSound()); animal.EatFood(foodType, quantity); break; case "Tiger": animal = new Tiger(args[1], double.Parse(args[2]), args[3], args[4]); animals.Add(animal); Console.WriteLine(animal.ProduceSound()); animal.EatFood(foodType, quantity); break; case "Owl": animal = new Owl(args[1], double.Parse(args[2]), double.Parse(args[3])); animals.Add(animal); Console.WriteLine(animal.ProduceSound()); animal.EatFood(foodType, quantity); break; case "Hen": animal = new Hen(args[1], double.Parse(args[2]), double.Parse(args[3])); animals.Add(animal); Console.WriteLine(animal.ProduceSound()); animal.EatFood(foodType, quantity); break; case "Mouse": animal = new Mouse(args[1], double.Parse(args[2]), args[3]); animals.Add(animal); Console.WriteLine(animal.ProduceSound()); animal.EatFood(foodType, quantity); break; case "Dog": animal = new Dog(args[1], double.Parse(args[2]), args[3]); animals.Add(animal); Console.WriteLine(animal.ProduceSound()); animal.EatFood(foodType, quantity); break; default: break; } } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } } animals.ForEach(a => Console.WriteLine(a)); }
private static void FillAnimals(string command, List <Animal> animals) { var tokens = command.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); var foodTokens = Console.ReadLine().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); var typeFood = foodTokens[0]; var quantity = int.Parse(foodTokens[1]); Food food = null; 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; } if (tokens.Length == 5) { try { switch (tokens[0]) { case "Cat": var cat = new Cat(tokens[1], double.Parse(tokens[2]), 0, tokens[3], tokens[4]); cat.ProduceSound(); animals.Add(cat); if (cat.IsEaten(food)) { cat.EatFood(quantity); } break; case "Tiger": var tiger = new Tiger(tokens[1], double.Parse(tokens[2]), 0, tokens[3], tokens[4]); tiger.ProduceSound(); animals.Add(tiger); if (tiger.IsEaten(food)) { tiger.EatFood(quantity); } break; } } catch (Exception e) { Console.WriteLine(e.Message); } } else if (tokens.Length == 4) { try { switch (tokens[0]) { case "Mouse": var mouse = new Mouse(tokens[1], double.Parse(tokens[2]), 0, tokens[3]); mouse.ProduceSound(); animals.Add(mouse); if (mouse.IsEaten(food)) { mouse.EatFood(quantity); } break; case "Dog": var dog = new Dog(tokens[1], double.Parse(tokens[2]), 0, tokens[3]); dog.ProduceSound(); animals.Add(dog); if (dog.IsEaten(food)) { dog.EatFood(quantity); } break; case "Hen": var hen = new Hen(tokens[1], double.Parse(tokens[2]), 0, double.Parse(tokens[3])); hen.ProduceSound(); animals.Add(hen); if (hen.IsEaten(food)) { hen.EatFood(quantity); } break; case "Owl": var owl = new Owl(tokens[1], double.Parse(tokens[2]), 0, double.Parse(tokens[3])); owl.ProduceSound(); animals.Add(owl); if (owl.IsEaten(food)) { owl.EatFood(quantity); } break; } } catch (Exception e) { Console.WriteLine(e.Message); } } }
public void Run() { List <IAnimal> animals = new List <IAnimal>(); string input; while ((input = Console.ReadLine()) != "End") { string[] animalTokens = input.Split(); string animalType = animalTokens[0]; string name = animalTokens[1]; double weight = double.Parse(animalTokens[2]); IAnimal currAnimal = null; switch (animalType) { case nameof(Owl): double wingSize = double.Parse(animalTokens[3]); currAnimal = new Owl(name, weight, wingSize); break; case nameof(Hen): wingSize = double.Parse(animalTokens[3]); currAnimal = new Hen(name, weight, wingSize); break; case nameof(Mouse): string livingRegion = animalTokens[3]; currAnimal = new Mouse(name, weight, livingRegion); break; case nameof(Dog): livingRegion = animalTokens[3]; currAnimal = new Dog(name, weight, livingRegion); break; case nameof(Cat): livingRegion = animalTokens[3]; string breed = animalTokens[4]; currAnimal = new Cat(name, weight, livingRegion, breed); break; case nameof(Tiger): livingRegion = animalTokens[3]; breed = animalTokens[4]; currAnimal = new Tiger(name, weight, livingRegion, breed); break; default: break; } string[] foodTokens = Console.ReadLine().Split(); string foodType = foodTokens[0]; int quantity = int.Parse(foodTokens[1]); IFood food = null; switch (foodType) { case nameof(Meat): food = new Meat(quantity); break; case nameof(Vegetable): food = new Vegetable(quantity); break; case nameof(Seeds): food = new Seeds(quantity); break; case nameof(Fruit): food = new Fruit(quantity); break; default: break; } Console.WriteLine(currAnimal.AskForFood()); bool hasEaten = currAnimal.EatFood(food); if (!hasEaten) { Console.WriteLine($"{animalType} does not eat {foodType}!"); } animals.Add(currAnimal); } foreach (IAnimal animal in animals) { Console.WriteLine(animal.ToString()); } }