Advertisement
Stray_Wolf

OOP_Aquarium

Aug 10th, 2022
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace HW_43_OOP_Aquarium
  8. {
  9.     internal class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {  
  13.             Aquarium aquarium = new Aquarium();
  14.  
  15.             while (true)
  16.             {
  17.                 Console.WriteLine("Аквариум");
  18.                 Console.WriteLine($"Максимум рыбок - {aquarium.MaxFishCount} / {aquarium.CurrentFishCount}\n");
  19.                 ShowMenu();
  20.  
  21.                 string userInput;
  22.                 userInput = Console.ReadLine();
  23.  
  24.                 switch (userInput)
  25.                 {
  26.                     case "1":
  27.                         if (aquarium.CurrentFishCount < aquarium.MaxFishCount)
  28.                         {
  29.                             Console.Write("Введите название рыбки: ");
  30.                             string fishToAdd = Console.ReadLine();
  31.                             Console.Write("Введите возраст рыбки, если неизвестно, по умолчанию возраст с 0: ");
  32.                             int fishAge = Convert.ToInt32(Console.ReadLine());
  33.                             aquarium.AddFish(fishToAdd, fishAge);
  34.                         }
  35.                         else Console.WriteLine("В аквариуме уже нет места, уберите какую нибудь рыбку или оставьте так");
  36.                         break;
  37.                     case "2":
  38.                         Console.Write("Введите название рыбки ");
  39.                         string fishToRemove = Console.ReadLine();
  40.                         aquarium.RemoveFish(fishToRemove);
  41.                         break;
  42.                     case "3":
  43.                         aquarium.ShowFishInfo();
  44.                         break;
  45.                     default:
  46.                         break;
  47.                 }
  48.  
  49.                 aquarium.MakeDayPass();
  50.                 aquarium.RemoveFish();
  51.                 Console.ReadKey();
  52.                 Console.Clear();
  53.             }
  54.         }
  55.  
  56.         public static void ShowMenu()
  57.         {
  58.             Console.WriteLine("1 - Добавить рыбку в аквариум\n2 - Достать рыбку из аквариума \n3 - Показать аквариум");
  59.         }
  60.     }
  61.  
  62.     class Aquarium
  63.     {
  64.         private List<Fish> _fishList = new List<Fish>();
  65.  
  66.         private int _maxFishCount = 3;
  67.         private int _currentFishCount;
  68.  
  69.         public int MaxFishCount { get { return _maxFishCount; } }
  70.         public int CurrentFishCount
  71.         {
  72.             get { return _currentFishCount; }
  73.             set { _currentFishCount = value; }
  74.         }
  75.  
  76.         public List<Fish> FishList
  77.         {
  78.             get { return _fishList; }
  79.             set { _fishList = value; }
  80.         }
  81.  
  82.         public void AddFish(string name, int age = 0)
  83.         {
  84.             _fishList.Add(new Fish(name, age));
  85.             CurrentFishCount++;
  86.             Console.WriteLine("Рыбка " + name + ". Добавлена");
  87.         }
  88.  
  89.         public void RemoveFish(string name)
  90.         {
  91.             _fishList.RemoveAll(x => x.Specie == name);
  92.         }
  93.  
  94.         public void RemoveFish()
  95.         {
  96.             _fishList.RemoveAll(x => x.Age > x.MaxAge);
  97.         }
  98.  
  99.         public void ShowFishInfo()
  100.         {
  101.             foreach (var fish in _fishList)
  102.             {
  103.                 Console.WriteLine($"Рыбка {fish.Specie}, возраст {fish.Age}");
  104.             }
  105.         }
  106.  
  107.         public void MakeDayPass()
  108.         {
  109.             foreach (var fish in _fishList)
  110.             {
  111.                 fish.Age++;
  112.             }
  113.         }
  114.     }
  115.  
  116.     class Fish
  117.     {
  118.         private string _specie;
  119.         private int _age;
  120.         private int _maxAge = 5;
  121.  
  122.         public Fish(string specie, int age)
  123.         {
  124.             _specie = specie;
  125.             _age = age;
  126.         }
  127.  
  128.         public string Specie
  129.         {
  130.             get { return _specie; }
  131.             set { _specie = value; }
  132.         }
  133.  
  134.         public int Age
  135.         {
  136.             get { return _age; }
  137.             set { _age = value; }
  138.         }
  139.  
  140.         public int MaxAge { get { return _maxAge; } }
  141.     }
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement