コード例 #1
0
        private static void GetAnimal(string animalType, StringBuilder animals, string name, int age, string gender) // COULD HAVE USED FACTORY PATTERN
        {
            switch (animalType)
            {
            case "Cat":
                var cat = new Cat(name, age, gender);
                animals.AppendLine(cat.ToString());
                break;

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

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

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

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

            default:
                animals.AppendLine("Invalid input!");
                break;
            }
        }
コード例 #2
0
        public static void Main(string[] args)
        {
            string input = Console.ReadLine().Trim();

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

                    string name = animalData[0];
                    double age  = double.Parse(animalData[1]);


                    switch (input.ToLower())
                    {
                    case "cat":
                        string gender = animalData[2];
                        Cat    cat    = new Cat(name, age, gender);
                        Console.WriteLine(cat);
                        break;

                    case "dog":
                        gender = animalData[2];
                        Dog dog = new Dog(name, age, gender);
                        Console.WriteLine(dog);
                        break;

                    case "frog":
                        gender = animalData[2];
                        Frog frog = new Frog(name, age, gender);
                        Console.WriteLine(frog);
                        break;

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

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


                    input = Console.ReadLine().Trim();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }