static void Main(string[] args) { string animalType = Console.ReadLine(); while (animalType != "Beast!") { string[] animalInfo = Console.ReadLine().Split(); string name = animalInfo[0]; int age = int.Parse(animalInfo[1]); string gender = animalInfo[2]; try { switch (animalType) { case "Cat": Cat cat = new Cat(name, age, gender); Console.WriteLine(cat); cat.MakeSound(); break; case "Dog": Dog dog = new Dog(name, age, gender); Console.WriteLine(dog); dog.MakeSound(); break; case "Frog": Frog frog = new Frog(name, age, gender); Console.WriteLine(frog); frog.MakeSound(); break; case "Kittens": Kittens kittens = new Kittens(name, age, gender); Console.WriteLine(kittens); kittens.MakeSound(); break; case "Tomcat": Tomcat tomcat = new Tomcat(name, age, gender); Console.WriteLine(tomcat); tomcat.MakeSound(); break; default: break; } } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } animalType = Console.ReadLine(); } }
static void Main(string[] args) { var frog1 = new Frog(25, "jaba1", true); var frog2 = new Frog(20, "jaba2", false); var frog3 = new Frog(25, "jaba3", true); var frog4 = new Frog(20, "jaba4", false); var allFrogs=new Frog[]{frog1,frog2,frog3,frog4}; var dog1 = new Dog(30, "kuche1", true); var dog2 = new Dog(30, "kuche2", true); var dog3 = new Dog(30, "kuche3", true); var allDogs=new Dog[]{dog1,dog2,dog3}; var randomCat1 = new Cat(5, "random kotka1", false); var randomCat2 = new Cat(13, "random kotka2", false); var randomCat3 = new Cat(5, "random kotka3",true); var allCats=new Cat[]{randomCat1,randomCat2,randomCat3}; var femaleCat1 = new Kitten(5, "jenska kotka1"); var femaleCat2 = new Kitten(43, "jenska kotka2"); var femaleCat3 = new Kitten(56, "jenska kotka3"); var allFemaleCats=new Kitten[]{femaleCat1,femaleCat2,femaleCat3}; var maleCat1 = new TomCat(5, "mujka kotka1"); var maleCat2 = new TomCat(6, "mujka kotka2"); var maleCat3 = new TomCat(200, "mujka kotka3"); var allMaleCats=new TomCat[]{maleCat1,maleCat2,maleCat3}; var frogs = allFrogs.Average(x => x.Age); Console.WriteLine("The avergae age of frogs is: {0}",frogs); var dogs = allDogs.Average(x => x.Age); Console.WriteLine("The avergae age of dogs is: {0}", dogs); var cats = allCats.Average(x => x.Age); Console.WriteLine("The avergae age of random cats is: {0}", cats); var femaleCats = allFemaleCats.Average(x => x.Age); Console.WriteLine("The avergae age of female cats(Kittens) is: {0}", femaleCats); var maleCats = allMaleCats.Average(x => x.Age); Console.WriteLine("The avergae age of male cats(Tomcats) is: {0}", maleCats); Console.WriteLine(randomCat1.MakeSound()); }
static void Main(string[] args) { //test Cat cat = new Cat(3, "Catty1", Sex.female); Console.WriteLine(cat.Name + " : " + cat.Age + " : " + cat.Sex); cat.MakeSound(); Console.WriteLine(); //make array of different kinds of animals Animal[] animals = { new Kitten(2,"Kitty1"), new Kitten(3,"Kitty2"), new Tomcat(5,"Tommy1"), new Tomcat(4,"Tommy2"), new Frog(1,"Frog1",Sex.female), new Frog(2,"Frog2",Sex.male), new Frog(1,"Frog3",Sex.female), new Cat(2,"",Sex.male), cat, new Cat(2,"Catty2",Sex.male), new Dog(4,"Doggy1",Sex.male), new Dog(6,"Doggy2",Sex.male)}; // using LINQ to calculate the average age of each // kind of animal in the array var averageAge = animals.GroupBy(x => x.GetType().Name) .Select(x => new { Name = x.Key, Average = x.Average(a => a.Age) }) .ToList(); foreach (var item in averageAge) { Console.WriteLine(String.Format("{0,-10}{1,2:0.0}",item.Name + " : ", item.Average)); } }
public void Start() { /*Console.WriteLine(""); * Console.WriteLine("Hello and welcome to our magnificent menagerie!"); * Console.WriteLine(""); * Console.WriteLine("May we introduce a fine bunch of animals for you?"); * Console.WriteLine("y = Yes, please! n = Not right now, thank you!"); * string input = Console.ReadLine(); * while (!(input == "y") && !(input == "n")) * { * Console.WriteLine("Please choose yes (y) or no (n)"); * input = Console.ReadLine(); * } * if (input == "n") * { * Console.WriteLine(""); * Console.WriteLine("You're welcome to visit us at a more convenient time!"); * System.Environment.Exit(0); * } * else if (input == "y") * { * Console.WriteLine(""); * Console.WriteLine("Splendid! We have for you some tame animals (bird, dog, cat, pig and rat)"); * Console.WriteLine("... as well as some wild ones (bear, tiger and wolf)."); * Console.WriteLine(""); * } * * Console.WriteLine("Which animal would you like to meet?"); * Console.WriteLine("1 = bird 2 = dog 3 = cat 4 = pig 5 = rat 6 = bear 7 = tiger 8 = wolf"); * string input3 = Console.ReadLine(); * Regex regex = new Regex("^(1|2|3|4|5|6|7|8)$"); * * while (true) * { * if (regex.IsMatch(input3)) * { * Console.WriteLine(""); * Console.WriteLine("An excellent choice!"); * Console.WriteLine(""); * break; * } * else * { * Console.WriteLine("Unfortunately that option isn't available. Please choose one of the following: 1 = bird 2 = dog 3 = cat 4 = pig 5 = rat 6 = bear 7 = tiger 8 = wolf"); * input3 = Console.ReadLine(); * } * } * * if (input3 == "1") * { * ITame bird = new Bird(); * } * * while (true) * { * Console.WriteLine("What would you like to do?"); * Regex regex2 = new Regex("^(0|1|2|3|4)$"); // if user chooses a tame animal * if (regex2.IsMatch(input3)) * { * Console.WriteLine("0 = Quit"); * Console.WriteLine("1 = Give the animal some food."); * Console.WriteLine("2 = Give a name to the animal."); * Console.WriteLine("3 = Call the animal to you."); * Console.WriteLine("4 = Talk to the animal."); * * } * Regex regex3 = new Regex("^(5|6|7)$"); // if user chooses a wild animal * if (regex3.IsMatch(input3)) * { * Console.WriteLine("0 = Quit"); * Console.WriteLine("1 = See the animal hunt."); * Console.WriteLine("2 = Hear what the animal sounds like."); * * } * * string input4 = Console.ReadLine(); * * if (input4 == "0") * { * Console.WriteLine("We're sorry to see you go! Please do stop by another time!"); * break; * } * * Regex regex4 = new Regex("^(1|2|3|4|)$"); * * if (!regex4.IsMatch(input4)) * { * Console.WriteLine("Please choose one of the options above!"); * } * * else if (regex4.IsMatch(input4)) * { * if (Convert.ToInt32(input4) == 1) * { * * } * if (Convert.ToInt32(input4) == 2) * { * * } * if (Convert.ToInt32(input4) == 3) * { * * } * if (Convert.ToInt32(input4) == 4) * { * * } * if (Convert.ToInt32(input4) == 5) * { * * } * }*/ Console.WriteLine(""); Console.WriteLine("Hello and welcome to our magnificent menagerie!"); Console.WriteLine(""); Console.WriteLine("May we introduce a fine bunch of animals for you?"); Console.WriteLine("y = Yes, please! n = Not right now, thank you!"); string input = Console.ReadLine(); while (!(input == "y") && !(input == "n")) { Console.WriteLine("Please choose yes (y) or no (n)"); input = Console.ReadLine(); } if (input == "n") { Console.WriteLine(""); Console.WriteLine("You're welcome to visit us at a more convenient time!"); System.Environment.Exit(0); } if (input == "y") { Console.WriteLine(""); Console.WriteLine("Splendid! We have for you some tame animals (bird, dog, cat, pig and rat)"); Console.WriteLine("... as well as some wild ones (bear, tiger and wolf)."); Console.WriteLine(""); } while (true) { Console.WriteLine("Which animal would you like to meet?"); Console.WriteLine(); // added an empty line Console.WriteLine("0 = quit\n1 = Bird\n2 = Cat\n3 = Dog\n4 = Pig\n5 = Rat\n6 = Bear\n7 = Tiger\n8 = Wolf"); // changed Quit to quit string whichAnimal = Console.ReadLine(); Regex regexAnimal = new Regex("^(0|1|2|3|4|5|6|7|8|)$"); if (!regexAnimal.IsMatch(whichAnimal)) { Console.WriteLine(); Console.WriteLine("Please choose one of the options above!"); Console.WriteLine(); System.Threading.Thread.Sleep(3000); continue; } if (whichAnimal == "0") { Console.WriteLine(); Console.WriteLine("We're sorry to see you go! Please do stop by another time!"); System.Threading.Thread.Sleep(3000); break; } if (whichAnimal == "1") { Bird bird = new Bird(); while (true) { Console.WriteLine("What would you like to do?"); Console.WriteLine(); // added an empty line to all Console.WriteLine("1 = Go back."); Console.WriteLine("2 = Give the animal some food."); Console.WriteLine("3 = Give a name to the animal."); Console.WriteLine("4 = Call the animal to you."); Console.WriteLine("5 = Talk to the animal."); string toDo = Console.ReadLine(); if (toDo != "1" && toDo != "2" && toDo != "3" && toDo != "4" && toDo != "5") { Console.WriteLine("Please choose one of the options above!"); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "1") { break; } if (toDo == "2") { Console.WriteLine(); bird.Eat(); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "3") { Console.WriteLine(); Console.WriteLine("What would you like to call the animal?"); string name = Console.ReadLine(); bird.GiveName(name); Console.WriteLine(); } if (toDo == "4") { Console.WriteLine(); Console.WriteLine(bird.ComeHere()); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "5") { Console.WriteLine(); Console.WriteLine("What do you want to say to the animal?"); string something = Console.ReadLine(); Console.WriteLine(); Console.WriteLine(bird.MakeSound()); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } } } if (whichAnimal == "2") { Cat cat = new Cat(); while (true) { Console.WriteLine("What would you like to do?"); Console.WriteLine(); Console.WriteLine("1 = Go back."); Console.WriteLine("2 = Give the animal some food."); Console.WriteLine("3 = Give a name to the animal."); Console.WriteLine("4 = Call the animal to you."); Console.WriteLine("5 = Talk to the animal."); string toDo = Console.ReadLine(); if (toDo != "1" && toDo != "2" && toDo != "3" && toDo != "4" && toDo != "5") { Console.WriteLine("Please choose one of the options above!"); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "1") { break; } if (toDo == "2") { Console.WriteLine(); cat.Eat(); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "3") { Console.WriteLine(); Console.WriteLine("What would you like to call the animal?"); string name = Console.ReadLine(); cat.GiveName(name); Console.WriteLine(); } if (toDo == "4") { Console.WriteLine(); Console.WriteLine(cat.ComeHere()); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "5") { Console.WriteLine(); Console.WriteLine("What do you want to say to the animal?"); string something = Console.ReadLine(); Console.WriteLine(); Console.WriteLine(cat.MakeSound()); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } } } if (whichAnimal == "3") { Dog dog = new Dog(); while (true) { Console.WriteLine("What would you like to do?"); Console.WriteLine(); Console.WriteLine("1 = Go back."); Console.WriteLine("2 = Give the animal some food."); Console.WriteLine("3 = Give a name to the animal."); Console.WriteLine("4 = Call the animal to you."); Console.WriteLine("5 = Talk to the animal."); string toDo = Console.ReadLine(); if (toDo != "1" && toDo != "2" && toDo != "3" && toDo != "4" && toDo != "5") { Console.WriteLine("Please choose one of the options above!"); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "1") { break; } if (toDo == "2") { Console.WriteLine(); dog.Eat(); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "3") { Console.WriteLine(); Console.WriteLine("What would you like to call the animal?"); string name = Console.ReadLine(); dog.GiveName(name); Console.WriteLine(); } if (toDo == "4") { Console.WriteLine(); Console.WriteLine(dog.ComeHere()); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "5") { Console.WriteLine(); Console.WriteLine("What do you want to say to the animal?"); string something = Console.ReadLine(); Console.WriteLine(); Console.WriteLine(dog.MakeSound()); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } } } if (whichAnimal == "4") { Pig pig = new Pig(); //CHANGE EVERYTHING FROM DOG TO PIG HERE (NOT CHANGE ALL OCCURRENCES) while (true) { Console.WriteLine("What would you like to do?"); Console.WriteLine(); Console.WriteLine("1 = Go back."); Console.WriteLine("2 = Give the animal some food."); Console.WriteLine("3 = Give a name to the animal."); Console.WriteLine("4 = Call the animal to you."); Console.WriteLine("5 = Talk to the animal."); string toDo = Console.ReadLine(); if (toDo != "1" && toDo != "2" && toDo != "3" && toDo != "4" && toDo != "5") { Console.WriteLine("Please choose one of the options above!"); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "1") { break; } if (toDo == "2") { Console.WriteLine(); pig.Eat(); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "3") { Console.WriteLine(); Console.WriteLine("What would you like to call the animal?"); string name = Console.ReadLine(); pig.GiveName(name); Console.WriteLine(); } if (toDo == "4") { Console.WriteLine(); Console.WriteLine(pig.ComeHere()); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "5") { Console.WriteLine(); Console.WriteLine("What do you want to say to the animal?"); string something = Console.ReadLine(); Console.WriteLine(); Console.WriteLine(pig.MakeSound()); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } } } if (whichAnimal == "5") { Rat rat = new Rat(); //CHANGE EVERYTHING FROM DOG TO RAT HERE (NOT CHANGE ALL OCCURRENCES) while (true) { Console.WriteLine("What would you like to do?"); Console.WriteLine(); Console.WriteLine("1 = Go back."); Console.WriteLine("2 = Give the animal some food."); Console.WriteLine("3 = Give a name to the animal."); Console.WriteLine("4 = Call the animal to you."); Console.WriteLine("5 = Talk to the animal."); string toDo = Console.ReadLine(); if (toDo != "1" && toDo != "2" && toDo != "3" && toDo != "4" && toDo != "5") { Console.WriteLine("Please choose one of the options above!"); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "1") { break; } if (toDo == "2") { Console.WriteLine(); rat.Eat(); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "3") { Console.WriteLine(); Console.WriteLine("What would you like to call the animal?"); string name = Console.ReadLine(); rat.GiveName(name); Console.WriteLine(); } if (toDo == "4") { Console.WriteLine(); Console.WriteLine(rat.ComeHere()); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "5") { Console.WriteLine(); Console.WriteLine("What do you want to say to the animal?"); string something = Console.ReadLine(); Console.WriteLine(); Console.WriteLine(rat.MakeSound()); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } } } if (whichAnimal == "6") { Bear bear = new Bear(); while (true) { Console.WriteLine("What would you like to do?"); Console.WriteLine(); Console.WriteLine("1 = Go back."); Console.WriteLine("2 = Watch the animal hunt"); Console.WriteLine("3 = Hear the sound of the animal"); string toDo = Console.ReadLine(); if (toDo != "1" && toDo != "2" && toDo != "3") { Console.WriteLine("Please choose one of the options above!"); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "1") { break; } if (toDo == "2") { Console.WriteLine(); Console.WriteLine(bear.Hunt()); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "3") { Console.WriteLine(); Console.WriteLine(bear.MakeSound()); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } } } if (whichAnimal == "7") { Tiger tiger = new Tiger(); //CHANGE EVERYTHING FROM BEAR TO TIGER HERE (NOT CHANGE ALL OCCURRENCES) while (true) { Console.WriteLine("What would you like to do?"); Console.WriteLine(); Console.WriteLine("1 = Go back."); Console.WriteLine("2 = Watch the animal hunt"); Console.WriteLine("3 = Hear the sound of the animal"); string toDo = Console.ReadLine(); if (toDo != "1" && toDo != "2" && toDo != "3") { Console.WriteLine("Please choose one of the options above!"); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "1") { break; } if (toDo == "2") { Console.WriteLine(); Console.WriteLine(tiger.Hunt()); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "3") { Console.WriteLine(); Console.WriteLine(tiger.MakeSound()); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } } } if (whichAnimal == "8") { Wolf wolf = new Wolf(); //CHANGE EVERYTHING FROM BEAR TO WOLF HERE (NOT CHANGE ALL OCCURRENCES) while (true) { Console.WriteLine("What would you like to do?"); Console.WriteLine(); Console.WriteLine("1 = Go back."); Console.WriteLine("2 = Watch the animal hunt."); Console.WriteLine("3 = Hear the sound of the animal."); string toDo = Console.ReadLine(); if (toDo != "1" && toDo != "2" && toDo != "3") { Console.WriteLine("Please choose one of the options above!"); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "1") { break; } if (toDo == "2") { Console.WriteLine(); Console.WriteLine(wolf.Hunt()); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } if (toDo == "3") { Console.WriteLine(); Console.WriteLine(wolf.MakeSound()); Console.WriteLine(); System.Threading.Thread.Sleep(3000); } } } } }