public static void Main(string[] args) { while (true) { string line = Console.ReadLine(); if (line == "Beast!") { break; } string[] animalInfo = Console.ReadLine().Split(); string name = animalInfo[0]; int age = int.Parse(animalInfo[1]); string gender = animalInfo[2]; if (string.IsNullOrEmpty(name) || age < 0 || string.IsNullOrEmpty(gender)) { Console.WriteLine("Invalid input!"); continue; } if (line == "Cat") { Cat cat = new Cat(name, age, gender); Console.WriteLine(cat); Console.WriteLine(cat.ProduceSound()); } else if (line == "Dog") { Dog dog = new Dog(name, age, gender); Console.WriteLine(dog); Console.WriteLine(dog.ProduceSound()); } else if (line == "Frog") { Frog frog = new Frog(name, age, gender); Console.WriteLine(frog); Console.WriteLine(frog.ProduceSound()); } else if (line == "Tomcat") { Tomcat tomcat = new Tomcat(name, age); Console.WriteLine(tomcat); Console.WriteLine(tomcat.ProduceSound()); } else if (line == "Kitten") { Kitten kitten = new Kitten(name, age); Console.WriteLine(kitten); Console.WriteLine(kitten.ProduceSound()); } } }
public static void Main(string[] args) { string type; while ((type = Console.ReadLine()) != "Beast!") { string comand = Console.ReadLine(); var comandArgs = comand.Split(); var name = comandArgs[0]; var age = int.Parse(comandArgs[1]); var gender = comandArgs[2]; if (name == "" || age < 0 || gender == "") { Console.WriteLine("Invalid input!"); } if (type == "Dog") { Dog dog = new Dog(name, age, gender); Console.WriteLine(dog.ToString()); dog.ProduceSound(); } else if (type == "Cat") { Cat cat = new Cat(name, age, gender); Console.WriteLine(cat.ToString()); cat.ProduceSound(); } else if (type == "Frog") { Frog frog = new Frog(name, age, gender); Console.WriteLine(frog.ToString()); frog.ProduceSound(); } else if (type == "Kittens") { Kitten kit = new Kitten(name, age); Console.WriteLine(kit.ToString()); kit.ProduceSound(); } else if (type == "Tomcat") { Tomcat tom = new Tomcat(name, age); Console.WriteLine(tom.ToString()); tom.ProduceSound(); } } }
static void Main(string[] args) { Animal Kalufka = new Kitten("Kalufka", 5); Kalufka.ProduceSound(); Animal Yanko = new Frog("Yanko", 10, "female"); Yanko.ProduceSound(); Animal Jonny = new Dog("Jonny", 2, "male"); Animal TommyTheCat = new Tomcat("Tommy The Cat", 5); TommyTheCat.ProduceSound(); Animal[] sofiiskiCentralen = { Kalufka, Yanko, Jonny, TommyTheCat }; double averageAge = sofiiskiCentralen.Average(x => x.Age); Console.WriteLine(averageAge); }
public static void Main(string[] args) { // input string input = string.Empty; while ((input = Console.ReadLine()) != "Beast!") { string[] data = Console.ReadLine().Split(); string name = data[0]; int age = int.Parse(data[1]); string gender = data[2]; if (input == "Cat") { var cat = new Cat(name, age, gender); Console.WriteLine(cat.ProduceSound()); } else if (input == "Dog") { var dog = new Dog(name, age, gender); Console.WriteLine(dog.ProduceSound()); } else if (input == "Frog") { var frog = new Frog(name, age, gender); Console.WriteLine(frog.ProduceSound()); } else if (input == "Kitten") { var kitten = new Kitten(name, age); Console.WriteLine(kitten.ProduceSound()); } else if (input == "Tomcat") { var tomcat = new Tomcat(name, age); Console.WriteLine(tomcat.ProduceSound()); } } }
public static void Main(string[] args) { string input = Console.ReadLine(); while (input != "Beast!") { if (input == "Cat") { string[] tokens = Console.ReadLine().Split(); Cat cat = new Cat(tokens[0], int.Parse(tokens[1]), tokens[2]); Console.WriteLine(cat); Console.WriteLine(cat.ProduceSound()); } else if (input == "Frog") { string[] tokens = Console.ReadLine().Split(); Frog frog = new Frog(tokens[0], int.Parse(tokens[1]), tokens[2]); Console.WriteLine(frog); Console.WriteLine(frog.ProduceSound()); } else if (input == "Dog") { string[] tokens = Console.ReadLine().Split(); Dog dog = new Dog(tokens[0], int.Parse(tokens[1]), tokens[2]); Console.WriteLine(dog); Console.WriteLine(dog.ProduceSound()); } else if (input == "Tomcat") { string[] tokens = Console.ReadLine().Split(); Tomcat tomcat = new Tomcat(tokens[0], int.Parse(tokens[1])); Console.WriteLine(tomcat); Console.WriteLine(tomcat.ProduceSound()); } else if (input == "Kitten") { string[] tokens = Console.ReadLine().Split(); Kitten kitten = new Kitten(tokens[0], int.Parse(tokens[1])); Console.WriteLine(kitten); Console.WriteLine(kitten.ProduceSound()); } input = Console.ReadLine(); } }
private static void CreateAnimal(string animalType) { string[] input = Console.ReadLine().Split(); string name = input[0]; int age = int.Parse(input[1]); string gender = input[2]; switch (animalType) { case "Dog": Dog dog = new Dog(name, age, gender); Console.WriteLine(dog); dog.ProduceSound(); break; case "Cat": Cat cat = new Cat(name, age, gender); Console.WriteLine(cat); cat.ProduceSound(); break; case "Frog": Frog frog = new Frog(name, age, gender); Console.WriteLine(frog); frog.ProduceSound(); break; case "Kitten": Kitten kitten = new Kitten(name, age, gender); Console.WriteLine(kitten); kitten.ProduceSound(); break; case "Tomcat": Tomcat tomcat = new Tomcat(name, age, gender); Console.WriteLine(tomcat); tomcat.ProduceSound(); break; default: throw new ArgumentException(Animal.ERROR_MESSAGE); } }
static void Main() { Animal jaba = new Frog("baba jaba", 1, Genders.Female); Animal kekerica = new Frog("kikirica", 3, Genders.Female); Animal sharo = new Dog("sharo", 3, Genders.Male); Animal sara = new Dog("sara", 2, Genders.Female); Animal oldy = new Dog("oldy", 12, Genders.Male); Animal puhi = new Kitten("puhi", 2); Animal tommy = new Tomcat("tommy", 4); Animal mouseKiller = new Cat("mousy", 5, Genders.Male); Animal[] animals = new Animal[] { jaba, kekerica, sharo, sara, puhi, tommy, mouseKiller, oldy }; var groupedAnimals = from animal in animals group animal by(animal is Cat) ? typeof(Cat) : animal.GetType() into animalLine select new { GroupName = animalLine.Key, AverageAge = animalLine.ToList().Average(an => an.Age) }; foreach (var animal in groupedAnimals) { Console.WriteLine("{0} - average age: {1:N2}", animal.GroupName.Name, animal.AverageAge); } puhi.ProduceSound(); oldy.ProduceSound(); jaba.ProduceSound(); }
public static void Main() { Animal jaba = new Frog("baba jaba", 1, Genders.Female); Animal kekerica = new Frog("kekerica", 3, Genders.Female); Animal sharo = new Dog("sharo", 3, Genders.Male); Animal sara = new Dog("sara", 2, Genders.Female); Animal oldy = new Dog("oldy", 12, Genders.Male); Animal puhi = new Kitten("puhi", 2); Animal tommy = new Tomcat("tommy", 4); Animal mouseKiller = new Cat("mousy", 5, Genders.Male); List<Animal> animals = new List<Animal>() { jaba, kekerica, sharo, sara, puhi, tommy, mouseKiller, oldy }; var groupedAnimals = from animal in animals group animal by (animal is Cat) ? typeof(Cat) : animal.GetType() into g select new { GroupName = g.Key, AverageAge = g.ToList().Average(an => an.Age) }; foreach (var animal in groupedAnimals) { Console.WriteLine("{0} - average age: {1:N2}", animal.GroupName.Name, animal.AverageAge); } puhi.ProduceSound(); oldy.ProduceSound(); jaba.ProduceSound(); }
static void Main() { Animals frog = new Frog("Ginka", 1, "Female"); Animals frog2 = new Frog("Gincho", 3, "Male"); Animals sharo = new Dogs("Sharo", 3, "Male"); Animals luki = new Dogs("Luki", 2, "Female"); Animals bu = new Dogs("Bu", 10, "Male"); Animals puhi = new Kitten("puhi", 2); Animals tom = new Tomcat("Tom", 4); Animals muke = new Cat("Muke", 5, "Male"); frog.ProduceSound(); Console.WriteLine(); List <Animals> animals = new List <Animals>() { frog, frog2, sharo, luki, puhi, tom, muke, bu }; var listedAnimals = from anim in animals group anim by anim.GetType() into groupAnim select new { GroupName = groupAnim.Key, AverageAge = groupAnim.ToList().Average(an => an.Age) }; foreach (var animal in listedAnimals) { Console.WriteLine("{0} - Average age: {1:N2}", animal.GroupName.Name, animal.AverageAge); } }
public void Run() { string animal = Console.ReadLine(); string[] properties = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries); while (true) { string name = properties[0]; int age = int.Parse(properties[1]); string gender = string.Empty; if (age <= 0) { Console.WriteLine("Invalid input!"); animal = Console.ReadLine(); if (animal == "Beast!") { break; } properties = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries); continue; } if (properties.Length > 2) { gender = properties[2]; } switch (animal) { case "Cat": { Console.WriteLine(animal); Cat cat = new Cat(name, age, gender); Console.WriteLine(cat); Console.WriteLine(cat.ProduceSound()); } break; case "Dog": { Dog dog = new Dog(name, age, gender); Console.WriteLine(animal); Console.WriteLine(dog); Console.WriteLine(dog.ProduceSound()); } break; case "Frog": { Console.WriteLine(animal); Frog frog = new Frog(name, age, gender); Console.WriteLine(frog); Console.WriteLine(frog.ProduceSound()); } break; case "Tomcat": { Console.WriteLine(animal); Tomcat tomcat = new Tomcat(name, age); Console.WriteLine(tomcat); Console.WriteLine(tomcat.ProduceSound()); } break; case "Kitten": { Console.WriteLine(animal); Kitten kitten = new Kitten(name, age); Console.WriteLine(kitten); Console.WriteLine(kitten.ProduceSound()); } break; } animal = Console.ReadLine(); if (animal == "Beast!") { break; } properties = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries); } }
static void Main() { var animal = Console.ReadLine(); while (!animal.Equals("Beast!")) { try { var animalInfo = Console.ReadLine() .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var animalName = animalInfo[0]; var animalAge = int.Parse(animalInfo[1]); var animalGender = animalInfo[2]; switch (animal) { case "Dog": var dog = new Dog(animalName, animalAge, animalGender); Console.Write(dog); dog.ProduceSound(); break; case "Frog": var frog = new Frog(animalName, animalAge, animalGender); Console.Write(frog); frog.ProduceSound(); break; case "Cat": var cat = new Cat(animalName, animalAge, animalGender); Console.Write(cat); cat.ProduceSound(); break; case "Kitten": var kitten = new Kitten(animalName, animalAge, "Female"); Console.Write(kitten); kitten.ProduceSound(); break; case "Tomcat": var tomcat = new Tomcat(animalName, animalAge, "Male"); Console.Write(tomcat); tomcat.ProduceSound(); break; case "Animal": Console.WriteLine("Not implemented!"); break; default: break; } } catch (Exception) { Console.WriteLine("Invalid input!"); } animal = Console.ReadLine(); } }
public static void Main(string[] args) { while (true) { try { var animal = Console.ReadLine(); if (animal == "Beast!") { break; } var animaInfo = Console.ReadLine().Split(" "); var name = animaInfo[0]; int age; if (!int.TryParse(animaInfo[1], out age)) { throw new ArgumentException("Invalid input!"); } var gender = animaInfo[2]; if (animal == "Cat") { Console.WriteLine(animal); Cat cat = new Cat(name, age, gender); Console.WriteLine(cat); cat.ProduceSound(); } else if (animal == "Dog") { Console.WriteLine(animal); Dog dog = new Dog(name, age, gender); Console.WriteLine($"{dog}"); dog.ProduceSound(); } else if (animal == "Frog") { Console.WriteLine(animal); Frog frog = new Frog(name, age, gender); Console.WriteLine(frog); frog.ProduceSound(); } else if (animal == "Kitten") { Console.WriteLine(animal); Kitten kitten = new Kitten(name, age); Console.WriteLine($"{kitten}"); kitten.ProduceSound(); } else if (animal == "Tomcat") { Console.WriteLine(animal); Tomcat tomcat = new Tomcat(name, age); Console.WriteLine($"{tomcat}"); tomcat.ProduceSound(); } } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } } }
static void Main(string[] args) { try { Frog goshko = new Frog("Goshko", 10, "male"); Frog peshko = new Frog("Peshko", 3, "male"); Frog mariika = new Frog("Mariika", 7, "female"); Dog ivancho = new Dog("Ivancho", 11, "male"); Dog sashko = new Dog("Sashko", 1, "male"); Dog magdalena = new Dog("Magdalena", 2, "female"); Kitten tania = new Kitten("Tania", 3); Kitten penka = new Kitten("Penka", 4); Kitten goshka = new Kitten("Goshka", 10); Tomcat kristian = new Tomcat("Kristian", 1); Tomcat pesho = new Tomcat("Pesho", 10); Tomcat stanislav = new Tomcat("Stanislav", 5); IList<Animal> animals = new List<Animal> { goshko, peshko, mariika, ivancho, sashko, magdalena, tania, penka, goshka, kristian, pesho, stanislav }; goshko.ProduceSound(); sashko.ProduceSound(); tania.ProduceSound(); kristian.ProduceSound(); double catsAverageAge = animals .Where(animal => animal is Cat) .Average(cat => cat.Age); double dogsAverageAge = animals .Where(animal => animal is Dog) .Average(dog => dog.Age); double frogsAverageAge = animals .Where(animal => animal is Frog) .Average(frog => frog.Age); Console.WriteLine("Frogs average age is: {0:F2}", frogsAverageAge); Console.WriteLine("Dogs average age is: {0:F2}", dogsAverageAge); Console.WriteLine("Cats average age is: {0:F2}", catsAverageAge); } catch (ArgumentOutOfRangeException ae) { Console.WriteLine(ae.Message); } catch (ArgumentNullException ae) { Console.WriteLine(ae.Message); } catch (ArgumentException ae) { Console.WriteLine(ae.Message); } }
static void Main(string[] args) { // Constructors test Kitten kity = new Kitten(10); Console.WriteLine("Kity's sex is : " + kity.Sex); Tomcat tommy = new Tomcat(10); Console.WriteLine("Tommy's sex is : " + tommy.Sex); Dog rex = new Dog(10,Sex.Male); Console.WriteLine("Rex's sex is : " + rex.Sex); Console.WriteLine("And Rex is {0} years old", rex.Age); Frog gogo = new Frog(100, Sex.Male); Console.WriteLine("Gogo the frog is {0} years old: ", gogo.Age); // Sounds test Console.WriteLine("__________________"); Console.WriteLine("Frogs sound: "); gogo.ProduceSound(); Console.WriteLine("__________________"); Console.WriteLine("Tomcats sound: "); tommy.ProduceSound(); Console.WriteLine("__________________"); Console.WriteLine("Kittens sound: "); kity.ProduceSound(); Console.WriteLine("__________________"); Console.WriteLine("Dogs sound: "); rex.ProduceSound(); Console.WriteLine(); Dog[] dogs = new Dog[4] { new Dog(10,Sex.Male), new Dog(12,Sex.Male), new Dog(14,Sex.Male), new Dog(8,Sex.Male), }; Frog[] frogs = new Frog[4] { new Frog(4,Sex.Male), new Frog(5,Sex.Male), new Frog(3,Sex.Male), new Frog(10,Sex.Male) }; Kitten[] kities = new Kitten[4] { new Kitten(10), new Kitten(11), new Kitten(15), new Kitten(18) }; Tomcat[] tomcats = new Tomcat[4] { new Tomcat(12), new Tomcat(15), new Tomcat(13), new Tomcat(40), }; // Avarage age Console.WriteLine(" The avarage age of Tomcat is " + AvarageAge(tomcats)); Console.WriteLine(" The avarage age of Kitten is " + AvarageAge(kities)); Console.WriteLine(" The avarage age of Dogs is " + AvarageAge(dogs)); Console.WriteLine(" The avarage age of Frogs is " + AvarageAge(frogs)); Console.WriteLine(); }