Advertisement
Guest User

Animals

a guest
Jul 7th, 2019
4,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  1. using Animals.Cats;
  2. using System;
  3.  
  4. namespace Animals
  5. {
  6.     public class StartUp
  7.     {
  8.         public static void Main(string[] args)
  9.         {
  10.             string input = Console.ReadLine();
  11.  
  12.             while (input != "Beast!")
  13.             {
  14.                 string type = input;
  15.  
  16.                 string[] animalTokens = Console.ReadLine()
  17.                     .Split();
  18.  
  19.                 try
  20.                 {
  21.                     Animal animal = newAnimal(type, animalTokens);
  22.                     Console.WriteLine(type);
  23.                     Console.WriteLine(animal);
  24.                     animal.ProduceSound();
  25.                 }
  26.                 catch (Exception exception)
  27.                 {
  28.                     Console.WriteLine(exception.Message);
  29.                 }
  30.  
  31.                 input = Console.ReadLine();
  32.             }
  33.         }
  34.  
  35.         private static Animal newAnimal(string type, string[] animalTokens)
  36.         {
  37.             switch (type)
  38.             {
  39.                 case "Dog":
  40.                     return new Dog(animalTokens[0], int.Parse(animalTokens[1]), animalTokens[2]);
  41.                 case "Frog":
  42.                     return new Frog(animalTokens[0], int.Parse(animalTokens[1]), animalTokens[2]);
  43.                 case "Cat":
  44.                     return new Cat(animalTokens[0], int.Parse(animalTokens[1]), animalTokens[2]);
  45.                 case "Tomcat":
  46.                     return new Tomcat(animalTokens[0], int.Parse(animalTokens[1]));
  47.                 case "Kitten":
  48.                     return new Kitten(animalTokens[0], int.Parse(animalTokens[1]));
  49.                 default:
  50.                     throw new ArgumentException("Invalid input!");
  51.             }
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement