private static Animal ParseAnimal(string input) { string[] animalTokens = input.Split(); string animalType = animalTokens[0]; string name = animalTokens[1]; double weight = double.Parse(animalTokens[2]); string thirdToken = animalTokens[3]; Animal animal = null; switch (animalType) { case "Cat": string catBreed = animalTokens[4]; animal = new Cat(name, weight, thirdToken, catBreed); break; case "Tiger": string tigerBreed = animalTokens[4]; animal = new Tiger(name, weight, thirdToken, tigerBreed); break; case "Dog": animal = new Dog(name, weight, thirdToken); break; case "Mouse": animal = new Mouse(name, weight, thirdToken); break; case "Owl": double owlWingSize = double.Parse(thirdToken); animal = new Owl(name, weight, owlWingSize); break; case "Hen": double henWingSize = double.Parse(thirdToken); animal = new Hen(name, weight, henWingSize); break; default: throw new ArgumentException("Invalid animal type!"); } return(animal); }
public static void Main() { 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); } }
public static void Main() { var animalInfo = Console.ReadLine(); while (animalInfo != "End") { var animalTokens = animalInfo.Split(); var animalType = animalTokens[0]; var animalName = animalTokens[1]; var animalWeight = double.Parse(animalTokens[2]); var animalRegion = animalTokens[3]; Animal animal = null; switch (animalType) { case "Cat": var breed = animalTokens[4]; animal = new Cat(animalName, animalType, animalWeight, animalRegion, breed); break; case "Tiger": animal = new Tiger(animalName, animalType, animalWeight, animalRegion); break; case "Zebra": animal = new Zebra(animalName, animalType, animalWeight, animalRegion); break; case "Mouse": animal = new Mouse(animalName, animalType, animalWeight, animalRegion); break; } Console.WriteLine(animal.MakeSound()); var foodInfo = Console.ReadLine().Split(); var foodType = foodInfo[0]; var foodQuantity = int.Parse(foodInfo[1]); Food food = null; if (foodType == "Vegetable") { food = new Vegetable(foodQuantity); } else if (foodType == "Meat") { food = new Meat(foodQuantity); } try { animal.Eat(food); } catch (ArgumentException ae) { Console.WriteLine(ae.Message); } Console.WriteLine(animal); animalInfo = Console.ReadLine(); } }
public static void Main() { string animalInfo = ""; string foodInfo = ""; while (true) { animalInfo = Console.ReadLine(); if (animalInfo == "End") { break; } foodInfo = Console.ReadLine(); string[] animalArgs = animalInfo.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string animalType = animalArgs[0]; string animalName = animalArgs[1]; double animalWeight = double.Parse(animalArgs[2]); string animalLivingRegion = animalArgs[3]; string catBreed = ""; if (animalArgs.Length == 5) { catBreed = animalArgs[4]; } string[] foodArgs = foodInfo.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string foodType = foodArgs[0]; int quantiy = int.Parse(foodArgs[1]); Food currentFood; if (foodType == "Vegetable") { currentFood = new Vegetable(quantiy); } else { currentFood = new Meat(quantiy); } switch (animalType) { case "Mouse": Animal mouse = new Mouse(animalType, animalName, animalWeight, animalLivingRegion); mouse.MakeSound(); mouse.Eat(currentFood); Console.WriteLine(mouse); break; case "Cat": Animal cat = new Cat(animalType, animalName, animalWeight, animalLivingRegion, catBreed); cat.MakeSound(); cat.Eat(currentFood); Console.WriteLine(cat); break; case "Tiger": Animal tiger = new Tiger(animalType, animalName, animalWeight, animalLivingRegion); tiger.MakeSound(); tiger.Eat(currentFood); Console.WriteLine(tiger); break; case "Zebra": Animal zebra = new Zebra(animalType, animalName, animalWeight, animalLivingRegion); zebra.MakeSound(); zebra.Eat(currentFood); Console.WriteLine(zebra); break; default: break; } } }
static void Main(string[] args) { string[] inputAnimal = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string[] inputFood = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); List <Animal> animals = new List <Animal>(); while (inputAnimal[0] != "End") { string getTypeAnimal = inputAnimal[0].ToLower(); string name = inputAnimal[1]; double weight = double.Parse(inputAnimal[2]); string livingRegion = inputAnimal[3]; string getTypeFood = inputFood[0].ToLower(); int foodQuantity = int.Parse(inputFood[1]); switch (getTypeAnimal) { case "cat": string breed = inputAnimal[4]; Cat cat = new Cat(name, weight, livingRegion, breed); cat.MakeSound(); animals.Add(cat); switch (getTypeFood) { case "vegetable": Vegetable veg = new Vegetable(foodQuantity); cat.Eat(veg); Console.WriteLine(cat); break; case "meat": Meat meat = new Meat(foodQuantity); cat.Eat(meat); Console.WriteLine(cat); break; } break; case "tiger": Tiger tiger = new Tiger(name, weight, livingRegion); tiger.MakeSound(); animals.Add(tiger); switch (getTypeFood) { case "vegetable": Vegetable veg = new Vegetable(foodQuantity); tiger.Eat(veg); Console.WriteLine(tiger); break; case "meat": Meat meat = new Meat(foodQuantity); tiger.Eat(meat); Console.WriteLine(tiger); break; } break; case "mouse": Mouse mouse = new Mouse(name, weight, livingRegion); mouse.MakeSound(); switch (getTypeFood) { case "vegetable": Vegetable veg = new Vegetable(foodQuantity); mouse.Eat(veg); Console.WriteLine(mouse); break; case "meat": Meat meat = new Meat(foodQuantity); mouse.Eat(meat); Console.WriteLine(mouse); break; } break; case "zebra": Zebra zebra = new Zebra(name, weight, livingRegion); zebra.MakeSound(); switch (getTypeFood) { case "vegetable": Vegetable veg = new Vegetable(foodQuantity); zebra.Eat(veg); Console.WriteLine(zebra); break; case "meat": Meat meat = new Meat(foodQuantity); zebra.Eat(meat); Console.WriteLine(zebra); break; } break; } inputAnimal = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (inputAnimal[0] == "End") { break; } inputFood = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); } foreach (var animal in animals) { //animal.MakeSound(); } }
public static void Main() { while (true) { var animalInfo = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (animalInfo[0] == "End") { break; } var foodInfo = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); Food food; switch (animalInfo[0]) { case "Mouse": var mouse = new Mouse(animalInfo[1], double.Parse(animalInfo[2]), animalInfo[3]); food = Food.FoodFactory(foodInfo[0], int.Parse(foodInfo[1])); mouse.makeSound(); try { mouse.AnimalType = "Mouse"; if (foodInfo[0] != "Vegetable") { mouse.FoodEaten = 0; throw new ArgumentException($"Mouses are not eating that type of food!"); } else { mouse.eatFood(food); } } catch (ArgumentException e) { Console.WriteLine(e.Message); } Console.WriteLine(mouse); break; case "Zebra": var zebra = new Zebra(animalInfo[1], double.Parse(animalInfo[2]), animalInfo[3]); food = Food.FoodFactory(foodInfo[0], int.Parse(foodInfo[1])); zebra.makeSound(); try { zebra.AnimalType = "Zebra"; if (foodInfo[0] != "Vegetable") { zebra.FoodEaten = 0; throw new ArgumentException($"Zebras are not eating that type of food!"); } else { zebra.eatFood(food); } } catch (ArgumentException e) { Console.WriteLine(e.Message); } Console.WriteLine(zebra); break; case "Cat": var cat = new Cat(animalInfo[1], double.Parse(animalInfo[2]), animalInfo[3], animalInfo[4]); cat.AnimalType = "Cat"; food = Food.FoodFactory(foodInfo[0], int.Parse(foodInfo[1])); cat.makeSound(); cat.eatFood(food); Console.WriteLine(cat); break; case "Tiger": var tiger = new Tiger(animalInfo[1], double.Parse(animalInfo[2]), animalInfo[3]); tiger.makeSound(); food = Food.FoodFactory(foodInfo[0], int.Parse(foodInfo[1])); try { tiger.AnimalType = "Tiger"; if (foodInfo[0] == "Vegetable") { tiger.FoodEaten = 0; throw new ArgumentException($"Tigers are not eating that type of food!"); } else { tiger.eatFood(food); } } catch (ArgumentException e) { Console.WriteLine(e.Message); } Console.WriteLine(tiger); break; } } }