public static Food CreateFood(string[] foodInfo) { Food food; switch (foodInfo[0]) { case "Fruit": food = new Fruit(int.Parse(foodInfo[1])); break; case "Vegetable": food = new Vegetable(int.Parse(foodInfo[1])); break; case "Meat": food = new Meat(int.Parse(foodInfo[1])); break; case "Seeds": food = new Seeds(int.Parse(foodInfo[1])); break; default: throw new ArgumentException("Invalid food type!"); } return(food); }
public static Food CreateFood(string[] foodArgs) { int quantity = int.Parse(foodArgs[1]); switch (foodArgs[0]) { case "Vegetable": Vegetable vegetable = new Vegetable(quantity); return(vegetable); case "Fruit": Fruit fruit = new Fruit(quantity); return(fruit); case "Meat": Meat meat = new Meat(quantity); return(meat); case "Seeds": Seeds seeds = new Seeds(quantity); return(seeds); default: return(null); } }
private static Food CreateFood(string[] parts, Food food) { string foodType = parts[0]; int quantity = int.Parse(parts[1]); 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); } return(food); }
public static Food CreateFood(string[] foodInfo) { Food food = null; string type = foodInfo[0]; int quantity = int.Parse(foodInfo[1]); if (type == "Fruit") { food = new Fruit(quantity); } else if (type == "Meat") { food = new Meat(quantity); } else if (type == "Seeds") { food = new Seeds(quantity); } else if (type == "Vegetable") { food = new Vegetable(quantity); } return(food); }
private static Food ReadFood(string str) { var data = str.Split(); string type = data[0]; int amount = int.Parse(data[1]); Food food = null; if (type == "Vegetable") { food = new Vegetable(amount); } else if (type == "Fruit") { food = new Fruit(amount); } else if (type == "Seeds") { food = new Seeds(amount); } else if (type == "Meat") { food = new Meat(amount); } return(food); }
private static Food CreateFood(string[] foodParts) { string type = foodParts[0]; int quantity = int.Parse(foodParts[1]); Food food = null; if (type == nameof(Meat)) { food = new Meat(quantity); } else if (type == nameof(Vegetable)) { food = new Vegetable(quantity); } else if (type == nameof(Fruit)) { food = new Fruit(quantity); } else if (type == nameof(Seeds)) { food = new Seeds(quantity); } return(food); }
private static Food CreateFood(string[] foodInput) { Food food = null; var type = foodInput[0]; var quantity = int.Parse(foodInput[1]); if (type == nameof(Fruit)) { food = new Fruit(quantity); } else if (type == nameof(Meat)) { food = new Meat(quantity); } else if (type == nameof(Seeds)) { food = new Seeds(quantity); } else if (type == nameof(Vegetable)) { food = new Vegetable(quantity); } return(food); }
public static Food CreateFood(string input) { string[] tokens = input.Split(" ", StringSplitOptions.RemoveEmptyEntries); switch (tokens[0]) { case "Vegetable": Food newVegetable = new Vegetable(int.Parse(tokens[1])); return(newVegetable); case "Fruit": Food newFruit = new Fruit(int.Parse(tokens[1])); return(newFruit); case "Meat": Food newMeat = new Meat(int.Parse(tokens[1])); return(newMeat); case "Seeds": Food newSeeds = new Seeds(int.Parse(tokens[1])); return(newSeeds); default: return(null); } }
static void Main() { var animalsFactory = new AnimalFactory(); string input; while ((input = Console.ReadLine()) != "End") { var tokens = input.Split(); Animal currentAnimal = null; currentAnimal = animalsFactory.CreateAnimal(tokens); if (currentAnimal != null) { list.Add(currentAnimal); Console.WriteLine(currentAnimal.MakeSound()); var foodTokens = Console.ReadLine().Split(); Food currentFood = null; switch (foodTokens[0]) { case "Vegetable": currentFood = new Vegetable(int.Parse(foodTokens[1])); break; case "Fruit": currentFood = new Fruit(int.Parse(foodTokens[1])); break; case "Meat": currentFood = new Meat(int.Parse(foodTokens[1])); break; case "Seeds": currentFood = new Seeds(int.Parse(foodTokens[1])); break; } try { currentAnimal.Feed(currentFood); } catch (Exception e) { Console.WriteLine(e.Message); } } } Console.WriteLine(string.Join(Environment.NewLine, list)); }
static void Main(string[] args) { List <Animal> animals = new List <Animal>(); while (true) { string input = Console.ReadLine(); if (input == "End") { break; } string[] dataAnimal = input.Split(); string[] dataFood = Console.ReadLine().Split(); string type = dataAnimal[0]; string name = dataAnimal[1]; double weight = double.Parse(dataAnimal[2]); Animal animal = null; if (type == nameof(Owl)) { animal = new Owl(name, weight, double.Parse(dataAnimal[3])); } else if (type == nameof(Hen)) { animal = new Hen(name, weight, double.Parse(dataAnimal[3])); } else if (type == nameof(Mouse)) { animal = new Mouse(name, weight, dataAnimal[3]); } else if (type == nameof(Dog)) { animal = new Dog(name, weight, dataAnimal[3]); } else if (type == nameof(Cat)) { animal = new Cat(name, weight, dataAnimal[3], dataAnimal[4]); } else if (type == nameof(Tiger)) { animal = new Tiger(name, weight, dataAnimal[3], dataAnimal[4]); } string foodType = dataFood[0]; int quantity = int.Parse(dataFood[1]); Food food = null; if (foodType == nameof(Meat)) { food = new Meat(quantity); } else if (foodType == nameof(Vegetable)) { food = new Vegetable(quantity); } else if (foodType == nameof(Fruit)) { food = new Fruit(quantity); } else if (foodType == nameof(Seeds)) { food = new Seeds(quantity); } Console.WriteLine(animal.ProduceSound()); animal.EatingFood(food); animals.Add(animal); } foreach (var animal in animals) { Console.WriteLine(animal); } }
static void Main(string[] args) { Queue <Animal> animals = new Queue <Animal>(); List <Animal> listAnimals = new List <Animal>(); var input = Console.ReadLine().Split(" ").ToArray(); while (input[0] != "End") { if (input.Length > 2) { switch (input[0]) { case "Hen": { Hen hen = new Hen(input[1], double.Parse(input[2]), double.Parse(input[3])); hen.WantFood(); animals.Enqueue(hen); break; } case "Owl": { Owl owl = new Owl(input[1], double.Parse(input[2]), double.Parse(input[3])); owl.WantFood(); animals.Enqueue(owl); break; } case "Mouse": { Mouse mouse = new Mouse(input[1], double.Parse(input[2]), input[3]); mouse.WantFood(); animals.Enqueue(mouse); break; } case "Cat": { Cat cat = new Cat(input[1], double.Parse(input[2]), input[3], input[4]); cat.WantFood(); animals.Enqueue(cat); break; } case "Dog": { Dog dog = new Dog(input[1], double.Parse(input[2]), input[3]); dog.WantFood(); animals.Enqueue(dog); break; } case "Tiger": { Tiger tiger = new Tiger(input[1], double.Parse(input[2]), input[3], input[4]); tiger.WantFood(); animals.Enqueue(tiger); break; } } } else { switch (input[0]) { case "Vegetable": { Vegetable vegetable = new Vegetable(int.Parse(input[1])); var animal = animals.Dequeue(); listAnimals.Add(animal); animal.Eat(vegetable); break; } case "Fruit": { Fruit fruit = new Fruit(int.Parse(input[1])); var animal = animals.Dequeue(); listAnimals.Add(animal); animal.Eat(fruit); break; } case "Meat": { Meat meat = new Meat(int.Parse(input[1])); var animal = animals.Dequeue(); listAnimals.Add(animal); animal.Eat(meat); break; } case "Seeds": { Seeds seeds = new Seeds(int.Parse(input[1])); var animal = animals.Dequeue(); listAnimals.Add(animal); animal.Eat(seeds); break; } } } input = Console.ReadLine().Split(" ").ToArray(); } foreach (var animal in listAnimals) { Console.WriteLine(animal.ToString()); } }
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); } }
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()); } }
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) { int counter = 0; string line = string.Empty; List <Animal> animals = new List <Animal>(); while ((line = Console.ReadLine()) != "End") { var information = line.Split(" "); counter++; if (counter % 2 != 0) { string type = information[0]; string name = information[1]; double weight = double.Parse(information[2]); Animal animal = null; switch (type) { case "Owl": double wingSize = double.Parse(information[3]); animal = new Owl(name, weight, wingSize); break; case "Hen": double wingSizes = double.Parse(information[3]); animal = new Hen(name, weight, wingSizes); break; case "Mouse": string livingRegion = information[3]; animal = new Mouse(name, weight, livingRegion); break; case "Dog": string livingRegiones = information[3]; animal = new Dog(name, weight, livingRegiones); break; case "Cat": string livingRegions = information[3]; string breed = information[4]; animal = new Cat(name, weight, livingRegions, breed); break; case "Tiger": string livingRegionse = information[3]; string breeds = information[4]; animal = new Tiger(name, weight, livingRegionse, breeds); break; } animals.Add(animal); } else { string foodName = information[0]; int quantity = int.Parse(information[1]); Food food = null; switch (foodName) { case "Vegetable": food = new Vegetable(quantity); break; case "Seeds": food = new Seeds(quantity); break; case "Meat": food = new Meat(quantity); break; case "Fruit": food = new Fruit(quantity); break; } Animal currentAnimal = animals[(counter / 2) - 1]; Console.WriteLine(currentAnimal.AskFood()); try { currentAnimal.Feed(food); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } foreach (var animal in animals) { Console.WriteLine(animal); } }
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()); } }
public static void Main() { List <Animal> animals = new List <Animal>(); List <Food> foods = new List <Food>(); int lineCounter = -1; while (true) { lineCounter++;//N.B. += 1 string cmd = Console.ReadLine(); if (cmd == "End") { break; } string[] cmdArgs = cmd.Split(' ', StringSplitOptions.RemoveEmptyEntries); switch (cmdArgs[0]) { case "Owl": var owl = new Owl(cmdArgs[1], double.Parse(cmdArgs[2]), double.Parse(cmdArgs[3])); animals.Add(owl); break; case "Hen": var hen = new Hen(cmdArgs[1], double.Parse(cmdArgs[2]), double.Parse(cmdArgs[3])); animals.Add(hen); break; case "Mouse": var mouse = new Mouse(cmdArgs[1], double.Parse(cmdArgs[2]), cmdArgs[3]); animals.Add(mouse); break; case "Dog": var dog = new Dog(cmdArgs[1], double.Parse(cmdArgs[2]), cmdArgs[3]); animals.Add(dog); break; case "Cat": var cat = new Cat(cmdArgs[1], double.Parse(cmdArgs[2]), cmdArgs[3], cmdArgs[4]); animals.Add(cat); break; case "Tiger": var tiger = new Tiger(cmdArgs[1], double.Parse(cmdArgs[2]), cmdArgs[3], cmdArgs[4]); animals.Add(tiger); break; case "Vegetable": var vegetable = new Vegetable(int.Parse(cmdArgs[1])); foods.Add(vegetable); break; case "Fruit": var fruit = new Fruit(int.Parse(cmdArgs[1])); foods.Add(fruit); break; case "Meat": var meat = new Meat(int.Parse(cmdArgs[1])); foods.Add(meat); break; case "Seeds": var seeds = new Seeds(int.Parse(cmdArgs[1])); foods.Add(seeds); break; } } for (int i = 0; i < animals.Count; i++) { try { Console.WriteLine(animals[i].AskForFood()); animals[i].Eat(foods[i]); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } } animals.ForEach(Console.WriteLine); }
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()); } }
static void Main(string[] args) { List <Animal> animals = new List <Animal>(); while (true) { string command = Console.ReadLine(); if (command.Equals("End")) { break; } string[] inputInfo = command.Split().ToArray(); string animalType = inputInfo[0]; string name = inputInfo[1]; double weight = double.Parse(inputInfo[2]); string[] food = Console.ReadLine().Split().ToArray(); string foodType = food[0]; int quantity = int.Parse(food[1]); IFood currentFood = null; Animal animal = null; if (foodType.Equals("Vegetable")) { currentFood = new Vegetable(quantity); } else if (foodType.Equals("Fruit")) { currentFood = new Fruit(quantity); } else if (foodType.Equals("Meat")) { currentFood = new Meat(quantity); } else if (foodType.Equals("Seeds")) { currentFood = new Seeds(quantity); } if (animalType.Equals("Hen")) { double wingSize = double.Parse(inputInfo[3]); animal = new Hen(name, weight, wingSize); } else if (animalType.Equals("Owl")) { double wingSize = double.Parse(inputInfo[3]); animal = new Owl(name, weight, wingSize); } else if (animalType.Equals("Mouse")) { string livingRegion = inputInfo[3]; animal = new Mouse(name, weight, livingRegion); } else if (animalType.Equals("Dog")) { string livingRegion = inputInfo[3]; animal = new Dog(name, weight, livingRegion); } else if (animalType.Equals("Cat")) { string livingRegion = inputInfo[3]; string breed = inputInfo[4]; animal = new Cat(name, weight, livingRegion, breed); } else if (animalType.Equals("Tiger")) { string livingRegion = inputInfo[3]; string breed = inputInfo[4]; animal = new Tiger(name, weight, livingRegion, breed); } animals.Add(animal); animal.ProduceSound(); try { animal.Eating(currentFood); } catch (Exception ex) { Console.WriteLine(ex.Message); } } foreach (var animal in animals) { Console.WriteLine(animal); } }
static void Main(string[] args) { var animalsCollect = new List <Animal>(); string input; while ((input = Console.ReadLine()) != "End") { string[] animal = input.Split(); string type = animal[0]; string name = animal[1]; double weight = double.Parse(animal[2]); string[] foods = Console.ReadLine().Split(); string typeFood = foods[0]; int quantity = int.Parse(foods[1]); Animal animals = null; switch (type) { case "Cat": string livingRegionCat = animal[3]; string breedCat = animal[4]; animals = new Cat(name, weight, livingRegionCat, breedCat); break; case "Tiger": string livingRegionTiger = animal[3]; string breedTiger = animal[4]; animals = new Tiger(name, weight, livingRegionTiger, breedTiger); break; case "Dog": string livingRegionDog = animal[3]; animals = new Dog(name, weight, livingRegionDog); break; case "Mouse": string livingRegionMouse = animal[3]; animals = new Mouse(name, weight, livingRegionMouse); break; case "Hen": double wingSizeHen = double.Parse(animal[3]); animals = new Hen(name, weight, wingSizeHen); break; case "Owl": double wingSizeOwl = double.Parse(animal[3]); animals = new Owl(name, weight, wingSizeOwl); break; default: break; } Console.WriteLine(animals.Sound()); animalsCollect.Add(animals); Food food = null; try { switch (typeFood) { case "Vegetable": food = new Vegetable(quantity); break; case "Fruit": food = new Fruit(quantity); break; case "Meat": food = new Meat(quantity); break; case "Seeds": food = new Seeds(quantity); break; default: break; } animals.EatFood(food); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } } foreach (var animal in animalsCollect) { Console.WriteLine(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() { List <Animal> animals = new List <Animal>(); List <Food> foods = new List <Food>(); int lineNum = 0; while (true) { string line = Console.ReadLine(); if (line == "End") { break; } Animal animal = null; Food food = null; if (lineNum % 2 == 0) { string[] animalArgs = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); lineNum++; string type = animalArgs[0]; string name = animalArgs[1]; double weight = double.Parse(animalArgs[2]); switch (type) { // {LivingRegion} {Breed} case "Cat": string livingRegion = animalArgs[3]; string breed = animalArgs[4]; animal = new Cat(name, weight, livingRegion, breed); break; case "Tiger": livingRegion = animalArgs[3]; breed = animalArgs[4]; animal = new Tiger(name, weight, livingRegion, breed); break; case "Owl": double wingSize = double.Parse(animalArgs[3]); animal = new Owl(name, weight, wingSize); break; case "Hen": wingSize = double.Parse(animalArgs[3]); animal = new Hen(name, weight, wingSize); break; case "Mouse": livingRegion = animalArgs[3]; animal = new Mouse(name, weight, livingRegion); break; case "Dog": livingRegion = animalArgs[3]; animal = new Dog(name, weight, livingRegion); break; } animals.Add(animal); } else { string[] foodArgs = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); lineNum++; string foodType = foodArgs[0]; int quantity = int.Parse(foodArgs[1]); switch (foodType) { case "Vegetable": food = new Vegetable(quantity); break; case "Fruit": food = new Fruit(quantity); break; case "Meat": food = new Meat(quantity); break; case "Seeds": food = new Seeds(quantity); break; } foods.Add(food); } } for (int i = 0; i < animals.Count; i++) { try { Console.WriteLine(animals[i].AskForFood()); animals[i].Eat(foods[i]); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } } animals.ForEach(Console.WriteLine); }
public static void Main() { string command = string.Empty; List <Animal> animals = new List <Animal>(); while ((command = Console.ReadLine()) != "End") { //TODO: try catch exeptions string[] args = command.Split(' ', StringSplitOptions.RemoveEmptyEntries); string type = args[0]; string name = args[1]; double weight = double.Parse(args[2]); string[] foodInput = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); string foodType = foodInput[0]; int foodQuanity = int.Parse(foodInput[1]); //TODO: if food is null Food food = null; switch (foodType) { case "Vegetable": food = new Vegetable(foodQuanity); break; case "Fruit": food = new Fruit(foodQuanity); break; case "Meat": food = new Meat(foodQuanity); break; case "Seeds": food = new Seeds(foodQuanity); break; default: break; } Animal animal = null; switch (type) { case "Owl": double wingsSize = double.Parse(args[3]); animal = new Owl(name, weight, wingsSize); break; case "Hen": wingsSize = double.Parse(args[3]); animal = new Hen(name, weight, wingsSize); break; case "Mouse": string livingRegion = args[3]; animal = new Mouse(name, weight, livingRegion); break; case "Dog": livingRegion = args[3]; animal = new Dog(name, weight, livingRegion); break; case "Cat": livingRegion = args[3]; string breed = args[4]; animal = new Cat(name, weight, livingRegion, breed); break; case "Tiger": livingRegion = args[3]; breed = args[4]; animal = new Tiger(name, weight, livingRegion, breed); break; } animal.Eat(food); Console.WriteLine(); animals.Add(animal); } }
static void Main(string[] args) { string input = string.Empty; int counter = 0; var listAnimals = new Stack <Animal>(); var listFood = new Stack <Food>(); while ((input = Console.ReadLine()) != "End") { if (counter % 2 == 0) { var tokens = input .Split() .ToArray(); string animalType = tokens[0]; string animalName = tokens[1]; double animalWeight = double.Parse(tokens[2]); if (animalType == "Owl") { double wingSize = double.Parse(tokens[3]); Owl owl = new Owl(animalName, animalWeight, wingSize); owl.Sound(); listAnimals.Push(owl); } else if (animalType == "Hen") { double wingSize = double.Parse(tokens[3]); Hen hen = new Hen(animalName, animalWeight, wingSize); hen.Sound(); listAnimals.Push(hen); } else if (animalType == "Mouse") { string livingRegion = tokens[3]; Mouse mouse = new Mouse(animalName, animalWeight, livingRegion); mouse.Sound(); listAnimals.Push(mouse); } else if (animalType == "Dog") { string livingRegion = tokens[3]; Dog dog = new Dog(animalName, animalWeight, livingRegion); dog.Sound(); listAnimals.Push(dog); } else if (animalType == "Cat") { string livingRegion = tokens[3]; string breed = tokens[4]; Cat cat = new Cat(animalName, animalWeight, livingRegion, breed); cat.Sound(); listAnimals.Push(cat); } else if (animalType == "Tiger") { string livingRegion = tokens[3]; string breed = tokens[4]; Tiger tiger = new Tiger(animalName, animalWeight, livingRegion, breed); tiger.Sound(); listAnimals.Push(tiger); } } else if (counter % 2 != 0) { var token = input .Split() .ToArray(); string foodType = token[0]; int quantity = int.Parse(token[1]); if (foodType == "Vegetable") { Vegetable vegetable = new Vegetable(quantity); listFood.Push(vegetable); } else if (foodType == "Fruit") { Fruit fruit = new Fruit(quantity); listFood.Push(fruit); } else if (foodType == "Meat") { Meat meat = new Meat(quantity); listFood.Push(meat); } else if (foodType == "Seeds") { Seeds seeds = new Seeds(quantity); listFood.Push(seeds); } } if (counter % 2 != 0) { listAnimals.Peek().Eat(listFood.Peek()); } counter++; } while (listAnimals.Count > 0) { Console.WriteLine(listAnimals.Pop().ToString()); } }