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(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) { 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()); } }