public Animal CreateAnimal(string typeStr, string name, double weight, string[] args) { Animal animal = null; if (args.Length == 1) { bool isBird = double.TryParse(args[0], out double wingSize); if (isBird) { if (typeStr == "Owl") { animal = new Owl(name, weight, wingSize); animal.ProduceSound(); } else { animal = new Hen(name, weight, wingSize); animal.ProduceSound(); } } else { string livingRegion = args[0]; if (typeStr == "Mouse") { animal = new Mouse(name, weight, livingRegion); animal.ProduceSound(); } else { animal = new Dog(name, weight, livingRegion); animal.ProduceSound(); } } } else { string livingRegion = args[0]; string breed = args[1]; if (typeStr == "Cat") { animal = new Cat(name, weight, livingRegion, breed); animal.ProduceSound(); } else { animal = new Tiger(name, weight, livingRegion, breed); animal.ProduceSound(); } } return(animal); }
public static void Main() { var animals = new List <Animal>(); string inputLine; while ((inputLine = Console.ReadLine()) != "End") { var input = inputLine.Split(); Animal animal = null; Food food = null; var animalType = input[0]; var name = input[1]; var weight = double.Parse(input[2]); switch (animalType) { case nameof(Owl): animal = new Owl(name, weight, double.Parse(input[3])); break; case nameof(Hen): animal = new Hen(name, weight, double.Parse(input[3])); break; case nameof(Mouse): animal = new Mouse(name, weight, input[3]); break; case nameof(Dog): animal = new Dog(name, weight, input[3]); break; case nameof(Cat): animal = new Cat(name, weight, input[3], input[4]); break; case nameof(Tiger): animal = new Tiger(name, weight, input[3], input[4]); break; } inputLine = Console.ReadLine(); input = inputLine.Split(); var foodType = input[0]; var quantity = int.Parse(input[1]); switch (foodType) { case nameof(Vegetable): food = new Vegetable(quantity); break; case nameof(Fruit): food = new Fruit(quantity); break; case nameof(Meat): food = new Meat(quantity); break; case nameof(Seeds): food = new Seeds(quantity); break; } Console.WriteLine(animal.ProduceSound()); try { animal.EatFood(food); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } animals.Add(animal); } animals.ForEach(a => Console.WriteLine(a)); }
public static void Main(string[] args) { var input = Console.ReadLine(); var list = new List <Animal>(); while (input != "End") { var line = input.Split(); if (line[0] == "Cat") { Animal cat = new Cat(line[1], double.Parse(line[2]), 0, line[3], line[4]); var food = Console.ReadLine().Split(); Console.WriteLine(cat.ProduceSound()); if (food[0] == "Vegetable" || food[0] == "Meat") { cat.FoodEaten += int.Parse(food[1]); cat.Weight += 0.3 * int.Parse(food[1]); } else { Console.WriteLine($"Cat does not eat {food[0]}!"); } list.Add(cat); } if (line[0] == "Tiger") { Animal tiger = new Tiger(line[1], double.Parse(line[2]), 0, line[3], line[4]); var food = Console.ReadLine().Split(); Console.WriteLine(tiger.ProduceSound()); if (food[0] == "Meat") { tiger.FoodEaten += int.Parse(food[1]); tiger.Weight += 1 * int.Parse(food[1]); } else { Console.WriteLine($"Tiger does not eat {food[0]}!"); } list.Add(tiger); } if (line[0] == "Dog") { Animal dog = new Dog(line[1], double.Parse(line[2]), 0, line[3]); var food = Console.ReadLine().Split(); Console.WriteLine(dog.ProduceSound()); if (food[0] == "Meat") { dog.FoodEaten += int.Parse(food[1]); dog.Weight += 0.40 * int.Parse(food[1]); } else { Console.WriteLine($"Dog does not eat {food[0]}!"); } list.Add(dog); } if (line[0] == "Owl") { Animal owl = new Owl(line[1], double.Parse(line[2]), 0, double.Parse(line[3])); var food = Console.ReadLine().Split(); Console.WriteLine(owl.ProduceSound()); if (food[0] == "Meat") { owl.FoodEaten += int.Parse(food[1]); owl.Weight += 0.25 * int.Parse(food[1]); } else { Console.WriteLine($"Owl does not eat {food[0]}!"); } list.Add(owl); } if (line[0] == "Mouse") { Animal mouse = new Mouse(line[1], double.Parse(line[2]), 0, line[3]); var food = Console.ReadLine().Split(); Console.WriteLine(mouse.ProduceSound()); if (food[0] == "Fruit" || food[0] == "Vegetable") { mouse.FoodEaten += int.Parse(food[1]); mouse.Weight += 0.10 * int.Parse(food[1]); } else { Console.WriteLine($"Mouse does not eat {food[0]}!"); } list.Add(mouse); } if (line[0] == "Hen") { Animal hen = new Hen(line[1], double.Parse(line[2]), 0, double.Parse(line[3])); var food = Console.ReadLine().Split(); Console.WriteLine(hen.ProduceSound()); hen.FoodEaten += int.Parse(food[1]); hen.Weight += 0.35 * int.Parse(food[1]); list.Add(hen); } input = Console.ReadLine(); } foreach (var item in list) { Console.WriteLine(item); } }
static void Main(string[] args) { string input = String.Empty; List <IAnimal> animals = new List <IAnimal>(); while ((input = Console.ReadLine()) != "End") { var inputTokens = input.Split(); //ParseAnimal start var animalType = inputTokens[0]; var animalName = inputTokens[1]; var animalWeight = double.Parse(inputTokens[2]); double wingSize = 0; string livingRegion = String.Empty; string breed = String.Empty; IAnimal animal = null; switch (animalType) { case "Owl": wingSize = double.Parse(inputTokens[3]); animal = new Owl(animalName, animalWeight, wingSize); break; case "Hen": wingSize = double.Parse(inputTokens[3]); animal = new Hen(animalName, animalWeight, wingSize); break; case "Mouse": livingRegion = inputTokens[3]; animal = new Mouse(animalName, animalWeight, livingRegion); break; case "Dog": livingRegion = inputTokens[3]; animal = new Dog(animalName, animalWeight, livingRegion); break; case "Cat": livingRegion = inputTokens[3]; breed = inputTokens[4]; animal = new Cat(animalName, animalWeight, livingRegion, breed); break; case "Tiger": livingRegion = inputTokens[3]; breed = inputTokens[4]; animal = new Tiger(animalName, animalWeight, livingRegion, breed); break; } animals.Add(animal); //ParseAnimal end //ParseFood start var foodTokens = Console.ReadLine().Split(); var foodType = foodTokens[0]; var foodQuantity = int.Parse(foodTokens[1]); animal.ProduceSound(); animal.Eat(animal, foodType, foodQuantity); //ParseFood end } foreach (var animal in animals) { Console.WriteLine(animal); } }
static void Main(string[] args) { var animals = new List <Animal>(); while (true) { var command = Console.ReadLine(); if (command == "End") { break; } var animalInfo = command.Split(" ", StringSplitOptions.RemoveEmptyEntries); var foodInfo = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries); var foodType = foodInfo[0]; var quantity = int.Parse(foodInfo[1]); if (animalInfo[0] == "Owl") { var name = animalInfo[1]; var weight = double.Parse(animalInfo[2]); var wingSize = double.Parse(animalInfo[3]); var animal = new Owl(name, weight, wingSize); animal.ProduceSound(); animal.Eat(foodType, quantity); animals.Add(animal); } else if (animalInfo[0] == "Hen") { var name = animalInfo[1]; var weight = double.Parse(animalInfo[2]); var wingSize = double.Parse(animalInfo[3]); var animal = new Hen(name, weight, wingSize); animal.ProduceSound(); animal.Eat(foodType, quantity); animals.Add(animal); } else if (animalInfo[0] == "Mouse") { var name = animalInfo[1]; var weight = double.Parse(animalInfo[2]); var livingRegion = animalInfo[3]; var animal = new Mouse(name, weight, livingRegion); animal.ProduceSound(); animal.Eat(foodType, quantity); animals.Add(animal); } else if (animalInfo[0] == "Dog") { var name = animalInfo[1]; var weight = double.Parse(animalInfo[2]); var livingRegion = animalInfo[3]; var animal = new Dog(name, weight, livingRegion); animal.ProduceSound(); animal.Eat(foodType, quantity); animals.Add(animal); } else if (animalInfo[0] == "Cat") { var name = animalInfo[1]; var weight = double.Parse(animalInfo[2]); var livingRegion = animalInfo[3]; var breed = animalInfo[4]; var animal = new Cat(name, weight, livingRegion, breed); animal.ProduceSound(); animal.Eat(foodType, quantity); animals.Add(animal); } else if (animalInfo[0] == "Tiger") { var name = animalInfo[1]; var weight = double.Parse(animalInfo[2]); var livingRegion = animalInfo[3]; var breed = animalInfo[4]; var animal = new Tiger(name, weight, livingRegion, breed); animal.ProduceSound(); animal.Eat(foodType, quantity); animals.Add(animal); } } foreach (var animal in animals) { Console.WriteLine(animal); } }
static void Main(string[] args) { var animalList = new List <Animal>(); while (true) { string[] input = Console.ReadLine().Split(); if (input[0] == "End") { break; } string animal = input[0]; Food food; switch (animal) { case nameof(Tiger): var tiger = new Tiger(input[1], double.Parse(input[2]), input[3], input[4]); Console.WriteLine(tiger.ProduceSound()); food = FoodFactory.Create(Console.ReadLine().Split()); try { tiger.Eat(food); } catch (Exception ex) { Console.WriteLine(ex.Message); } animalList.Add(tiger); break; case "Cat": var cat = new Cat(input[1], double.Parse(input[2]), input[3], input[4]); Console.WriteLine(cat.ProduceSound()); food = FoodFactory.Create(Console.ReadLine().Split()); try { cat.Eat(food); } catch (Exception ex) { Console.WriteLine(ex.Message); } animalList.Add(cat); break; case "Dog": var dog = new Dog(input[1], double.Parse(input[2]), input[3]); Console.WriteLine(dog.ProduceSound()); food = FoodFactory.Create(Console.ReadLine().Split()); try { dog.Eat(food); } catch (Exception ex) { Console.WriteLine(ex.Message); } animalList.Add(dog); break; case "Mouse": var mice = new Mouse(input[1], double.Parse(input[2]), input[3]); Console.WriteLine(mice.ProduceSound()); food = FoodFactory.Create(Console.ReadLine().Split()); try { mice.Eat(food); } catch (Exception ex) { Console.WriteLine(ex.Message); } animalList.Add(mice); break; case "Hen": var hen = new Hen(input[1], double.Parse(input[2]), double.Parse(input[3])); Console.WriteLine(hen.ProduceSound()); food = FoodFactory.Create(Console.ReadLine().Split()); try { hen.Eat(food); } catch (Exception ex) { Console.WriteLine(ex.Message); } animalList.Add(hen); break; case "Owl": var owl = new Owl(input[1], double.Parse(input[2]), double.Parse(input[3])); Console.WriteLine(owl.ProduceSound()); food = FoodFactory.Create(Console.ReadLine().Split()); try { owl.Eat(food); } catch (Exception ex) { Console.WriteLine(ex.Message); } animalList.Add(owl); break; default: break; } } animalList.ForEach(Console.WriteLine); //var hen = new Hen("GOGo", 2.5, 30); //Console.WriteLine(hen); }
public static void Main() { var result = new StringBuilder(); var animalList = new List <Animal>(); var typeOfAnimals = new Dictionary <string, List <string> >(); AddTypes(typeOfAnimals); while (true) { string input = Console.ReadLine(); if (input == "End") { break; } string[] animalToken = input.Split(' '); string type = animalToken[0]; input = Console.ReadLine(); string[] token = input.Split(' '); string typeOfFood = token[0]; int quantity = int.Parse(token[1]); try { // Birds if (typeOfAnimals["Bird"].Contains(type)) { string name = animalToken[1]; double animalWeight = double.Parse(animalToken[2]); double wingSize = double.Parse(animalToken[3]); switch (type) { case "Owl": Owl owl = new Owl(name, animalWeight, wingSize); result.AppendLine(owl.ProduceSound()); animalList.Add(owl); owl.Eat(typeOfFood, quantity); break; case "Hen": Hen hen = new Hen(name, animalWeight, wingSize); result.AppendLine(hen.ProduceSound()); animalList.Add(hen); hen.Eat(typeOfFood, quantity); break; } } // Mammals if (typeOfAnimals["Mammal"].Contains(type)) { string mammalName = animalToken[1]; double mammalAnimalWeight = double.Parse(animalToken[2]); string mammalLivingRegion = animalToken[3]; switch (type) { case "Mouse": Mouse mouse = new Mouse(mammalName, mammalAnimalWeight, mammalLivingRegion); animalList.Add(mouse); result.AppendLine(mouse.ProduceSound()); mouse.Eat(typeOfFood, quantity); break; case "Dog": Dog dog = new Dog(mammalName, mammalAnimalWeight, mammalLivingRegion); result.AppendLine(dog.ProduceSound()); animalList.Add(dog); dog.Eat(typeOfFood, quantity); break; } } // Feline if (typeOfAnimals["Feline"].Contains(type)) { string felineName = animalToken[1]; double felineWeight = double.Parse(animalToken[2]); string felineLivingRegion = animalToken[3]; string felineBreed = animalToken[4]; switch (type) { case "Cat": Cat cat = new Cat(felineName, felineWeight, felineLivingRegion, felineBreed); animalList.Add(cat); result.AppendLine(cat.ProduceSound()); cat.Eat(typeOfFood, quantity); break; case "Tiger": Tiger tiger = new Tiger(felineName, felineWeight, felineLivingRegion, felineBreed); animalList.Add(tiger); result.AppendLine(tiger.ProduceSound()); tiger.Eat(typeOfFood, quantity); break; } } } catch (Exception ex) { result.AppendLine(ex.Message); } } Console.Write(result.ToString()); foreach (var animal in animalList) { Console.WriteLine(animal); } }
static void Main(string[] args) { List <Animal> animals = new List <Animal>(); while (true) { string[] animalArgs = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); string type = animalArgs[0]; if (type == "End") { break; } string name = animalArgs[1]; double weight = double.Parse(animalArgs[2]); string[] foodArgs = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); string food = foodArgs[0]; int foodQuantity = int.Parse(foodArgs[1]); if (animalArgs.Length == 5) { string livingRegion = animalArgs[3]; string breed = animalArgs[4]; if (type == "Cat") { Cat cat = new Cat(name, weight, livingRegion, breed); Console.WriteLine(cat.ProduceSound()); animals.Add(cat); try { cat.FeedAnimal(ReturnFood(food, foodQuantity)); } catch (Exception ex) { Console.WriteLine(ex.Message); } } else if (type == "Tiger") { Tiger tiger = new Tiger(name, weight, livingRegion, breed); Console.WriteLine(tiger.ProduceSound()); animals.Add(tiger); try { tiger.FeedAnimal(ReturnFood(food, foodQuantity)); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } else if (double.TryParse(animalArgs[3], out double wingsSize)) { if (type == "Hen") { Hen hen = new Hen(name, weight, wingsSize); Console.WriteLine(hen.ProduceSound()); animals.Add(hen); try { hen.FeedAnimal(ReturnFood(food, foodQuantity)); } catch (Exception ex) { Console.WriteLine(ex.Message); } } else if (type == "Owl") { Owl owl = new Owl(name, weight, wingsSize); Console.WriteLine(owl.ProduceSound()); animals.Add(owl); try { owl.FeedAnimal(ReturnFood(food, foodQuantity)); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } else { string livingRegion = animalArgs[3]; if (type == "Dog") { Dog dog = new Dog(name, weight, livingRegion); Console.WriteLine(dog.ProduceSound()); animals.Add(dog); try { dog.FeedAnimal(ReturnFood(food, foodQuantity)); } catch (Exception ex) { Console.WriteLine(ex.Message); } } else if (type == "Mouse") { Mouse mouse = new Mouse(name, weight, livingRegion); Console.WriteLine(mouse.ProduceSound()); animals.Add(mouse); try { mouse.FeedAnimal(ReturnFood(food, foodQuantity)); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } } foreach (var animal in animals) { Console.WriteLine(animal.ToString()); } }
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>(); while (true) { string input = Console.ReadLine(); if (input == "End") { break; } string[] currentAnimal = input.Split(" ", StringSplitOptions.RemoveEmptyEntries); string[] currentFood = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries); try { Animal animal = null; string typeOfAnimal = currentAnimal[0]; string animalName = currentAnimal[1]; double animalWeight = double.Parse(currentAnimal[2]); double wingSize; string livingRegion; string breed; switch (typeOfAnimal) { case "Hen": wingSize = double.Parse(currentAnimal[3]); animal = new Hen(animalName, animalWeight, wingSize); break; case "Owl": wingSize = double.Parse(currentAnimal[3]); animal = new Owl(animalName, animalWeight, wingSize); break; case "Dog": livingRegion = currentAnimal[3]; animal = new Dog(animalName, animalWeight, livingRegion); break; case "Mouse": livingRegion = currentAnimal[3]; animal = new Mouse(animalName, animalWeight, livingRegion); break; case "Cat": livingRegion = currentAnimal[3]; breed = currentAnimal[4]; animal = new Cat(animalName, animalWeight, livingRegion, breed); break; case "Tiger": livingRegion = currentAnimal[3]; breed = currentAnimal[4]; animal = new Tiger(animalName, animalWeight, livingRegion, breed); break; } animals.Add(animal); Food food = null; string typeOfFood = currentFood[0]; int foodQuantity = int.Parse(currentFood[1]); switch (typeOfFood) { case "Vegetable": food = new Vegetable(foodQuantity); break; case "Seeds": food = new Seeds(foodQuantity); break; case "Fruit": food = new Fruit(foodQuantity); break; case "Meat": food = new Meat(foodQuantity); break; default: break; } Console.WriteLine(animal.ProduceSound()); animal.ValidateFood(food); } catch (Exception ex) { Console.WriteLine(ex.Message); } } foreach (Animal animal in animals) { Console.WriteLine(animal.ToString()); } }