Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cage.cs:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Rabbits
- {
- public class Cage
- {
- private List<Rabbit> data;
- public Cage(string name, int capacity)
- {
- this.Name = name;
- this.Capacity = capacity;
- this.data = new List<Rabbit>();
- }
- public string Name { get; private set; }
- public int Capacity { get; private set; }
- public int Count => this.data.Count();//getter Count
- //adds an entity to the data if ima mqsto//////
- public void Add(Rabbit rabbit)
- {
- if (this.data.Count < this.Capacity)
- {
- data.Add(rabbit);
- }
- }
- //removes a rabbit by given name, if such exists, and returns bool///
- public bool RemoveRabbit(string name)
- {
- return this.data.Remove(this.data.FirstOrDefault(x => x.Name == name));
- }
- //removes all rabbits by given species//
- public void RemoveSpecies(string species)
- {
- this.data.RemoveAll(x => x.Species == species);
- }
- //sell (set its Available property to false without removing it from the collection) the first rabbit with the given name, also return the rabbit///
- public Rabbit SellRabbit(string name)//pyrviq zaek s dadeno ime
- {
- Rabbit sell = this.data.FirstOrDefault(x => x.Name == name);
- sell.Available = false;
- return sell;
- }
- //sells (set their Available property to false without removing them from the collection) and returns all rabbits from that species as an array///
- public Rabbit[] SellRabbitsBySpecies(string species)
- {
- List<Rabbit> rabbits = new List<Rabbit>();
- rabbits = this.data.Where(x => x.Species == species).ToList();
- for (int i = 0; i < rabbits.Count; i++)
- {
- rabbits[i].Available = false;
- }
- return rabbits.ToArray();
- }
- ///returns a string in the following format, including only not sold rabbits//vkl samo neprodadeni zaici
- public string Report()
- {
- StringBuilder sb = new StringBuilder();
- sb.AppendLine($"Rabbits available at {this.Name}:");
- foreach (var rab in this.data)
- {
- if (rab.Available == true)
- {
- sb.AppendLine($"{rab}");
- }
- }
- return sb.ToString().TrimEnd();
- }
- }
- }
- ------------------------------------------------------------------------------
- rabbit.cs:
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Rabbits
- {
- public class Rabbit
- {
- public Rabbit(string name, string species)
- {
- this.Name = name;
- this.Species = species;
- this.Available = true;
- }
- public string Name { get; private set; }
- public string Species { get; private set; }
- public bool Available { get; set; }
- public override string ToString()
- {
- return $"Rabbit ({ this.Species}): {this.Name}";
- }
- }
- }
- ----------------------------------------------------------------------------------------------
- startUp.cs:
- namespace Rabbits
- {
- using System;
- using System.Linq;
- public class StartUp
- {
- public static void Main()
- {
- //Initialize the repository (Cage)
- Cage cage = new Cage("Wildness", 20);
- //Initialize entity
- Rabbit rabbit = new Rabbit("Fluffy", "Blanc de Hotot");
- //Print Rabbit
- Console.WriteLine(rabbit); //Rabbit (Blanc de Hotot): Fluffy
- //Add Rabbit
- cage.Add(rabbit);
- Console.WriteLine(cage.Count); //1
- //Remove Rabbit
- cage.RemoveRabbit("Rabbit Name"); //false
- Rabbit secondRabbit = new Rabbit("Bunny", "Brazilian");
- Rabbit thirdRabbit = new Rabbit("Jumpy", "Cashmere Lop");
- Rabbit fourthRabbit = new Rabbit("Puffy", "Cashmere Lop");
- Rabbit fifthRabbit = new Rabbit("Marlin", "Brazilian");
- //Add Rabbits
- cage.Add(secondRabbit);
- cage.Add(thirdRabbit);
- cage.Add(fourthRabbit);
- cage.Add(fifthRabbit);
- //Sell Rabbit by name
- Console.WriteLine(cage.SellRabbit("Bunny")); //Rabbit (Brazilian): Bunny
- //Sell Rabbit by species
- Rabbit[] soldSpecies = cage.SellRabbitsBySpecies("Cashmere Lop");
- Console.WriteLine(string.Join(", ", soldSpecies.Select(f => f.Name))); //Jumpy, Puffy
- Console.WriteLine(cage.Report());
- //Rabbits available at Wildness:
- //Rabbit (Blanc de Hotot): Fluffy
- //Rabbit (Brazilian): Marlin
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement