Advertisement
Guest User

WildFarm

a guest
Jul 9th, 2016
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.55 KB | None | 0 0
  1. using System;
  2.  
  3. namespace WildFarm
  4. {
  5.     public class WildFarmMain
  6.     {
  7.         public static void Main()
  8.         {
  9.             string input = Console.ReadLine();
  10.  
  11.             while (input != "End")
  12.             {
  13.                 var animal = CreateAnimal(input);
  14.                 var food = CreateFood(Console.ReadLine());
  15.  
  16.                 animal.MakeSound();
  17.                 animal.Eat(food);
  18.                 Console.WriteLine(animal);
  19.  
  20.                 input = Console.ReadLine();
  21.             }
  22.         }
  23.  
  24.         private static Food CreateFood(string input)
  25.         {
  26.             var foodInfo = SplitInput(input);
  27.  
  28.             string type = foodInfo[0];
  29.             int quantity = int.Parse(foodInfo[1]);
  30.  
  31.             if (type == "Vegetable")
  32.             {
  33.                 return new Vegetable(quantity);
  34.             }
  35.  
  36.             return new Meat(quantity);
  37.         }
  38.  
  39.         private static Animal CreateAnimal(string input)
  40.         {
  41.             var animalInfo = SplitInput(input);
  42.  
  43.             string type = animalInfo[0];
  44.             string name = animalInfo[1];
  45.             double weight = double.Parse(animalInfo[2]);
  46.             string livingRegion = animalInfo[3];
  47.  
  48.             if (type == "Mouse")
  49.             {
  50.                 return new Mouse(name, weight, livingRegion);
  51.             }
  52.             if (type == "Zebra")
  53.             {
  54.                 return new Zebra(name, weight, livingRegion);
  55.             }
  56.             if (type == "Tiger")
  57.             {
  58.                 return new Tiger(name, weight, livingRegion);
  59.             }
  60.  
  61.             string breed = animalInfo[4];
  62.             return new Cat(name, weight, livingRegion, breed);
  63.         }
  64.  
  65.         private static string[] SplitInput(string input)
  66.         {
  67.             return input.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
  68.         }
  69.     }
  70.  
  71.     public interface IFood
  72.     {
  73.         int Quantity { get; }
  74.     }
  75.  
  76.     public abstract class Food : IFood
  77.     {
  78.         protected Food(int quantity)
  79.         {
  80.             this.Quantity = quantity;
  81.         }
  82.  
  83.         public int Quantity { get; set; }
  84.     }
  85.  
  86.     public class Vegetable : Food
  87.     {
  88.         public Vegetable(int quantity)
  89.             : base(quantity)
  90.         {
  91.         }
  92.     }
  93.  
  94.     public class Meat : Food
  95.     {
  96.         public Meat(int quantity)
  97.             : base(quantity)
  98.         {
  99.         }
  100.     }
  101.  
  102.     public interface ISoundProducable
  103.     {
  104.         void MakeSound();
  105.     }
  106.  
  107.     public interface IEat
  108.     {
  109.         void Eat(IFood food);
  110.     }
  111.  
  112.     public abstract class Animal : ISoundProducable, IEat
  113.     {
  114.         protected Animal(string name, double weight)
  115.         {
  116.             this.Name = name;
  117.             this.Weight = weight;
  118.         }
  119.  
  120.         public string Name { get; protected set; }
  121.  
  122.         public double Weight { get; protected set; }
  123.  
  124.         public int FoodEaten { get; protected set; }
  125.  
  126.         public abstract void MakeSound();
  127.  
  128.         public abstract void Eat(IFood food);
  129.     }
  130.  
  131.     public abstract class Mammal : Animal
  132.     {
  133.         protected Mammal(string name, double weight, string livingRegion)
  134.             : base(name, weight)
  135.         {
  136.             this.LivingRegion = livingRegion;
  137.         }
  138.  
  139.         public string LivingRegion { get; protected set; }
  140.  
  141.         public override string ToString()
  142.         {
  143.             string result = string.Format("{0}[{1}, {2}, {3}, {4}]",
  144.                 this.GetType().Name,
  145.                 this.Name,
  146.                 this.Weight,
  147.                 this.LivingRegion,
  148.                 this.FoodEaten);
  149.  
  150.             return result;
  151.         }
  152.     }
  153.  
  154.     public abstract class Feline : Mammal
  155.     {
  156.         protected Feline(string name, double weight, string livingRegion)
  157.             : base(name, weight, livingRegion)
  158.         {
  159.         }
  160.     }
  161.  
  162.     public class Mouse : Mammal
  163.     {
  164.         public Mouse(string name, double weight, string livingRegion)
  165.             : base(name, weight, livingRegion)
  166.         {
  167.         }
  168.  
  169.         public override void MakeSound()
  170.         {
  171.             Console.WriteLine("SQUEEEAAAK!");
  172.         }
  173.  
  174.         public override void Eat(IFood food)
  175.         {
  176.             if (food is Vegetable)
  177.             {
  178.                 this.FoodEaten += food.Quantity;
  179.                 Console.WriteLine("A cheese was just eaten!");
  180.             }
  181.             else
  182.             {
  183.                 Console.WriteLine("Mice are not eating that type of food!");
  184.             }
  185.         }
  186.     }
  187.  
  188.     public class Zebra : Mammal
  189.     {
  190.         public Zebra(string name, double weight, string livingRegion)
  191.             : base(name, weight, livingRegion)
  192.         {
  193.         }
  194.  
  195.         public override void MakeSound()
  196.         {
  197.             Console.WriteLine("Zs");
  198.         }
  199.  
  200.         public override void Eat(IFood food)
  201.         {
  202.             if (food is Vegetable)
  203.             {
  204.                 this.FoodEaten += food.Quantity;
  205.             }
  206.             else
  207.             {
  208.                 Console.WriteLine("Zebras are not eating that type of food!");
  209.             }
  210.         }
  211.     }
  212.  
  213.     public class Tiger : Feline
  214.     {
  215.         public Tiger(string name, double weight, string livingRegion)
  216.             : base(name, weight, livingRegion)
  217.         {
  218.         }
  219.  
  220.         public override void MakeSound()
  221.         {
  222.             Console.WriteLine("ROAAR!!!");
  223.         }
  224.  
  225.         public override void Eat(IFood food)
  226.         {
  227.             if (food is Meat)
  228.             {
  229.                 this.FoodEaten += food.Quantity;
  230.             }
  231.             else
  232.             {
  233.                 Console.WriteLine("Tigers are not eating that type of food!");
  234.             }
  235.         }
  236.     }
  237.  
  238.     public class Cat : Feline
  239.     {
  240.         public Cat(string name, double weight, string livingRegion, string breed)
  241.             : base(name, weight, livingRegion)
  242.         {
  243.             this.Breed = breed;
  244.         }
  245.  
  246.         public string Breed { get; protected set; }
  247.  
  248.         public override void MakeSound()
  249.         {
  250.             Console.WriteLine("Meowwww");
  251.         }
  252.  
  253.         public override void Eat(IFood food)
  254.         {
  255.             this.FoodEaten += food.Quantity;
  256.         }
  257.  
  258.         public override string ToString()
  259.         {
  260.             string result = string.Format("{0}[{1}, {2}, {3}, {4}, {5}]",
  261.                 this.GetType().Name,
  262.                 this.Name,
  263.                 this.Breed,
  264.                 this.Weight,
  265.                 this.LivingRegion,
  266.                 this.FoodEaten);
  267.  
  268.             return result;
  269.         }
  270.     }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement