예제 #1
0
        static void Main()
        {
            string line;

            while ((line = Console.ReadLine()) != "Beast!")
            {
                try
                {
                    if (line == "Dog")
                    {
                        var info = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
                        var dog  = new Dog(info[0], int.Parse(info[1]), info[2]);

                        Console.WriteLine(dog);
                        dog.ProduceSound();
                    }
                    else if (line == "Cat")
                    {
                        var info = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
                        var cat  = new Cat(info[0], int.Parse(info[1]), info[2]);

                        Console.WriteLine(cat);
                        cat.ProduceSound();
                    }
                    else if (line == "Frog")
                    {
                        var info = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
                        var frog = new Frog(info[0], int.Parse(info[1]), info[2]);

                        Console.WriteLine(frog);
                        frog.ProduceSound();
                    }
                    else if (line == "Kitten")
                    {
                        var info   = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
                        var kitten = new Kitten(info[0], int.Parse(info[1]));

                        Console.WriteLine(kitten);
                        kitten.ProduceSound();
                    }
                    else if (line == "Tomcat")
                    {
                        var info   = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries);
                        var tomcat = new Tomcat(info[0], int.Parse(info[1]));

                        Console.WriteLine(tomcat);
                        tomcat.ProduceSound();
                    }
                    else
                    {
                        throw new ArgumentException("Invalid input!");
                    }
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
예제 #2
0
        public static void Main()
        {
            var animals = new List <Animal>();

            string typeOfAnimal = Console.ReadLine();

            while (typeOfAnimal != "Beast!")
            {
                var animalTokens = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                try
                {
                    switch (typeOfAnimal.ToLower())
                    {
                    case "cat":
                        Animal cat = new Cat(animalTokens[0], int.Parse(animalTokens[1]), animalTokens[2]);
                        animals.Add(cat);
                        break;

                    case "dog":
                        Animal dog = new Dog(animalTokens[0], int.Parse(animalTokens[1]), animalTokens[2]);
                        animals.Add(dog);
                        break;

                    case "frog":
                        Animal frog = new Frog(animalTokens[0], int.Parse(animalTokens[1]), animalTokens[2]);
                        animals.Add(frog);
                        break;

                    case "kitten":
                        Animal kitten = new Kitten(animalTokens[0], int.Parse(animalTokens[1]), animalTokens[2]);
                        animals.Add(kitten);
                        break;

                    case "tomcat":
                        Animal tomcat = new Tomcat(animalTokens[0], int.Parse(animalTokens[1]), animalTokens[2]);
                        animals.Add(tomcat);
                        break;

                    default:
                        Console.WriteLine("Invalid input!");
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);;
                }

                typeOfAnimal = Console.ReadLine();
            }

            foreach (var animal in animals)
            {
                Console.WriteLine(animal.IntroduceAnimal());
                Console.WriteLine(animal.ProduceSound());
            }
        }
예제 #3
0
        public static void Main(string[] args)
        {
            string         animalType = Console.ReadLine();
            List <Animals> animals    = new List <Animals>();

            try
            {
                while (animalType != "Beast!")
                {
                    string[] animalData = Console.ReadLine().Split(new[] { ' ', '\n', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    string   name       = animalData[0];
                    int      age        = int.Parse(animalData[1]);

                    switch (animalType)
                    {
                    case "Dog":
                        var dog = new Dog(name, age, animalData[2]);
                        animals.Add(dog);
                        break;

                    case "Cat":
                        var cat = new Cat(name, age, animalData[2]);
                        animals.Add(cat);
                        break;

                    case "Frog":
                        var frog = new Frog(name, age, animalData[2]);
                        animals.Add(frog);
                        break;

                    case "Kitten":
                        var kitten = new Kitten(name, age);
                        animals.Add(kitten);
                        break;

                    case "Tomcat":
                        var tomcat = new Tomcat(name, age);
                        animals.Add(tomcat);
                        break;

                    default: throw new ArgumentException("Invalid input!");
                    }

                    animalType = Console.ReadLine();
                }

                foreach (var animal in animals)
                {
                    Console.WriteLine(animal);
                }
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
        }
예제 #4
0
        public static void Main()
        {
            var type = Console.ReadLine();

            while (type != "Beast!")
            {
                var tokens = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var name   = tokens[0];
                int age    = int.Parse(tokens[1]);
                //int age;
                //if (!int.TryParse(tokens[1], out age))
                //{
                //    throw new ArgumentException("Invalid input!");
                //}

                var gender = tokens[2];

                try
                {
                    Animal animal;
                    switch (type)
                    {
                    case "Cat":
                        animal = new Cat(name, age, gender);
                        break;

                    case "Dog":
                        animal = new Dog(name, age, gender);
                        break;

                    case "Frog":
                        animal = new Frog(name, age, gender);
                        break;

                    case "Kitten":
                        animal = new Kitten(name, age);
                        break;

                    case "Tomcat":
                        animal = new Tomcat(name, age);
                        break;

                    default:
                        throw new ArgumentException("Invalid input!");
                    }

                    Console.WriteLine(animal);
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                }

                type = Console.ReadLine();
            }
        }
예제 #5
0
        static void Main(string[] args)
        {
            var animals = new List <object>();

            while (true)
            {
                try
                {
                    string animalType = Console.ReadLine();
                    if (animalType == "Beast!")
                    {
                        break;
                    }

                    string[] animalParams = Console.ReadLine().Split();

                    switch (animalType)
                    {
                    case "Dog":
                        Dog dog = new Dog(animalParams[0], animalParams[1], animalParams[2]);
                        animals.Add(dog);
                        break;

                    case "Cat":
                        Cat cat = new Cat(animalParams[0], animalParams[1], animalParams[2]);
                        animals.Add(cat);
                        break;

                    case "Frog":
                        Frog frog = new Frog(animalParams[0], animalParams[1], animalParams[2]);
                        animals.Add(frog);
                        break;

                    case "Kitten":
                        Kitten kitten = new Kitten(animalParams[0], animalParams[1]);
                        animals.Add(kitten);
                        break;

                    case "Tomcat":

                        Tomcat tomcat = new Tomcat(animalParams[0], animalParams[1]);
                        animals.Add(tomcat);
                        break;

                    default:
                        throw new ArgumentException("Invalid input!");
                    }
                    Console.WriteLine(animals.Last());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
예제 #6
0
        static void Main()
        {
            string input;

            while ((input = Console.ReadLine()) != "Beast!")
            {
                var animal     = input;
                var animalArgs = Console.ReadLine()
                                 .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var    name   = animalArgs[0];
                var    age    = int.Parse(animalArgs[1]);
                Gender gender = Gender.None;
                Enum.TryParse(animalArgs[2], out gender);

                try
                {
                    switch (animal.ToLower())
                    {
                    case "dog":
                        var dog = new Dog(name, age, gender);
                        Console.WriteLine(dog);
                        break;

                    case "cat":
                        var cat = new Cat(name, age, gender);
                        Console.WriteLine(cat);
                        break;

                    case "frog":
                        var frog = new Frog(name, age, gender);
                        Console.WriteLine(frog);
                        break;

                    case "kitten":
                        var kitten = new Kitten(name, age, gender);
                        Console.WriteLine(kitten);
                        break;

                    case "tomcat":
                        var tomcat = new Tomcat(name, age, gender);
                        Console.WriteLine(tomcat);
                        break;

                    default:
                        throw new ArgumentException("Invalid input!");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
예제 #7
0
        public static void Main()
        {
            var type = Console.ReadLine();

            while (type != "Beast!")
            {
                var tokens = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                try
                {
                    Animal animal = null;

                    switch (type)
                    {
                    case "Cat":
                        animal = new Cat(tokens[0], int.Parse(tokens[1]), tokens[2]);
                        break;

                    case "Dog":
                        animal = new Dog(tokens[0], int.Parse(tokens[1]), tokens[2]);
                        break;

                    case "Frog":
                        animal = new Frog(tokens[0], int.Parse(tokens[1]), tokens[2]);
                        break;

                    case "Kittens":
                        animal = new Kitten(tokens[0], int.Parse(tokens[1]));
                        break;

                    case "Tomcat":
                        animal = new Tomcat(tokens[0], int.Parse(tokens[1]));
                        break;

                    default:
                        throw new ArgumentException("Invalid input!");
                    }

                    Console.WriteLine(animal);
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                }
                catch (FormatException fe)
                {
                    Console.WriteLine("Invalid input!");
                }

                type = Console.ReadLine();
            }
        }
예제 #8
0
        private static void ReadAndCreateAnimal(List <Animal> animals, string animalType)
        {
            string[] tokens = Console.ReadLine().Split();
            string   name   = tokens[0];
            int      age    = int.Parse(tokens[1]);
            string   gender = null;

            if (tokens.Length == 3)
            {
                gender = tokens[2];
            }

            switch (animalType)
            {
            case "Cat":
                var cat = new Cat(name, age, gender);
                animals.Add(cat);
                break;

            case "Dog":
                var dog = new Dog(name, age, gender);
                animals.Add(dog);
                break;

            case "Frog":
                var frog = new Frog(name, age, gender);
                animals.Add(frog);
                break;

            case "Tomcat":
                var tomcat = new Tomcat(name, age);
                animals.Add(tomcat);
                break;

            case "Kitten":
                var kitten = new Kitten(name, age);
                animals.Add(kitten);
                break;

            default:
                throw new ArgumentException("Invalid input!");
            }
        }
예제 #9
0
        private static void ParseInput(string input)
        {
            string[] tokens = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            switch (input)
            {
            case "Cat":
                var cat = new Cat(tokens[0], int.Parse(tokens[1]), tokens[2]);
                Console.WriteLine(cat);
                Console.WriteLine(cat.ProduceSound());
                break;

            case "Dog":
                var dog = new Dog(tokens[0], int.Parse(tokens[1]), tokens[2]);
                Console.WriteLine(dog);
                Console.WriteLine(dog.ProduceSound());
                break;

            case "Frog":
                var frog = new Frog(tokens[0], int.Parse(tokens[1]), tokens[2]);
                Console.WriteLine(frog);
                Console.WriteLine(frog.ProduceSound());
                break;

            case "Kitten":
                var kitten = new Kitten(tokens[0], int.Parse(tokens[1]));
                Console.WriteLine(kitten);
                Console.WriteLine(kitten.ProduceSound());
                break;

            case "Tomcat":
                var tomcat = new Tomcat(tokens[0], int.Parse(tokens[1]));
                Console.WriteLine(tomcat);
                Console.WriteLine(tomcat.ProduceSound());
                break;

            default:
                Console.WriteLine("Not implemented!");
                break;
            }
        }
예제 #10
0
파일: Program.cs 프로젝트: morskibg/OOP_1
        static void CreateAnimal(string animalData, string animalType, List <Animal> animals)
        {
            Animal currAnimal = null;

            string[] tokens = animalData.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim())
                              .ToArray();
            if (tokens.Length < 2)
            {
                throw new ArgumentException("Invalid input!");
            }
            if (animalType.ToLower() == "dog")
            {
                currAnimal = new Dog(tokens[0], tokens[1], tokens[2]);
            }
            else if (animalType.ToLower() == "cat")
            {
                currAnimal = new Cat(tokens[0], tokens[1], tokens[2]);
            }
            else if (animalType.ToLower() == "frog")
            {
                currAnimal = new Frog(tokens[0], tokens[1], tokens[2]);
            }
            else if (animalType.ToLower() == "kitten")
            {
                currAnimal = new Kitten(tokens[0], tokens[1], tokens.Length == 3 ? tokens[2] : null);
            }
            else if (animalType.ToLower() == "tomcat")
            {
                currAnimal = new Tomcat(tokens[0], tokens[1], tokens.Length == 3 ? tokens[2] : null);
            }
            else
            {
                throw new ArgumentException("Invalid input!");
            }
            if (currAnimal != null)
            {
                animals.Add(currAnimal);
            }
        }
예제 #11
0
        private static void AddValidAnimal(List <Animal> animals, string animalType, string name, int age, string gender)
        {
            Animal currentAnimal;

            switch (animalType)
            {
            case "Cat":
                currentAnimal = new Cat(name, age, gender);
                animals.Add(currentAnimal);
                break;

            case "Kitten":
                currentAnimal = new Kitten(name, age, gender);
                animals.Add(currentAnimal);
                break;

            case "Tomcat":
                currentAnimal = new Tomcat(name, age, gender);
                animals.Add(currentAnimal);
                break;

            case "Dog":
                currentAnimal = new Dog(name, age, gender);
                animals.Add(currentAnimal);
                break;

            case "Frog":
                currentAnimal = new Frog(name, age, gender);
                animals.Add(currentAnimal);
                break;

            default:
                Console.WriteLine("Invalid input!");
                break;
            }
        }
예제 #12
0
        static void Main(string[] args)
        {
            var data = Console.ReadLine();

            while (data != "Beast!")
            {
                try
                {
                    var    animal = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
                    string animalName;
                    int    animalAge;
                    string animalGender;

                    if (animal.Length != 3)
                    {
                        throw new ArgumentException("Invalid input!");
                    }

                    animalName = animal[0];
                    if (!int.TryParse(animal[1], out animalAge))
                    {
                        throw new ArgumentException("Invalid input!");
                    }

                    animalGender = animal[2];
                    switch (data)
                    {
                    case "Dog":
                        var dog = new Dog(animalName, animalAge, animalGender);
                        Console.WriteLine(dog.ProduceSound());
                        break;

                    case "Cat":
                        var cat = new Cat(animalName, animalAge, animalGender);
                        Console.WriteLine(cat.ProduceSound());
                        break;

                    case "Frog":
                        var frog = new Frog(animalName, animalAge, animalGender);
                        Console.WriteLine(frog.ProduceSound());
                        break;

                    case "Kitten":
                        var kitten = new Kitten(animalName, animalAge);
                        Console.WriteLine(kitten.ProduceSound());
                        break;

                    case "Tomcat":
                        var tomcat = new Tomcat(animalName, animalAge);
                        Console.WriteLine(tomcat.ProduceSound());
                        break;

                    default:
                        throw new ArgumentException("Invalid input!");
                    }
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                }

                data = Console.ReadLine();
            }
        }
예제 #13
0
        public static void Main()
        {
            string input;

            while ((input = Console.ReadLine()) != "Beast!")
            {
                try
                {
                    string[] animalElements = Console.ReadLine()
                                              .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    string name   = animalElements[0];
                    int    age    = int.Parse(animalElements[1]);
                    string gender = animalElements[2];

                    Animal animal;

                    if (input.ToLower() == "cat")
                    {
                        animal = new Cat(animalElements[0], age, gender);

                        Console.WriteLine(animal);
                    }
                    else if (input.ToLower() == "tomcat")
                    {
                        animal = new Tomcat(name, age, gender);

                        Console.WriteLine(animal);
                    }
                    else if (input.ToLower() == "kitten")
                    {
                        animal = new Kitten(name, age, gender);

                        Console.WriteLine(animal);
                    }
                    else if (input.ToLower() == "dog")
                    {
                        animal = new Dog(name, age, gender);

                        Console.WriteLine(animal);
                    }
                    else if (input.ToLower() == "frog")
                    {
                        animal = new Frog(name, age, gender);

                        Console.WriteLine(animal);
                    }
                    else
                    {
                        animal = new Animal(name, age, gender);
                        Console.WriteLine(animal);
                    }
                }
                catch (ArgumentException ae)
                {
                    Console.WriteLine(ae.Message);
                }
                catch (Exception)
                {
                    Console.WriteLine("Invalid input!");
                }
            }
        }
예제 #14
0
        static void Main()
        {
            string input;

            while ((input = Console.ReadLine()) != "Beast!")
            {
                string classType = input;
                var    tokens    = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                try
                {
                    if (tokens.Length < 3)
                    {
                        throw new ArgumentException("Invalid input!");
                    }

                    string name = tokens[0];
                    int    age;
                    string gender = tokens[2];

                    if (int.TryParse(tokens[1], out age) == false)
                    {
                        throw new ArgumentException("Invalid input!");
                    }

                    switch (classType)
                    {
                    case "Cat":
                    {
                        var cat = new Cat(name, age, gender);
                        Console.WriteLine(cat);
                        break;
                    }

                    case "Dog":
                    {
                        var dog = new Dog(name, age, gender);
                        Console.WriteLine(dog);
                        break;
                    }

                    case "Frog":
                    {
                        var frog = new Frog(name, age, gender);
                        Console.WriteLine(frog);
                        break;
                    }

                    case "Kitten":
                    {
                        var kitten = new Kitten(name, age, "Female");
                        Console.WriteLine(kitten);
                        break;
                    }

                    case "Tomcat":
                    {
                        var tomcat = new Tomcat(name, age, "Male");
                        Console.WriteLine(tomcat);
                        break;
                    }

                    default:
                    {
                        throw new ArgumentException("Invalid input!");
                    }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }