Advertisement
markovood

Untitled

Jun 19th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Rabbits
  7. {
  8.     public class Cage
  9.     {
  10.         private List<Rabbit> data;
  11.  
  12.         public Cage(string name, int capacity)
  13.         {
  14.             this.Name = name;
  15.             this.Capacity = capacity;
  16.  
  17.             this.data = new List<Rabbit>(this.Capacity);
  18.         }
  19.  
  20.         public string Name { get; private set; }
  21.         public int Capacity { get; private set; }
  22.         public int Count => this.data.Count;
  23.  
  24.         public void Add(Rabbit rabbit)
  25.         {
  26.             if (this.Count + 1 <= this.Capacity)
  27.             {
  28.                 this.data.Add(rabbit);
  29.             }
  30.         }
  31.  
  32.         public bool RemoveRabbit(string name)
  33.         {
  34.             int removed = this.data.RemoveAll(r => r.Name == name);
  35.             if (removed > 0)
  36.             {
  37.                 return true;
  38.             }
  39.  
  40.             return false;
  41.         }
  42.  
  43.         public void RemoveSpecies(string species)
  44.         {
  45.             this.data.RemoveAll(r => r.Species == species);
  46.         }
  47.  
  48.         public Rabbit SellRabbit(string name)
  49.         {
  50.             var rabbitToSell = this.data.FirstOrDefault(r => r.Name == name);
  51.             rabbitToSell.Available = false;
  52.             return rabbitToSell;
  53.         }
  54.  
  55.         public Rabbit[] SellRabbitsBySpecies(string species)
  56.         {
  57.             var filtered = this.data
  58.                 .Where(r => r.Species == species)
  59.                 .ToArray();
  60.  
  61.             foreach (var rabbit in filtered)
  62.             {
  63.                 rabbit.Available = false;
  64.             }
  65.  
  66.             return filtered;
  67.         }
  68.  
  69.         public string Report()
  70.         {
  71.             StringBuilder sb = new StringBuilder();
  72.             sb.AppendLine($"Rabbits available at {this.Name}:");
  73.  
  74.             foreach (var rabbit in this.data)
  75.             {
  76.                 if (rabbit.Available)
  77.                 {
  78.                     sb.AppendLine(rabbit.ToString());
  79.                 }
  80.             }
  81.  
  82.             return sb.ToString().Trim();
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement