public static Animal CreateAnimal(string[] animalInputSplitted) { var animalType = animalInputSplitted[0]; var animalName = animalInputSplitted[1]; var animalWeight = double.Parse(animalInputSplitted[2]); var animalLivingRegion = animalInputSplitted[3]; if (animalType == "Cat") { var catBreed = animalInputSplitted[4]; Animal animal = new Cat(animalName, animalType, animalWeight, 0, animalLivingRegion, catBreed); return(animal); } else if (animalType == "Tiger") { Animal animal = new Tiger(animalName, animalType, animalWeight, 0, animalLivingRegion); return(animal); } else if (animalType == "Mouse") { Animal animal = new Mouse(animalName, animalType, animalWeight, 0, animalLivingRegion); return(animal); } else { Animal animal = new Zebra(animalName, animalType, animalWeight, 0, animalLivingRegion); return(animal); } }
private static Mammal TryAnimalCreate(string animalType, string animalName, double animalWeight, string animalLivingRegion, string catBreed) { Mammal result = null; switch (animalType) { case "Mouse": result = new Mouse(animalName, animalType, animalWeight, animalLivingRegion); break; case "Zebra": result = new Zebra(animalName, animalType, animalWeight, animalLivingRegion); break; case "Cat": result = new Cat(animalName, animalType, animalWeight, animalLivingRegion, catBreed); break; case "Tiger": result = new Tiger(animalName, animalType, animalWeight, animalLivingRegion); break; } return(result); }
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(); } }
private static List <Animal> InitAnimals(string command) { List <Animal> animals = new List <Animal>(); try { string[] animalData = command.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string animalType = animalData[0]; string animalName = animalData[1]; double.TryParse(animalData[2], out double animalWeight); string livingRegion = animalData[3]; switch (animalType) { case "Cat": string breed = animalData[4]; Animal cat = new Cat(animalType, animalName, animalWeight, livingRegion, breed); animals.Add(cat); break; case "Zebra": Animal zebra = new Zebra(animalType, animalName, animalWeight, livingRegion); animals.Add(zebra); break; case "Mouse": Animal mouse = new Mouse(animalType, animalName, animalWeight, livingRegion); animals.Add(mouse); break; case "Tiger": Animal tiger = new Tiger(animalType, animalName, animalWeight, livingRegion); animals.Add(tiger); break; } } catch (Exception e) { Console.WriteLine(e.Message); } return(animals); }
static void Main(string[] args) { Animal animal = null; string[] tokens = Console.ReadLine().Split(' '); string animalType = tokens[0]; string animalName = tokens[1]; double animalWeight = double.Parse(tokens[2]); string animalLivingRegion = tokens[3]; switch (animalType) { case "Cat": string catBreed = tokens[4]; animal = new Cat(animalType, animalName, animalWeight, animalLivingRegion, catBreed); break; case "Tiger": animal = new Tiger(animalType, animalName, animalWeight, animalLivingRegion); break; case "Zebra": animal = new Zebra(animalType, animalName, animalWeight, animalLivingRegion); break; case "Mouse": animal = new Mouse(animalType, animalName, animalWeight, animalLivingRegion); break; default: break; } string[] foodTokens = Console.ReadLine().Split(' '); string foodType = foodTokens[0]; int foodQuantity = int.Parse(foodTokens[1]); Food food = null; switch (foodType) { case "Vegetable": food = new Vegetable(foodQuantity); break; case "Meal": food = new Meat(foodQuantity); break; default: break; } try { animal.MakeSound(); animal.Eat(food); } catch (InvalidOperationException ex) { Console.WriteLine(ex.Message); } Console.WriteLine(animal); }
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); } }
static void Main(string[] args) { var input = string.Empty; while ((input = Console.ReadLine()) != "End") { var animalInformation = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var foodInformation = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); Animal currentAnimal = null; Food currentFood = null; var animalType = animalInformation[0]; var animalName = animalInformation[1]; var animalWeight = double.Parse(animalInformation[2]); var animalRegion = animalInformation[3]; var animalBreed = string.Empty; if (animalType == "Cat") { animalBreed = animalInformation[4]; currentAnimal = new Cat(animalName, animalType, animalWeight, animalRegion, animalBreed); } else { switch (animalType) { case "Tiger": currentAnimal = new Tiger(animalName, animalType, animalWeight, animalRegion); break; case "Zebra": currentAnimal = new Zebra(animalName, animalType, animalWeight, animalRegion); break; case "Mouse": currentAnimal = new Mouse(animalName, animalType, animalWeight, animalRegion); break; } } var foodType = foodInformation[0]; var foodAmount = int.Parse(foodInformation[1]); if (foodType == "Vegetable") { currentFood = new Vegetable(foodAmount); } else if (foodType == "Meat") { currentFood = new Meat(foodAmount); } Console.WriteLine(currentAnimal.MakeSound()); try { currentAnimal.Eat(currentFood); } catch (InvalidOperationException ioe) { Console.WriteLine(ioe.Message); } Console.WriteLine(currentAnimal); } }