public void Feed(Animal animal, IEdible edibleProduct, int productQuantity) { if (edibleProduct.Quantity >= productQuantity) { animal.Eat(edibleProduct, productQuantity); edibleProduct.Quantity -= productQuantity; } else { throw new ArgumentException("There is not enough quantity of the specified product."); } }
public void Run() { var animals = new List <Animal>(); while (true) { var animalInfo = Console.ReadLine().Split(); if (animalInfo[0] == "End") { break; } Animal animal = AnimalFactory.CreateAnimal(animalInfo); animals.Add(animal); var foodInfo = Console.ReadLine().Split(); string type = foodInfo[0]; int quantity = int.Parse(foodInfo[1]); Food food = FoodFactory.CreateFood(type, quantity); try { animal.ProduceSound(); animal.Eat(food); } catch (ArgumentException exception) { Console.WriteLine(exception.Message); } } foreach (var animal in animals) { Console.WriteLine(animal); } }
public static void Main() { var animalInfo = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); while (animalInfo[0] != "End") { Animal animal = CreateAnimal(animalInfo); var foodInfo = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); Food food = CreateFood(foodInfo); Console.WriteLine(animal.MakeSound()); try { animal.Eat(food); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine(animal); animalInfo = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); } }
public static void Main() { string input = string.Empty; while ((input = Console.ReadLine()) != "End") { var tokens = input.Split(new char[] { '\t', '\n', ' ' }, StringSplitOptions.RemoveEmptyEntries); Animal animal = AnimalFactory.GetAnimal(tokens); var foodTokens = Console.ReadLine().Split(new char[] { '\t', '\n', ' ' }, StringSplitOptions.RemoveEmptyEntries); Food food = FoodFactory.GetFood(foodTokens); Console.WriteLine(animal.MakeSound()); try { animal.Eat(food); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine(animal); } }
static void Main(string[] args) { string input = Console.ReadLine(); List <Animal> animals = new List <Animal>(); while (input != "End") { string[] animalArgs = input .Split(); Animal animal = AnimalFactory.CreateAnimal(animalArgs); animals.Add(animal); string[] foodArgs = Console.ReadLine() .Split(); Food food = FoodFactory.CreateFood(foodArgs); Console.WriteLine(animal.ProduceSound()); try { animal.Eat(food); } catch (Exception ex) { Console.WriteLine(ex.Message); } input = Console.ReadLine(); } foreach (var animal in animals) { Console.WriteLine(animal.ToString()); } }
public void Run() { List <Animal> animals = new List <Animal>(); string animalInput; while ((animalInput = Console.ReadLine()) != "End") { //Felines - "{Type} {Name} {Weight} {LivingRegion} {Breed}"; //Birds - "{Type} {Name} {Weight} {WingSize}"; //Mice and Dogs - "{Type} {Name} {Weight} {LivingRegion}"; string[] animalTokens = animalInput.Split(" ", StringSplitOptions.RemoveEmptyEntries); string type = animalTokens[0]; string name = animalTokens[1]; double weight = double.Parse(animalTokens[2]); Animal newAnimal = null; newAnimal = CreateAnimal(animals, animalTokens, type, name, weight, newAnimal); if (newAnimal != null) { Console.WriteLine(newAnimal.ProduceSound()); Food food = null; food = CreateFood(food); try { newAnimal.Eat(food); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } } } foreach (var animal in animals) { Console.WriteLine(animal); } }
public void Run() { while (true) { string input = Console.ReadLine(); if (input == "End") { break; } string[] animalArgs = input.Split(); Animal animal = AnimalFactory.CreateAnimal(animalArgs); string[] foodArgs = Console.ReadLine().Split(); Food food = FoodFactory.CreateFood(foodArgs); Console.WriteLine(animal.ProduceSound()); try { animal.Eat(food); } catch (ArgumentException ae) { Console.WriteLine(ae.Message); } animals.Add(animal); } foreach (var animal in animals) { Console.WriteLine(animal); } }
public static void Main() { List <Animal> animals = new List <Animal>(); while (true) { string input = Console.ReadLine(); if (input == "End") { break; } string animalInput = input; Animal animal = AnimalFactory.CreateAnimal(animalInput); animals.Add(animal); string foodInput = Console.ReadLine(); Food food = FoodFactory.CreateFood(foodInput); try { Console.WriteLine(animal.ProduceSound()); animal.Eat(food); } catch (ArgumentException exception) { Console.WriteLine(exception.Message); } } foreach (var animal in animals) { Console.WriteLine(animal.ToString()); } }
public static void Main() { List <Animal> animals = new List <Animal>(); FoodFactory foodFactory = new FoodFactory(); AnimalFactory animalFactory = new AnimalFactory(); string input; while ((input = Console.ReadLine()) != "End") { string[] animalTokens = input.Split(); Animal animal = animalFactory.GetAnimal(animalTokens[0], animalTokens); string[] foodTokens = Console.ReadLine().Split(); Food food = foodFactory.GetFood(foodTokens[0], int.Parse(foodTokens[1])); Console.WriteLine(animal.ProduceSound()); try { animal.Eat(food); } catch (ArgumentException ae) { Console.WriteLine(ae.Message); } animals.Add(animal); } foreach (var animal in animals) { Console.WriteLine(animal); } }
static void Main(string[] args) { List <Animal> animals = new List <Animal>(); while (true) { string line = Console.ReadLine(); if (line == "End") { break; } string[] animalParts = line.Split(); Animal animal = CreateAnimal(animalParts); animals.Add(animal); string[] foodParts = Console.ReadLine().Split(); Food food = CreateFood(foodParts); Console.WriteLine(animal.ProduceSound()); try { animal.Eat(food); } catch (InvalidOperationException ex) { Console.WriteLine(ex.Message); } } foreach (var animal in animals) { Console.WriteLine(animal); } }
public static void Main() { var animalInput = Console.ReadLine().Split(); while (animalInput[0] != "End") { var foodInput = Console.ReadLine().Split(); Food food = CraeteFood(foodInput); Animal animal = CreateAnimal(animalInput, food); try { Console.WriteLine(animal.MakeSound()); animal.Eat(); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine(animal.ToString()); animalInput = Console.ReadLine().Split(); } }
private static void Main() { List <Animal> animals = new List <Animal>(); string input = string.Empty; while ((input = Console.ReadLine()) != "End") { Animal animal = null; Food food = null; try { animal = AnimalFactory.GetAnimal(input.Split()); food = FoodFactory.GetFood(Console.ReadLine().Split()); } catch (ArgumentException) { continue; } animal.MakeSound(); try { animal.Eat(food); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } animals.Add(animal); } animals.ForEach(a => Console.WriteLine(a.ToString())); }
public static void Main() { string input = Console.ReadLine(); List <Animal> animals = new List <Animal>(); while (input != "End") { string[] animalInfo = input.Split(); Animal animal = AnimalFactory.Create(animalInfo); string[] foodInfo = Console.ReadLine().Split(); Food food = FoodFactory.Create(foodInfo); Console.WriteLine(animal.AskForFood()); try { animal.Eat(food); } catch (Exception ex) { Console.WriteLine(ex.Message); } animals.Add(animal); input = Console.ReadLine(); } foreach (Animal animal in animals) { Console.WriteLine(animal); } }
private static void EatFood(Animal animal, Food currentFood) { animal.MakeSound(); animal.Eat(currentFood); animals.Add(animal); }
/// <summary> /// 喂食 /// </summary> /// <param name="animal"></param> /// <param name="food"></param> public void Feed(Animal animal, Food food) { animal.Eat(food); }
public void Run() { string input = Console.ReadLine(); Animal animal = null; while (!input.Equals("End")) { string[] inputArguments = input.Split(); string type = inputArguments[0]; string name = inputArguments[1];; double weight = double.Parse(inputArguments[2]); try { if (type.Equals("Cat") || type.Equals("Tiger")) { string livingRegion = inputArguments[3]; string breed = inputArguments[4]; animal = felineFactory.CreateFeline(type, name, weight, livingRegion, breed); } else if (type.Equals("Dog") || type.Equals("Mouse")) { string livingRegion = inputArguments[3]; animal = mammalsFactory.CreateMamamls(type, name, weight, livingRegion); } else if (type.Equals("Owl") || type.Equals("Hen")) { double wingSize = double.Parse(inputArguments[3]); animal = birdFactory.CreateBirds(type, name, weight, wingSize); } } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } string secondInput = Console.ReadLine(); string[] secondInputs = secondInput.Split(); var foodType = foodfactory.CreateFoods(secondInputs[0], int.Parse(secondInputs[1])); animal.ProduceSound(); try { animal.Eat(foodType); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } animals.Add(animal); input = Console.ReadLine(); } foreach (var currentAnimal in animals) { Console.WriteLine(currentAnimal); } }
public void Run() { BirdFactory birdFactory = new BirdFactory(); MammalFactory mammalFactory = new MammalFactory(); FelineFactory felineFactory = new FelineFactory(); FoodFactory foodFactory = new FoodFactory(); List <Animal> animals = new List <Animal>(); while (true) { string[] currentAnimal = Console.ReadLine().Split(); if (currentAnimal[0] == "End") { break; } string[] currentFood = Console.ReadLine().Split(); string animalType = currentAnimal[0]; string animalName = currentAnimal[1]; double animalWeight = double.Parse(currentAnimal[2]); string foodType = currentFood[0]; int foodQuantity = int.Parse(currentFood[1]); Food food = foodFactory.CreateFood(foodType, foodQuantity); try { if (animalType == "Hen" || animalType == "Owl") { double wingSize = double.Parse(currentAnimal[3]); Animal animal = birdFactory.CreateBird(animalType, animalName, animalWeight, wingSize); animal.ProduceSound(); animals.Add(animal); animal.Eat(food); } else if (animalType == "Mouse" || animalType == "Dog") { string livingRegion = currentAnimal[3]; Animal animal = mammalFactory.CreateMammal(animalType, animalName, animalWeight, livingRegion); animal.ProduceSound(); animals.Add(animal); animal.Eat(food); } else if (animalType == "Cat" || animalType == "Tiger") { string livingRegion = currentAnimal[3]; string breed = currentAnimal[4]; Animal animal = felineFactory.CreateFeline(animalType, animalName, animalWeight, livingRegion, breed); animal.ProduceSound(); animals.Add(animal); animal.Eat(food); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } foreach (Animal animal in animals) { Console.WriteLine(animal); } }
void GetAnimalEat(AnimalFactory fa) { Animal am = fa.GetAnimal(); am.Eat(); }
private static void ProcessEating(Animal animal, Food food) { animal.MakeSound(); animal.Eat(food); Console.WriteLine(animal.ToString()); }
public void FeedAnimal(Animal animal) { var food = new Food(12000); animal.Eat(food); }
private static void FeedCurrentAnimal(string[] parameters) { food = FarmFactory.CreateFood(parameters); animal.Eat(food); animals.Add(animal); }
public static void Main(string[] args) { List <Animal> animals = new List <Animal>(); Animal currentAnimal = null; int currentLine = 0; while (true) { string command = Console.ReadLine(); if (command == "End") { break; } string[] tokens = command.Split(); if (currentLine == 0 || currentLine % 2 == 0) { Animal animal = null; string name = tokens[1]; double weight = double.Parse(tokens[2]); if (tokens[0] == "Cat") { string livingRegion = tokens[3]; string breed = tokens[4]; animal = new Cat(name, weight, livingRegion, breed); animals.Add(animal); } else if (tokens[0] == "Tiger") { string livingRegion = tokens[3]; string breed = tokens[4]; animal = new Tiger(name, weight, livingRegion, breed); animals.Add(animal); } else if (tokens[0] == "Dog") { string livingRegion = tokens[3]; animal = new Dog(name, weight, livingRegion); animals.Add(animal); } else if (tokens[0] == "Mouse") { string livingRegion = tokens[3]; animal = new Mouse(name, weight, livingRegion); animals.Add(animal); } else if (tokens[0] == "Hen") { double wingSize = double.Parse(tokens[3]); animal = new Hen(name, weight, wingSize); animals.Add(animal); } else if (tokens[0] == "Owl") { double wingSize = double.Parse(tokens[3]); animal = new Owl(name, weight, wingSize); animals.Add(animal); } currentAnimal = animal; animal.ProduceSound(); } else { int quantity = int.Parse(tokens[1]); try { if (tokens[0] == "Fruit") { Fruit fruit = new Fruit(quantity); currentAnimal.Eat(fruit); } else if (tokens[0] == "Meat") { Meat meat = new Meat(quantity); currentAnimal.Eat(meat); } else if (tokens[0] == "Seeds") { Seeds seeds = new Seeds(quantity); currentAnimal.Eat(seeds); } else if (tokens[0] == "Vegetable") { Vegetable vegetable = new Vegetable(quantity); currentAnimal.Eat(vegetable); } } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } } currentLine++; } foreach (var animal in animals) { Console.WriteLine(animal); } }
static void Main() { Animal[] animals = new Animal[4]; Animal animal = new Animal(); Bird bird = new Bird(); bird.name = "Vasia"; Cat cat = new Cat(); Lion lion = new Lion(); animals [0] = animal; animals [1] = bird; animals [2] = cat; animals [3] = lion; animal.Eat(); bird.Eat(); cat.Eat(); lion.Eat(); //animal.cells = 0; Console.WriteLine(); int i; for (i = 0; i < 4; i++) { animals[i].Eat(); } Console.WriteLine(); ((Cat)animals[2]).Eat(); Console.WriteLine(); IFlying[] flying = new IFlying[2]; Bat bat = new Bat(); flying [0] = bat; flying [1] = bird; for (i = 0; i < 2; i++) { flying [i].Fly(); } Console.WriteLine(); Man man = new Man(); Batman batman = new Batman(); man.Eat(); batman.Eat(); batman.Fly(); Console.WriteLine(); Console.WriteLine(Animal.Quantity); }
static void Main(string[] args) { string command; List <Animal> animals = new List <Animal>(); while ((command = Console.ReadLine()) != "End") { string[] cmdArgs = command.Split().ToArray(); string animalType = cmdArgs[0]; string name = cmdArgs[1]; double weight = double.Parse(cmdArgs[2]); if (animalType == "Owl") { Owl owl = new Owl(name, weight, double.Parse(cmdArgs[3])); animals.Add(owl); Console.WriteLine(Sound(owl)); } else if (animalType == "Hen") { Hen hen = new Hen(name, weight, double.Parse(cmdArgs[3])); animals.Add(hen); Console.WriteLine(Sound(hen)); } else if (animalType == "Mouse") { Mouse mouse = new Mouse(name, weight, cmdArgs[3]); animals.Add(mouse); Console.WriteLine(Sound(mouse)); } else if (animalType == "Dog") { Dog dog = new Dog(name, weight, cmdArgs[3]); animals.Add(dog); Console.WriteLine(Sound(dog)); } else if (animalType == "Cat") { Cat cat = new Cat(name, weight, cmdArgs[3], cmdArgs[4]); animals.Add(cat); Console.WriteLine(Sound(cat)); } else if (animalType == "Tiger") { Tiger tiger = new Tiger(name, weight, cmdArgs[3], cmdArgs[4]); animals.Add(tiger); Console.WriteLine(Sound(tiger)); } string[] foodArgs = Console.ReadLine() .Split() .ToArray(); string TypeOfFood = foodArgs[0]; int quantityOfFood = int.Parse(foodArgs[1]); try { Animal currentAnimal = animals.Last(); currentAnimal.Eat(TypeOfFood, quantityOfFood); } catch (ArgumentException e) { Console.WriteLine(e.Message); } } foreach (var animal in animals) { Console.WriteLine(animal.ToString()); } }
public void FeedAnimal(Animal animal) { Console.WriteLine($"\n{Name} feed {animal.GetType().Name} {animal.Name} by {animal.KindEat}"); animal.Eat(); }
public void Feed(Animal animal, IEdible edibleProduct, int productQuantity) { animal.Eat(edibleProduct, productQuantity); edibleProduct.Quantity -= productQuantity; }
static void Main(string[] args) { /* * Comments: * * Shte si govorim za : * * 1.Inheritance v C# (Nasledqvane v C#) * 2.Class Hierarchies (klasicheska ierarhiq) * 3.Accessing Members of the Base Class (dostup do chlenove na bazoviq klas) * 4.Virtual Classes (Virtualni metodi) * 5.Overriding Methods (Prezapisvane na metodi) * 6.Generic Classess (Generic klasove i kolekcii) */ /* * 1. Nasledqvane: * imame bazov klas (Base Class) (Parent Class) (SuperClass) i - predava na child klasa ! * podklas (SubCalss) (Child Class) - vzima ot base klasa ! * * podklasa moje da si ima i sobstveni dopulnitelni propertita * * * PRIMER: Imame klas Person() (base class) koito e nasleden ot klas Employee i klas Student (child classes) * No Child klasovete mogat da si imat i te sobstveni propertita * NASLEDQVA SE S: : Person * * * V praktikata shte imamo mnogo po zadulbocheni primeri ot tova !!! * * * */ //SEGA KLASA Dog IMA VSICHKI NESHTA OT KLASA Animal BEZ DA SME MU GI DOBAVQI. Dog dog = new Dog("Gafy", 9, 15); Console.WriteLine(dog.Name); Console.WriteLine(dog.Age); dog.Eat(); //SEGA TOVA PUPPY IMA VSICHKO OT PREDISHNITE KLASOVE NAD NEGO. Puppy puppy = new Puppy("Jaky", 16, 5); puppy.Eat(); puppy.Weep(); // plache //Kotkata nasledqva samo klasa Animal !!! Cat cat = new Cat("Sisa", 7, 4); cat.Eat(); cat.Meaw(); //KONSTRUKTORITE SE PREIZPOLZVAT !!! /* * Nasledqvaneto e proces kato stupalo. * NQMAME MULTIPLE INHERITANCE V C#, TRQBVA DA SA EDNO SLED DRUGO ! * * NE MOJEM PROSTO DA NAPISHEM : Puppy : Dog : Animal */ /* * * S 'override' prezapisvame metodi ! * VIRTUALNITE METODI MOGAT DA SE PREZAPISVAT ! * * VIRTUALNI METODI: * ToString() e edin vgraden virtualen metod ! * * Ponqkoga ni trqbva da mojem da promenqme metodi ot klasus koito * nasledqvame. * * Virtualnite metodi mogat da se promenqt * POZVOLQVAT NI DA GI PREZAPISVAME I DA IM PROMENQME LOGIKATA! * pishet se s dumata 'virtual' ! */ Console.WriteLine(); Console.WriteLine(); //PRIMER : Prezapisvame metoda Eating za vseki klas //Prezapisahme virtualniq metod ot klasa Animal !!! dog.Eat(); // pokazva Dog Eating ... puppy.Eat(); cat.Eat(); Animal animal = new Animal("Lion", 20, 250); animal.Eat(); // pokazva Animal Eating ... /* * Interfeisi: * Te sa neshto kato klasove * i se krushtavat zpochvaiki s 'I' i polse sushtestvitelno . * Primerno : * 1.IList t.e. sudurja samo spisuci, * 2.IDictionary t.e. sudurja samo rechnici. * * public interface IMovable * { * . . . * } * */ // Tuk si izvikvame metoda koito polzva interfeisa IMovable koito suzdadohme MoveObject(cat, 10); /* * Generic Collections: * * tuk govorim za system.collection.generic * TE SA KATO PLACEHOLDERI KOITO POLZVAME Z DA SLAGAME TIPOVE DANNI, * Mojem da si slagame kakuvto tip danni si iskame, DAJE I KLASOVE KOITO * SME SI NAPRAVILI MOGAT DA SA TIPOVE DANNI I MOJEM DA PRAVIM KOLEKCII OT TQH !!! * * */ /*SUMMARY: * 1.Nasledqvaneto e nestho mnogo nujno i polezno. * Imame base class i child class. * Child klasovete mogat da prezapisvat virtualni metodi ot baze klasa. * 2.interfeisa e kato dokument koito ni pokazva koi klaza za kakvo e, ulesnqva ni rabotata. * * 3.Generic kolekciite sa konteineri za vsqkauv tip danni daje i pt tip klas koito sme suzdali. * Ne mojem da im slagame drug tip danni ot tozi koito sme im zadali. * * */ }
public void Run() { string input = string.Empty; while ((input = Console.ReadLine()) != "End") { try { string[] animalInfo = input .Split(" ", StringSplitOptions.RemoveEmptyEntries); string[] foodInfo = Console.ReadLine() .Split(" ", StringSplitOptions.RemoveEmptyEntries); string animalType = animalInfo[0]; string name = animalInfo[1]; double weight = double.Parse(animalInfo[2]); if (animalType == "Hen" || animalType == "Owl") { double wingSize = double.Parse(animalInfo[3]); animal = this.birdFactory.CreateBird(animalType, name, weight, wingSize); } else if (animalType == "Mouse" || animalType == "Dog") { string livingRegion = animalInfo[3]; animal = this.mammalFactory.CreateMammal(animalType, name, weight, livingRegion); } else if (animalType == "Cat" || animalType == "Tiger") { string livingRegion = animalInfo[3]; string breed = animalInfo[4]; animal = this.felineFactory.CreateFeline(animalType, name, weight, livingRegion, breed); } string foodType = foodInfo[0]; int foodQuantity = int.Parse(foodInfo[1]); food = this.foodFactory.CreateFood(foodType, foodQuantity); animal.ProduceSound(); animal.Eat(food); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } animals.Add(animal); } foreach (var animal in animals) { Console.WriteLine(animal); } }
public void FeedTheAnimal(Animal animal) { //NOTE: Eat method is a virtaul method animal.Eat(); }