static void Main(string[] args) { Animal animal = null; Food food = null; string input = Console.ReadLine(); //string foods = Console.ReadLine(); while (input != "End") { string[] line = input.Split(new[] { ' ', '\n', '\t' }, StringSplitOptions.RemoveEmptyEntries); string[] str = Console.ReadLine().Split(' '); string type = line[0]; string name = line[1]; double weight = double.Parse(line[2]); string region = line[3]; switch (type.ToLower()) { case "cat": string breed = line[4]; animal = new Cat(name, type, weight, region, breed); break; case "mouse": animal = new Mouse(name, type, weight, region); break; case "tiger": animal = new Tiger(name, type, weight, region); break; case "zebra": animal = new Zebra(name, type, weight, region); break; } var vg = str[0]; int r = int.Parse(str[1]); if (vg == "Meat") { food = new Meat(r); } else { food = new Vegetable(r); } animal.MakeSound(); animal.Eat(food); Console.WriteLine(animal); input = Console.ReadLine(); } }
static void Main() { var animalInfo = Console.ReadLine() .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); while (!animalInfo[0].Equals("End")) { var foodInfo = Console.ReadLine() .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var animalName = animalInfo[1]; var animalWeight = double.Parse(animalInfo[2]); var animalLivingRegion = animalInfo[3]; var foodType = foodInfo[0]; var foodQuantity = int.Parse(foodInfo[1]); Food food; if (foodType.Equals("Vegetable")) { food = new Vegetable(foodQuantity); } else { food = new Meat(foodQuantity); } switch (animalInfo[0]) { case "Cat": var catBreed = animalInfo[4]; var cat = new Cat(animalName, animalWeight, animalLivingRegion, catBreed); cat.MakeSound(); cat.Eat(food); Console.WriteLine(cat); break; case "Tiger": var tiger = new Tiger(animalName, animalWeight, animalLivingRegion); tiger.MakeSound(); tiger.Eat(food); Console.WriteLine(tiger); break; case "Zebra": var zebra = new Zebra(animalName, animalWeight, animalLivingRegion); zebra.MakeSound(); zebra.Eat(food); Console.WriteLine(zebra); break; case "Mouse": var mouse = new Mouse(animalName, animalWeight, animalLivingRegion); mouse.MakeSound(); mouse.Eat(food); Console.WriteLine(mouse); break; } animalInfo = Console.ReadLine() .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); } }
public static void Main() { Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; string inputAnimalInfo = Console.ReadLine(); List <Animal> animals = new List <Animal>(); Stack <Food> foods = new Stack <Food>(); while (inputAnimalInfo != "End") { string[] animalData = inputAnimalInfo.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string animalType = animalData[0]; string name = animalData[1]; double weight = double.Parse(animalData[2]); string region = animalData[3]; string[] foodData = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string foodName = foodData[0]; int quantity = int.Parse(foodData[1]); if (foodName == "Meat") { foods.Push(new Meat(quantity)); } else { foods.Push(new Vegatable(quantity)); } if (animalType == "Cat") { string cName = animalData[4]; string breed = animalData[1]; Animal cat = new Cat(cName, weight, region, breed); cat.Eat(foods.Pop()); animals.Add(cat); } else { switch (animalType) { case "Tiger": Animal tiger = new Tiger(name, weight, region); tiger.Eat(foods.Pop()); animals.Add(tiger); break; case "Zebra": Animal zebra = new Zebra(name, weight, region); zebra.Eat(foods.Pop()); animals.Add(zebra); break; case "Mouse": Animal mouse = new Mouse(name, weight, region); mouse.Eat(foods.Pop()); animals.Add(mouse); break; } } inputAnimalInfo = Console.ReadLine(); } foreach (var animal in animals) { animal.MakeSound(); Console.WriteLine(animal); } }