Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace WildFarm
- {
- public class WildFarmMain
- {
- public static void Main()
- {
- string input = Console.ReadLine();
- while (input != "End")
- {
- var animal = CreateAnimal(input);
- var food = CreateFood(Console.ReadLine());
- animal.MakeSound();
- animal.Eat(food);
- Console.WriteLine(animal);
- input = Console.ReadLine();
- }
- }
- private static Food CreateFood(string input)
- {
- var foodInfo = SplitInput(input);
- string type = foodInfo[0];
- int quantity = int.Parse(foodInfo[1]);
- if (type == "Vegetable")
- {
- return new Vegetable(quantity);
- }
- return new Meat(quantity);
- }
- private static Animal CreateAnimal(string input)
- {
- var animalInfo = SplitInput(input);
- string type = animalInfo[0];
- string name = animalInfo[1];
- double weight = double.Parse(animalInfo[2]);
- string livingRegion = animalInfo[3];
- if (type == "Mouse")
- {
- return new Mouse(name, weight, livingRegion);
- }
- if (type == "Zebra")
- {
- return new Zebra(name, weight, livingRegion);
- }
- if (type == "Tiger")
- {
- return new Tiger(name, weight, livingRegion);
- }
- string breed = animalInfo[4];
- return new Cat(name, weight, livingRegion, breed);
- }
- private static string[] SplitInput(string input)
- {
- return input.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
- }
- }
- public interface IFood
- {
- int Quantity { get; }
- }
- public abstract class Food : IFood
- {
- protected Food(int quantity)
- {
- this.Quantity = quantity;
- }
- public int Quantity { get; set; }
- }
- public class Vegetable : Food
- {
- public Vegetable(int quantity)
- : base(quantity)
- {
- }
- }
- public class Meat : Food
- {
- public Meat(int quantity)
- : base(quantity)
- {
- }
- }
- public interface ISoundProducable
- {
- void MakeSound();
- }
- public interface IEat
- {
- void Eat(IFood food);
- }
- public abstract class Animal : ISoundProducable, IEat
- {
- protected Animal(string name, double weight)
- {
- this.Name = name;
- this.Weight = weight;
- }
- public string Name { get; protected set; }
- public double Weight { get; protected set; }
- public int FoodEaten { get; protected set; }
- public abstract void MakeSound();
- public abstract void Eat(IFood food);
- }
- public abstract class Mammal : Animal
- {
- protected Mammal(string name, double weight, string livingRegion)
- : base(name, weight)
- {
- this.LivingRegion = livingRegion;
- }
- public string LivingRegion { get; protected set; }
- public override string ToString()
- {
- string result = string.Format("{0}[{1}, {2}, {3}, {4}]",
- this.GetType().Name,
- this.Name,
- this.Weight,
- this.LivingRegion,
- this.FoodEaten);
- return result;
- }
- }
- public abstract class Feline : Mammal
- {
- protected Feline(string name, double weight, string livingRegion)
- : base(name, weight, livingRegion)
- {
- }
- }
- public class Mouse : Mammal
- {
- public Mouse(string name, double weight, string livingRegion)
- : base(name, weight, livingRegion)
- {
- }
- public override void MakeSound()
- {
- Console.WriteLine("SQUEEEAAAK!");
- }
- public override void Eat(IFood food)
- {
- if (food is Vegetable)
- {
- this.FoodEaten += food.Quantity;
- Console.WriteLine("A cheese was just eaten!");
- }
- else
- {
- Console.WriteLine("Mice are not eating that type of food!");
- }
- }
- }
- public class Zebra : Mammal
- {
- public Zebra(string name, double weight, string livingRegion)
- : base(name, weight, livingRegion)
- {
- }
- public override void MakeSound()
- {
- Console.WriteLine("Zs");
- }
- public override void Eat(IFood food)
- {
- if (food is Vegetable)
- {
- this.FoodEaten += food.Quantity;
- }
- else
- {
- Console.WriteLine("Zebras are not eating that type of food!");
- }
- }
- }
- public class Tiger : Feline
- {
- public Tiger(string name, double weight, string livingRegion)
- : base(name, weight, livingRegion)
- {
- }
- public override void MakeSound()
- {
- Console.WriteLine("ROAAR!!!");
- }
- public override void Eat(IFood food)
- {
- if (food is Meat)
- {
- this.FoodEaten += food.Quantity;
- }
- else
- {
- Console.WriteLine("Tigers are not eating that type of food!");
- }
- }
- }
- public class Cat : Feline
- {
- public Cat(string name, double weight, string livingRegion, string breed)
- : base(name, weight, livingRegion)
- {
- this.Breed = breed;
- }
- public string Breed { get; protected set; }
- public override void MakeSound()
- {
- Console.WriteLine("Meowwww");
- }
- public override void Eat(IFood food)
- {
- this.FoodEaten += food.Quantity;
- }
- public override string ToString()
- {
- string result = string.Format("{0}[{1}, {2}, {3}, {4}, {5}]",
- this.GetType().Name,
- this.Name,
- this.Breed,
- this.Weight,
- this.LivingRegion,
- this.FoodEaten);
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement