Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace _08.PetClinics.Models
  8. {
  9.     public class Clinic : IEnumerable<Pet>
  10.     {
  11.         private int roomsCount;
  12.         private Pet[] pets;
  13.  
  14.         public Clinic(string name, int roomsCount)
  15.         {
  16.             this.RoomsCount = roomsCount;
  17.             this.Name = name;
  18.             this.pets = new Pet[roomsCount];
  19.         }
  20.  
  21.         public string Name { get; set; }
  22.         public int RoomsCount
  23.         {
  24.             get { return this.roomsCount; }
  25.             set
  26.             {
  27.                 if (value % 2 != 1)
  28.                 {
  29.                     throw new ArgumentException("Invalid Operation!");
  30.                 }
  31.  
  32.                 this.roomsCount = value;
  33.             }
  34.         }
  35.  
  36.         public bool AddPet(Pet pet)
  37.         {
  38.             int middleRoomIndex = this.pets.Length / 2;
  39.             if (pets[middleRoomIndex] == null)
  40.             {
  41.                 pets[middleRoomIndex] = pet;
  42.                 return true;
  43.             }
  44.  
  45.             int count = 1;
  46.             while (this.pets.Any(x => x == null))
  47.             {
  48.  
  49.                 if (this.pets[middleRoomIndex - count] == null)
  50.                 {
  51.                     this.pets[middleRoomIndex - count] = pet;
  52.                     return true;
  53.                 }
  54.                 if (this.pets[middleRoomIndex + count] == null)
  55.                 {
  56.                     this.pets[middleRoomIndex + count] = pet;
  57.                     return true;
  58.                 }
  59.  
  60.                 count++;
  61.             }
  62.  
  63.             return false;
  64.         }
  65.  
  66.         public bool ReleasePet()
  67.         {
  68.             if (this.pets.All(x => x == null))
  69.             {
  70.                 return false;
  71.             }
  72.  
  73.             int middleRoomIndex = this.pets.Length / 2;
  74.             for (int i = middleRoomIndex; i < this.pets.Length; i++)
  75.             {
  76.                 if (this.pets[i] != null)
  77.                 {
  78.                     this.pets[i] = null;
  79.                     return true;
  80.                 }
  81.             }
  82.  
  83.             for (int i = 0; i < middleRoomIndex; i++)
  84.             {
  85.                 if (this.pets[i] != null)
  86.                 {
  87.                     this.pets[i] = null;
  88.                     return true;
  89.                 }
  90.             }
  91.  
  92.             return false;
  93.         }
  94.  
  95.         public bool HasEmptyRooms()
  96.         {
  97.             if (this.pets.Any(x => x == null))
  98.             {
  99.                 return true;
  100.             }
  101.  
  102.             return false;
  103.         }
  104.  
  105.         public string Print()
  106.         {
  107.             var sb = new StringBuilder();
  108.  
  109.             foreach (var pet in this.pets)
  110.             {
  111.                 if (pet == null)
  112.                 {
  113.                     sb.AppendLine("Room empty");
  114.                     continue;
  115.                 }
  116.                 sb.AppendLine(pet.ToString());
  117.             }
  118.  
  119.             return sb.ToString().TrimEnd();
  120.         }
  121.  
  122.         public string Print(int room)
  123.         {
  124.             int roomIndex = room - 1;
  125.             if (roomIndex >= this.pets.Length || roomIndex < 0)
  126.             {
  127.                 throw new ArgumentException("Invalid Operation!");
  128.             }
  129.  
  130.             return this.pets[roomIndex].ToString();
  131.         }
  132.  
  133.         public IEnumerator<Pet> GetEnumerator()
  134.         {
  135.             for (int i = 0; i < this.pets.Length; i++)
  136.             {
  137.                 yield return this.pets[i];
  138.             }
  139.         }
  140.  
  141.         IEnumerator IEnumerable.GetEnumerator()
  142.         {
  143.             return GetEnumerator();
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement