public override void Eat(Food food) { if (food is Meat) { Console.WriteLine($"{this.AnimalType}s are not eating that type of food!"); } else { Console.WriteLine("A cheese was just eaten!"); base.FoodEaten += food.quantity; } }
public override void Eat(Food food) { base.FoodEaten += food.quantity; }
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); } }
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()); } }
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); } }