public void Execute() { List <Animal> animals = new List <Animal>(); string input = Console.ReadLine(); while (input != "End") { try { string[] animalArgs = input.Split(); Animal animal = AnimalFactory.Create(animalArgs); Console.WriteLine(animal.ProduceSound()); string[] foodArgs = Console.ReadLine().Split(); Food food = FoodFactory.Create(foodArgs); animal.Eat(food); animals.Add(animal); } catch (Exception ex) { Console.WriteLine(ex.Message); } input = Console.ReadLine(); } animals.ForEach(Console.WriteLine); }
public void Can_create_all_animals() { var af = new AnimalFactory(); af.Create <Dog>().Should().BeOfType <Dog>(); af.Create <Cat>().Should().BeOfType <Cat>(); af.Create <Fish>().Should().BeOfType <Fish>(); af.Create <Monkey>().Should().BeOfType <Monkey>(); af.Create <Horse>().Should().BeOfType <Horse>(); // af.Create("Dog").Should().BeOfType<Dog>(); // af.Create("Cat").Should().BeOfType<Cat>(); // af.Create("Fish").Should().BeOfType<Fish>(); // af.Create("Monkey").Should().BeOfType<Monkey>(); // af.Create("Horse").Should().BeOfType<Horse>(); }
static void Main(string[] args) { IAnimalFactory factory = new AnimalFactory(); IAnimal animal = factory.Create("fish"); animal.MakeSound(); }
public void Run() { List <Animal> animals = new List <Animal>(); string command; while ((command = Console.ReadLine()) != "End") { string[] animalArgs = command.Split(" ", StringSplitOptions.RemoveEmptyEntries) .ToArray(); Animal animal = AnimalFactory.Create(animalArgs); animals.Add(animal); Console.WriteLine(animal.ProduceSound()); string[] foodArgs = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries) .ToArray(); Food food = FoodFactory.CreateFood(foodArgs); try { animal.EatFood(food); } catch (ArgumentException e) { Console.WriteLine(e.Message); } } animals.ForEach(Console.WriteLine); }
public void Run() { string command; while ((command = Console.ReadLine()) != "End") { Animal animal = AnimalFactory.Create(command.Split(' ', StringSplitOptions.RemoveEmptyEntries)); animals.Add(animal); Console.WriteLine(animal.ProduceSound()); Food food = FoodFactory.Create(Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries)); try { animal.EatFood(food); } catch (ArgumentException ae) { Console.WriteLine(ae.Message); } } animals.ForEach(Console.WriteLine); }
public void RegisterAnimal(string[] inputData) { var adoptionCenterName = inputData[4]; var center = Centers.SingleOrDefault(x => x.Name == adoptionCenterName); center.StoredAnimals.Add(AnimalFactory.Create(inputData)); }
static void Main(string[] args) { List <Animal> animals = new List <Animal>(); string command; while ((command = Console.ReadLine()) != "End") { Animal animal = AnimalFactory.Create(command.Split(' ', StringSplitOptions.RemoveEmptyEntries)); animals.Add(animal); Console.WriteLine(animal.ProduceSound()); Food.Food food = FoodFactory.Create(Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries)); try { animal.EatFood(food); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } } animals.ForEach(Console.WriteLine); }
static void Main(string[] args) { List <Animal> animals = new List <Animal>(); string input; while ((input = Console.ReadLine()) != "End") { string[] animalArgs = input.Split(); Animal animal = AnimalFactory.Create(animalArgs); string[] foodArgs = Console.ReadLine().Split(); Food food = FoodFactory.Create(foodArgs); Console.WriteLine(animal.ProduceSound()); try { animal.Eat(food); } catch (Exception ex) { Console.WriteLine(ex.Message); } animals.Add(animal); } foreach (var animal in animals) { Console.WriteLine(animal); } }
static void Main() { var myAnimal = AnimalFactory.Create(AnimalType.Dog, "gatchinho", 4); var myNullAnimal = AnimalFactory.Create(AnimalType.Whale, "baleinha", 0); Console.WriteLine(myAnimal.MakeSound()); Console.WriteLine(myNullAnimal.MakeSound()); Console.ReadKey(); }
public void Run() { string command; while ((command = Console.ReadLine()) != "End") { string[] animalArgs = command.Split(); string type = animalArgs[0]; string name = animalArgs[1]; double weight = double.Parse(animalArgs[2]); string[] args = animalArgs.Skip(3).ToArray(); Animal animal = null; try { animal = animalFactory.Create(type, name, weight, args); this.animals.Add(animal); Console.WriteLine(animal.ProduceSound()); } catch (InvalidOperationException ioe) { Console.WriteLine(ioe.Message); } string[] foodArgs = Console.ReadLine().Split(); string foodType = foodArgs[0]; int foodQty = int.Parse(foodArgs[1]); try { Food food = this.foodFactory.CreateFood(foodType, foodQty); if (animal != null) { animal.Feed(food); } } catch (InvalidOperationException ioe) { Console.WriteLine(ioe.Message); } } foreach (var animal in animals) { Console.WriteLine(animal); } }
public void Run() { Animal animal = null; Food food = null; int count = 0; string input = Console.ReadLine(); while (input != "End") { var command = input.Split().ToArray(); string type = command[0]; if (count % 2 == 0) { string name = command[1]; double weight = double.Parse(command[2]); try { animal = animalFactory.Create(name, type, weight, command); Console.WriteLine(animal.ProduceSound()); animals.Add(animal); } catch (InvalidOperationException ioe) { Console.WriteLine(ioe.Message); } } else { int quantity = int.Parse(command[1]); try { food = foodFactory.CreateFood(type, quantity); animal.Feed(food); } catch (InvalidOperationException ioe) { Console.WriteLine(ioe.Message); } } count++; input = Console.ReadLine(); } PrintAnimalsInfo(); }
static void Main(string[] args) { var vet = new Vet(); var mgr = new Manager(); var zoo = new Zoo { mgr = mgr, vet = vet }; AnimalFactory <Horse> factory = new AnimalFactory <Horse>(); var horse = factory.Create("Milly", zoo); horse.Hungry(); vet.BeginVaccination(); }
private void CreateAnimalButton_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(ConsumptionTypeBox.Text) || string.IsNullOrEmpty(SizeBox.Text)) { MessageBox.Show("Je hebt nog geen gegevens ingevuld!", "Mislukt!", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } Animal animal = AnimalFactory.Create( (ConsumptionType)Enum.Parse(typeof(ConsumptionType), ConsumptionTypeBox.SelectedItem.ToString().ToUpper()), (Sizes)Enum.Parse(typeof(Sizes), SizeBox.SelectedItem.ToString().ToUpper())); train.AnimalsToAdd.Add(animal); createdAnimalsBox.Items.Add(animal.getString()); MessageBox.Show("Dier toegevoegd met de volgende attributen: " + animal.getString(), "Succes!", MessageBoxButtons.OK, MessageBoxIcon.Information); }
public void Run() { string animalInfo; while ((animalInfo = Console.ReadLine()) != "End") { string foodInfo = Console.ReadLine(); string[] animalTokens = animalInfo.Split(' ', StringSplitOptions.RemoveEmptyEntries); string[] argS = animalTokens .Skip(3).ToArray(); Animal animal = null; try { animal = animalFactory.Create(animalTokens[0], animalTokens[1], double.Parse(animalTokens[2]), argS); animals.Add(animal); Console.WriteLine(animal.ProduceSoundSForFood()); } catch (InvalidOperationException ioex) { Console.WriteLine(ioex.Message); } string[] foodTokens = foodInfo.Split(' ', StringSplitOptions.RemoveEmptyEntries); try { Food food = foodFactory.CreateFood(foodTokens[0], int.Parse(foodTokens[1])); if (animal != null) { animal.Feed(food); } } catch (InvalidOperationException ioex) { Console.WriteLine(ioex.Message); } } foreach (Animal animall in animals) { Console.WriteLine(animall); } }
public override void Run() { AnimalFactory animalFactory = new AnimalFactory(); Writer.WriteLine("Animal factory demonstration."); Writer.WriteLine(); IAnimal[] animals = new IAnimal[] { //Invoke instances with different constructors animalFactory.Create(typeof(Tomcat), "12", "Tom", "Male"), animalFactory.Create(typeof(Cat), "9", "Tom", "Male"), animalFactory.Create(typeof(Tomcat)), animalFactory.Create(typeof(Tomcat), "7", "Jerry"), animalFactory.Create(typeof(Dog), "6", "Lalo", "Female"), animalFactory.Create(typeof(Tomcat), "8", "Tom", "Male"), animalFactory.Create(typeof(Kitten), "12", "Tom"), animalFactory.Create(typeof(Frog), "12", "Fro", "Female"), animalFactory.Create(typeof(Dog)) }; Writer.WriteLine(string.Join(Environment.NewLine, (object[])animals)); }
public static void Main(string[] args) { int index = 0; var animalType = ""; List <Animal> animals = new List <Animal>(); while (true) { var input = Console.ReadLine(); if (input == "End") { break; } var splittedInput = input.Split(); if (index % 2 == 0) { var currentAnimal = AnimalFactory.Create(splittedInput); animals.Add(currentAnimal); animalType = currentAnimal.GetType().Name; } else { try { var currentFood = FoodFactory.Create(splittedInput); var currentAnimal = animals[animals.Count - 1]; Console.WriteLine(currentAnimal.SayHello()); currentAnimal.Eat(currentFood); } catch (ArgumentException ae) { Console.WriteLine(ae.Message); } } index++; } foreach (var animal in animals) { Console.WriteLine(animal.ToString()); } }
public static void Run() { List <Animal> animals = new List <Animal>(); var dog = new Dog("Beto", 25, "John House"); Animal animal = AnimalFactory.Create(dog); Fruit fruit = new Fruit(3); Food food = FoodFactory.Create(fruit); try { animal.Eat(food); } catch (Exception ex) { Console.WriteLine(ex.Message); } animals.Add(animal); Tiger tiger = new Tiger("King", 650, "Forest", "Big Cat"); animal = AnimalFactory.Create(tiger); Meat meat = new Meat(30); food = FoodFactory.Create(meat); try { animal.Eat(food); } catch (Exception ex) { Console.WriteLine(ex.Message); } animals.Add(animal); foreach (var item in animals) { Console.WriteLine(item); Console.WriteLine("Sound: " + item.ProduceSound()); Console.WriteLine("=============================================="); } }
public static void Main(string[] args) { List <Animal> animals = new List <Animal>(); while (true) { string command = Console.ReadLine(); if (command == "End") { break; } string[] animalArgs = command.Split(); var animal = AnimalFactory.Create(animalArgs); animals.Add(animal); string[] foodArgs = Console.ReadLine().Split(); var food = FoodFactory.Create(foodArgs); Console.WriteLine(animal.AskFood()); try { animal.Eat(food); } catch (Exception ex) { Console.WriteLine(ex.Message); } } foreach (var animal in animals) { Console.WriteLine(animal); } }
public void Run() { List <IAnimal> animals = new List <IAnimal>(); string input = string.Empty; while ((input = Console.ReadLine()) != "End") { string[] animalElements = input .Split(" ", StringSplitOptions.RemoveEmptyEntries) .ToArray(); IAnimal animal = AnimalFactory.Create(animalElements); animals.Add(animal); Console.WriteLine(animal.ProduceSound()); string[] foodElements = Console.ReadLine() .Split(" ", StringSplitOptions.RemoveEmptyEntries) .ToArray(); IFood food = FoodFactory.Create(foodElements); try { animal.Eat(food); } catch (Exception ex) { Console.WriteLine(ex.Message); } } foreach (IAnimal animal in animals) { Console.WriteLine(animal); } }
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); } }
static void Main(string[] args) { var databasePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "ZooSQlite.db"); var db = new SQLiteConnection(databasePath); db.CreateTable <Cat>(); //db.CreateTable<Valuation>(); Console.WriteLine("Создаем котиков..."); List <Cat> cats = new List <Cat>() { new Cat() { Nickname = "Мурка", Gender = GenderEnum.Female }, new Cat() { Nickname = "Мурзик", Gender = GenderEnum.Male } }; cats.Add(new Cat() { Nickname = "Пушок", Gender = GenderEnum.Male }); cats.Add(new Cat() { Nickname = "Бусинка", Gender = GenderEnum.Female }); cats.ForEach(cat => Console.WriteLine(cat)); List <IAnimal> vipcats = (from cat in cats where cat.Gender == GenderEnum.Female && cat.Nickname.Contains('М') select cat).ToList <IAnimal>(); XmlSerializer xml = new XmlSerializer(cats.GetType()); FileStream f = new FileStream("Cats.xml", FileMode.Create, FileAccess.Write, FileShare.Read); Console.WriteLine("Сохраняем котиков..."); xml.Serialize(f, cats); f.Close(); f = new FileStream("Cats.xml", FileMode.Open, FileAccess.Read, FileShare.Read); Console.WriteLine("Загружаем котиков..."); List <Cat> catsRead = xml.Deserialize(f) as List <Cat>; AnimalFactory <Cat> factoryCats = new AnimalFactory <Cat>(new Selector <Cat>(new Population <Cat>(catsRead))); catsRead.Add(factoryCats.Create()); catsRead.ForEach(cat => Console.WriteLine(cat)); Console.WriteLine("Создаем собачек..."); List <Dog> dogs = new List <Dog>() { new Dog() { Nickname = "Дружок", Gender = GenderEnum.Female }, new Dog() { Nickname = "Шарик", Gender = GenderEnum.Male } }; dogs.Add(new Dog() { Nickname = "Тритон", Gender = GenderEnum.Male }); dogs.Add(new Dog() { Nickname = "Гена", Gender = GenderEnum.Female }); //dogs.ForEach(dog => Console.WriteLine(dog)); foreach (Dog dog in dogs) { Console.WriteLine($"{ dog }"); dog.Song(); //zooContents.Add((dog as IZooContent)); } f = new FileStream("Dogs.xml", FileMode.Create, FileAccess.Write, FileShare.Read); Console.WriteLine("Сохраняем собачек..."); xml.Serialize(f, dogs); f.Close(); f = new FileStream("Dogs.xml", FileMode.Open, FileAccess.Read, FileShare.Read); Console.WriteLine("Загружаем собачек..."); List <Dog> dogsRead = xml.Deserialize(f) as List <Dog>; Console.ReadLine(); }