private static Food ParseFood(string[] foodTokens) { string foodType = foodTokens[0]; int quantity = int.Parse(foodTokens[1]); Food food = null; switch (foodType) { case "Meat": food = new Meat(quantity); break; case "Vegetable": food = new Vegetable(quantity); break; case "Fruit": food = new Fruit(quantity); break; case "Seeds": food = new Seeds(quantity); break; default: throw new ArgumentException("Invalid type of food!"); } return(food); }
private static Food ReadFoods(string[] food) { Food currentFood = null; switch (food[0]) { case "Vegetable": Food vegetable = new Vegetable(int.Parse(food[1])); currentFood = vegetable; break; case "Fruit": Food fruit = new Fruit(int.Parse(food[1])); currentFood = fruit; break; case "Meat": Food meat = new Meat(int.Parse(food[1])); currentFood = meat; break; case "Seeds": Food seeds = new Seeds(int.Parse(food[1])); currentFood = seeds; break; } return(currentFood); }
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(); } }