SHOW:
|
|
- or go back to the newest paste.
1 | using System; | |
2 | using System.Collections.Generic; | |
3 | - | namespace C_sharp_Light |
3 | + | |
4 | namespace СLightt | |
5 | { | |
6 | class Program | |
7 | - | static void Main(string[] args) |
7 | + | |
8 | public static void Main() | |
9 | - | |
9 | + | |
10 | new Aquarium(10).Work(); | |
11 | } | |
12 | } | |
13 | public static class RandomStatic | |
14 | { | |
15 | static private Random _rand = new Random(); | |
16 | static public int GetNext(int min, int max) | |
17 | { | |
18 | return _rand.Next(min, max); | |
19 | } | |
20 | } | |
21 | public static class Messager | |
22 | { | |
23 | static public void ShowMessageWithColor(string message, ConsoleColor color, bool delay) | |
24 | { | |
25 | ConsoleColor defaultColor = Console.ForegroundColor; | |
26 | Console.ForegroundColor = color; | |
27 | Console.WriteLine(message); | |
28 | Console.ForegroundColor = defaultColor; | |
29 | if (delay) | |
30 | Console.ReadKey(); | |
31 | } | |
32 | } | |
33 | ||
34 | class Aquarium | |
35 | { | |
36 | private List<Fish> _fishs = new List<Fish>(); | |
37 | private int _maxFish; | |
38 | public Aquarium(int maxFish) | |
39 | { | |
40 | _maxFish = maxFish; | |
41 | if (_maxFish <= 0) | |
42 | _maxFish = 1; | |
43 | } | |
44 | public void Work() | |
45 | { | |
46 | bool isOpen = true; | |
47 | while (isOpen) | |
48 | { | |
49 | bool isMenu = true; | |
50 | while (isMenu) | |
51 | { | |
52 | Console.Clear(); | |
53 | ShowInfo(); | |
54 | Console.WriteLine("\n1 - Добавить рыбу \n2 - Убрать рыбу \nЛюбая клавиша - Прожить время"); | |
55 | switch (Console.ReadKey(true).Key) | |
56 | { | |
57 | case ConsoleKey.D1: | |
58 | AddFish(); | |
59 | break; | |
60 | case ConsoleKey.D2: | |
61 | RemoveFish(); | |
62 | break; | |
63 | default: | |
64 | isMenu = false; | |
65 | break; | |
66 | } | |
67 | } | |
68 | LiveFishs(); | |
69 | } | |
70 | } | |
71 | private void ShowInfo() | |
72 | { | |
73 | Messager.ShowMessageWithColor($"Аквариум содержит {_fishs.Count} рыб из {_maxFish} возможных", ConsoleColor.White, false); | |
74 | int count = 0; | |
75 | foreach (Fish fish in _fishs) | |
76 | { | |
77 | ConsoleColor color; | |
78 | Messager.ShowMessageWithColor($"{count}: {fish.GetInfo(out color)}", color, false); | |
79 | count++; | |
80 | } | |
81 | Console.WriteLine(); | |
82 | } | |
83 | private void AddFish() | |
84 | { | |
85 | if (_fishs.Count < _maxFish) | |
86 | { | |
87 | _fishs.Add(new Fish()); | |
88 | Messager.ShowMessageWithColor("Новая рыба добавлена", ConsoleColor.Green, true); | |
89 | } | |
90 | else | |
91 | { | |
92 | Messager.ShowMessageWithColor("ОШИБКА: Аквариум полностью заполнен", ConsoleColor.Red, true); | |
93 | } | |
94 | } | |
95 | private void RemoveFish() | |
96 | { | |
97 | if (_fishs.Count > 0) | |
98 | { | |
99 | Console.Write("Введите индекс рыбы для удаления - "); | |
100 | int indexForRemove = Convert.ToInt32(Console.ReadLine()); | |
101 | _fishs.RemoveAt(indexForRemove); | |
102 | } | |
103 | else | |
104 | { | |
105 | Messager.ShowMessageWithColor("ОШИБКА: Рыб нет", ConsoleColor.Red, true); | |
106 | } | |
107 | } | |
108 | private void LiveFishs() | |
109 | { | |
110 | Messager.ShowMessageWithColor("Рыбы прожили год в аквариуме", ConsoleColor.White, true); | |
111 | foreach (Fish fish in _fishs) | |
112 | { | |
113 | fish.Live(); | |
114 | } | |
115 | } | |
116 | } | |
117 | class Fish | |
118 | { | |
119 | public int Age { get; private set; } | |
120 | private bool _isAlive; | |
121 | private int _maxAge; | |
122 | public Fish() | |
123 | { | |
124 | Age = 0; | |
125 | _isAlive = true; | |
126 | _maxAge = RandomStatic.GetNext(3, 20); | |
127 | } | |
128 | public void Live() | |
129 | { | |
130 | CheckedAlive(); | |
131 | if (_isAlive) | |
132 | Age++; | |
133 | } | |
134 | public string GetInfo(out ConsoleColor color) | |
135 | { | |
136 | if (_isAlive) | |
137 | { | |
138 | color = ConsoleColor.Blue; | |
139 | return $"Рыбе - {Age} лет. Она Жива."; | |
140 | } | |
141 | else | |
142 | { | |
143 | color = ConsoleColor.Magenta; | |
144 | return $"Рыбе - {Age} лет. Она Мертва."; | |
145 | } | |
146 | } | |
147 | private void CheckedAlive() | |
148 | { | |
149 | if (Age >= _maxAge) | |
150 | if (Convert.ToBoolean(RandomStatic.GetNext(0, 2))) | |
151 | _isAlive = false; | |
152 | } | |
153 | } | |
154 | } |