public void Run() { var animals = new List <Animal>(); var counter = 0; while (true) { var command = Console.ReadLine().Split(); if (command[0] == "End") { break; } if (counter % 2 == 0) { if (command[0] == "Hen") { var name = command[1]; var weight = double.Parse(command[2]); var wingSize = double.Parse(command[3]); var currHen = new Hen(name, weight, wingSize); animals.Add(currHen); } else if (command[0] == "Owl") { var name = command[1]; var weight = double.Parse(command[2]); var wingSize = double.Parse(command[3]); var currOwl = new Owl(name, weight, wingSize); animals.Add(currOwl); } else if (command[0] == "Mouse") { var name = command[1]; var weight = double.Parse(command[2]); var livingRegion = command[3]; var currMouse = new Mouse(name, weight, livingRegion); animals.Add(currMouse); } else if (command[0] == "Cat") { var name = command[1]; var weight = double.Parse(command[2]); var livingRegion = command[3]; var breed = command[4]; var currCat = new Cat(name, weight, livingRegion, breed); animals.Add(currCat); } else if (command[0] == "Dog") { var name = command[1]; var weight = double.Parse(command[2]); var livingRegion = command[3]; var currDog = new Dog(name, weight, livingRegion); animals.Add(currDog); } else if (command[0] == "Tiger") { var name = command[1]; var weight = double.Parse(command[2]); var livingRegion = command[3]; var breed = command[4]; var currTiger = new Tiger(name, weight, livingRegion, breed); animals.Add(currTiger); } Console.WriteLine(animals.Last().ProduceSound()); } else { var lastAnimal = animals.Last().GetType().Name; if (command[0] == "Fruit") { var quantity = int.Parse(command[1]); var currFruit = new Fruit(quantity); if (lastAnimal == "Hen" || lastAnimal == "Mouse") { HenEating(animals, quantity); MouseEating(animals, quantity); } else { Console.WriteLine($"{lastAnimal} does not eat Fruit!"); } } else if (command[0] == "Meat") { var quantity = int.Parse(command[1]); var currMeat = new Meat(quantity); if (lastAnimal == "Hen" || lastAnimal == "Cat" || lastAnimal == "Tiger" || lastAnimal == "Dog" || lastAnimal == "Owl") { HenEating(animals, quantity); CatEating(animals, quantity); TigerEating(animals, quantity); DogEating(animals, quantity); OwlEating(animals, quantity); } else { Console.WriteLine($"{lastAnimal} does not eat Meat!"); } } else if (command[0] == "Seeds") { var quantity = int.Parse(command[1]); var currSeeds = new Seeds(quantity); if (lastAnimal == "Hen") { HenEating(animals, quantity); } else { Console.WriteLine($"{lastAnimal} does not eat Seeds!"); } } else if (command[0] == "Vegetable") { var quantity = int.Parse(command[1]); var currVegetable = new Vegetable(quantity); if (lastAnimal == "Mouse" || lastAnimal == "Hen" || lastAnimal == "Cat") { HenEating(animals, quantity); MouseEating(animals, quantity); CatEating(animals, quantity); } else { Console.WriteLine($"{lastAnimal} does not eat Vegetable!"); } } } counter++; } 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(string[] args) { List <Animal> animals = new List <Animal>(); string animalInfo = string.Empty; while ((animalInfo = Console.ReadLine()) != "End") { string[] info = animalInfo.Split(" ", StringSplitOptions.RemoveEmptyEntries); Animal animal = null; string animalType = info[0]; string animalName = info[1]; double weight = double.Parse(info[2]); if (animalType == "Cat" || animalType == "Tiger") { string livingRegion = info[3]; string breed = info[4]; if (animalType == "Cat") { animal = new Cat(animalName, weight, livingRegion, breed); } else { animal = new Tiger(animalName, weight, livingRegion, breed); } } else if (animalType == "Owl" || animalType == "Hen") { double wingSize = double.Parse(info[3]); if (animalType == "Owl") { animal = new Owl(animalName, weight, wingSize); } else { animal = new Hen(animalName, weight, wingSize); } } else if (animalType == "Mouse" || animalType == "Dog") { string livingRegion = info[3]; if (animalType == "Mouse") { animal = new Mouse(animalName, weight, livingRegion); } else { animal = new Dog(animalName, weight, livingRegion); } } animals.Add(animal); Console.WriteLine(animal.ProduceSound()); string[] foodInfo = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries); string foodType = foodInfo[0]; int quantity = int.Parse(foodInfo[1]); Food food = null; if (foodType == "Vegetable") { food = new Vegetable(quantity); } else if (foodType == "Fruit") { food = new Fruit(quantity); } else if (foodType == "Meat") { food = new Meat(quantity); } else if (foodType == "Seeds") { food = new Seeds(quantity); } Type animalT = animal.GetType(); if (animalT.Name == "Hen") { animal.Weight += 0.35 * quantity; animal.FoodEaten += quantity; } else if (animalT.Name == "Mouse") { if (foodType == "Fruit" || foodType == "Vegetable") { animal.Weight += 0.10 * quantity; animal.FoodEaten += quantity; } else { Console.WriteLine($"{animalT.Name} does not eat {foodType}!"); } } else if (animalT.Name == "Cat") { if (foodType == "Meat" || foodType == "Vegetable") { animal.Weight += 0.30 * quantity; animal.FoodEaten += quantity; } else { Console.WriteLine($"{animalT.Name} does not eat {foodType}!"); } } else if (animalT.Name == "Tiger" || animalT.Name == "Dog" || animalT.Name == "Owl") { if (foodType == "Meat") { if (animalT.Name == "Tiger") { animal.Weight += 1.00 * quantity; } else if (animalT.Name == "Dog") { animal.Weight += 0.40 * quantity; } else if (animalT.Name == "Owl") { animal.Weight += 0.25 * quantity; } animal.FoodEaten += quantity; } else { Console.WriteLine($"{animalT.Name} does not eat {foodType}!"); } } } foreach (var animal in animals) { Console.WriteLine(animal.ToString()); } }
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) { Animal dog = new Dog("Gosho", 45, "Bulgaria"); dog.ProduceSound(); }
static void Main(string[] args) { var animals = new List <Animal>(); while (true) { string[] animalInput = Console.ReadLine().Split(); if (animalInput[0] == "End") { break; } string[] foodInput = Console.ReadLine().Split(); Animal animal = null; Food food = null; string type = animalInput[0]; string name = animalInput[1]; double weight = double.Parse(animalInput[2]); switch (type) { case "Owl": animal = new Owl(name, weight, double.Parse(animalInput[3])); break; case "Hen": animal = new Hen(name, weight, double.Parse(animalInput[3])); break; case "Mouse": animal = new Mouse(name, weight, animalInput[3]); break; case "Dog": animal = new Dog(name, weight, animalInput[3]); break; case "Cat": animal = new Cat(name, weight, animalInput[3], animalInput[4]); break; case "Tiger": animal = new Tiger(name, weight, animalInput[3], animalInput[4]); break; default: break; } string foodType = foodInput[0]; int foodQuantity = int.Parse(foodInput[1]); switch (foodType) { case "Fruit": food = new Fruit(foodQuantity); break; case "Vegetable": food = new Vegetable(foodQuantity); break; case "Meat": food = new Meat(foodQuantity); break; case "Seeds": food = new Seeds(foodQuantity); break; default: break; } Console.WriteLine(animal.AskForFood()); animal.Feed(food); animals.Add(animal); } 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> listAnimals = new List <Animal>(); while (true) { string inputCommand = Console.ReadLine(); if (inputCommand == "End") { break; } else { string[] inputAnimalData = inputCommand .Split(new[] { ' ' }) .ToArray(); string animal = inputAnimalData[0]; string[] eatInformation = Console.ReadLine() .Split(new[] { ' ' }) .ToArray(); switch (animal) { case "Hen": Hen hen = new Hen(inputAnimalData[1], double.Parse(inputAnimalData[2]), int.Parse(inputAnimalData[3])); Console.WriteLine(hen.Speak()); Console.Write(hen.AddFood(eatInformation[0], int.Parse(eatInformation[1]))); listAnimals.Add(hen); //Console.WriteLine(hen); break; case "Owl": Owl owl = new Owl(inputAnimalData[1], double.Parse(inputAnimalData[2]), int.Parse(inputAnimalData[3])); Console.WriteLine(owl.Speak()); Console.Write(owl.AddFood(eatInformation[0], int.Parse(eatInformation[1]))); listAnimals.Add(owl); //Console.WriteLine(owl); break; case "Mouse": Mouse mouse = new Mouse(inputAnimalData[1], double.Parse(inputAnimalData[2]), inputAnimalData[3]); Console.WriteLine(mouse.Speak()); Console.Write(mouse.AddFood(eatInformation[0], int.Parse(eatInformation[1]))); listAnimals.Add(mouse); //Console.WriteLine(mouse); break; case "Dog": Dog dog = new Dog(inputAnimalData[1], double.Parse(inputAnimalData[2]), inputAnimalData[3]); Console.WriteLine(dog.Speak()); Console.Write(dog.AddFood(eatInformation[0], int.Parse(eatInformation[1]))); listAnimals.Add(dog); //Console.WriteLine(dog); break; case "Cat": Cat cat = new Cat(inputAnimalData[1], double.Parse(inputAnimalData[2]), inputAnimalData[3], inputAnimalData[4]); Console.WriteLine(cat.Speak()); Console.Write(cat.AddFood(eatInformation[0], int.Parse(eatInformation[1]))); //Console.WriteLine(cat); listAnimals.Add(cat); break; case "Tiger": Tiger tiger = new Tiger(inputAnimalData[1], double.Parse(inputAnimalData[2]), inputAnimalData[3], inputAnimalData[4]); Console.WriteLine(tiger.Speak()); Console.Write(tiger.AddFood(eatInformation[0], int.Parse(eatInformation[1]))); //Console.WriteLine(tiger); listAnimals.Add(tiger); break; default: break; } } } foreach (var currentAnimal in listAnimals) { Console.WriteLine(currentAnimal); } }
static void Main(string[] args) { List <Animal> animals = new List <Animal>(); string[] animalInput = Console.ReadLine().Split(); while (animalInput[0].ToLower() != "end") { string animalType = animalInput[0].ToLower(); string animalName = animalInput[1]; double animalWeight = double.Parse(animalInput[2]); Animal animal = null; if (animalType == "hen") { double wingSize = double.Parse(animalInput[3]); animal = new Hen(animalName, animalWeight, wingSize); } else if (animalType == "owl") { double wingSize = double.Parse(animalInput[3]); animal = new Owl(animalName, animalWeight, wingSize); } else if (animalType == "mouse") { string livingRegion = animalInput[3]; animal = new Mouse(animalName, animalWeight, livingRegion); } else if (animalType == "cat") { string livingRegion = animalInput[3]; string breed = animalInput[4]; animal = new Cat(animalName, animalWeight, livingRegion, breed); } else if (animalType == "dog") { string livingRegion = animalInput[3]; animal = new Dog(animalName, animalWeight, livingRegion); } else if (animalType == "tiger") { string livingRegion = animalInput[3]; string breed = animalInput[4]; animal = new Tiger(animalName, animalWeight, livingRegion, breed); } Console.WriteLine(animal.ProduceSound()); string[] foodInput = Console.ReadLine().Split(); string foodType = foodInput[0].ToLower(); int foodQuantity = int.Parse(foodInput[1]); Food food = null; if (foodType == "vegetable") { food = new Vegetable(foodQuantity); } else if (foodType == "fruit") { food = new Fruit(foodQuantity); } else if (foodType == "meat") { food = new Meat(foodQuantity); } else if (foodType == "seeds") { food = new Seeds(foodQuantity); } animals.Add(animal); try { animal.Eat(food); } catch (ArgumentException ae) { Console.WriteLine(ae.Message); } animalInput = Console.ReadLine().Split(); } foreach (var animal in animals) { Console.WriteLine(animal); } }
static void Main(string[] args) { List <Animal> animals = new List <Animal>(); string input = string.Empty; while ((input = Console.ReadLine()) != "End") { string[] animal = input.Split(' ', StringSplitOptions.RemoveEmptyEntries); string[] food = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); string foodType = food[0]; int foodQuantity = int.Parse(food[1]); if (animal[0] == "Cat" || animal[0] == "Tiger") { string type = animal[0]; string name = animal[1]; double weight = double.Parse(animal[2]); string livingRegion = animal[3]; string breed = animal[4]; if (type == "Cat") { Cat cat = new Cat(type, name, weight, livingRegion, breed); cat.FeedMe(); if (foodType == "Vegetable" || foodType == "Meat") { cat.Weight += foodQuantity * 0.30; cat.FoodEaten = foodQuantity; } else { Console.WriteLine($"{type} does not eat {foodType}!"); } animals.Add(cat); } else if (type == "Tiger") { Tiger tiger = new Tiger(type, name, weight, livingRegion, breed); tiger.FeedMe(); if (foodType == "Meat") { tiger.Weight += foodQuantity * 1.00; tiger.FoodEaten = foodQuantity; } else { Console.WriteLine($"{type} does not eat {foodType}!"); } animals.Add(tiger); } } else if (animal[0] == "Hen" || animal[0] == "Owl") { string type = animal[0]; string name = animal[1]; double weight = double.Parse(animal[2]); double wingSize = double.Parse(animal[3]); if (type == "Hen") { Hen hen = new Hen(type, name, weight, wingSize); hen.FeedMe(); hen.Weight += foodQuantity * 0.35; hen.FoodEaten = foodQuantity; animals.Add(hen); } else if (type == "Owl") { Owl owl = new Owl(type, name, weight, wingSize); owl.FeedMe(); if (foodType == "Meat") { owl.Weight += foodQuantity * 0.25; owl.FoodEaten = foodQuantity; } else { Console.WriteLine($"{type} does not eat {foodType}!"); } animals.Add(owl); } } else if (animal[0] == "Mouse" || animal[0] == "Dog") { string type = animal[0]; string name = animal[1]; double weight = double.Parse(animal[2]); string livingRegion = animal[3]; if (type == "Mouse") { Mouse mouse = new Mouse(type, name, weight, livingRegion); mouse.FeedMe(); if (foodType == "Vegetable" || foodType == "Fruit") { mouse.Weight += foodQuantity * 0.10; mouse.FoodEaten = foodQuantity; } else { Console.WriteLine($"{type} does not eat {foodType}!"); } animals.Add(mouse); } else if (type == "Dog") { Dog dog = new Dog(type, name, weight, livingRegion); dog.FeedMe(); if (foodType == "Meat") { dog.Weight += foodQuantity * 0.40; dog.FoodEaten = foodQuantity; } else { Console.WriteLine($"{type} does not eat {foodType}!"); } animals.Add(dog); } } } foreach (var animal in animals) { Console.WriteLine(animal.ToString()); } }