Advertisement
OldBeliver

NewYearParty_02

Apr 23rd, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.58 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 NewYearParty_02
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Santa santa = new Santa(new List<Gift>());
  14.             Boy boy = new Boy(new List<Gift>());
  15.  
  16.             bool isOpen = true;
  17.             string userInput;
  18.  
  19.             while (isOpen)
  20.             {
  21.                 Console.WriteLine($"Новогодний детский утренник\n");
  22.                 Console.WriteLine($"1 - Заглянуть в мешок Санты\n2 - Клянчить подарок\n3 - Посмотреть в свой карман\n4 - Уйти с ёлки");
  23.                 Console.Write($"\nВведите номер команды: ");
  24.                 userInput = Console.ReadLine();
  25.  
  26.                 switch (userInput)
  27.                 {
  28.                     case "1":
  29.                         Console.WriteLine($"\nпока Санта отвернулся Вы украдкой заглянули в его мешок...");
  30.                         Console.WriteLine($"надпись на отвороте мешка гласила: спонсор подарков настольная карточная игра \"Манчкин\"\n");
  31.                         santa.ShowInfo();
  32.                         break;
  33.                     case "2":
  34.                         Console.WriteLine($"Вы промямлили стишок у ёлки про Деда Мороза, какую-то бороду из ваты и горбатого человека,");
  35.                         Console.Write($"после чего с надеждой смотрите на подарок номер: ");
  36.                         if (santa.GiveGift(out Gift present))
  37.                         {
  38.                             boy.TakeGift(present);
  39.                         }
  40.                         else
  41.                         {
  42.                             Console.WriteLine($"ошибка номера подарка");
  43.                         }
  44.                         break;
  45.                     case "3":
  46.                         boy.ShowInfo();
  47.                         break;
  48.                     case "4":
  49.                         isOpen = false;
  50.                         break;
  51.                     default:
  52.                         Console.WriteLine($"НЕверная команда");
  53.                         break;
  54.                 }
  55.                 Console.WriteLine($"\nлюбую для продолжения ...");
  56.                 Console.ReadKey();
  57.                 Console.Clear();
  58.             }
  59.         }
  60.     }
  61.  
  62.     class Actor
  63.     {
  64.         protected List<Gift> Gifts;
  65.  
  66.         public Actor(List<Gift> gifts)
  67.         {
  68.             Gifts = gifts;
  69.         }
  70.  
  71.         public void ShowInfo()
  72.         {
  73.             for (int i = 0; i < Gifts.Count; i++)
  74.             {
  75.                 Console.Write($"{i + 1}. ");
  76.                 Gifts[i].ShowDescription();
  77.             }
  78.  
  79.             if (Gifts.Count == 0)
  80.             {
  81.                 Console.WriteLine($"\n здесь пусто");
  82.             }
  83.         }
  84.  
  85.         public bool TryGetGiftIndex(out int index)
  86.         {
  87.             int number;
  88.             index = 0;
  89.  
  90.             if (TryReadInt(out number))
  91.             {
  92.                 index = number;
  93.             }
  94.  
  95.             bool isLegalRange = (index > 0 && index <= Gifts.Count);
  96.  
  97.             return isLegalRange;
  98.         }
  99.  
  100.         private bool TryReadInt(out int number)
  101.         {
  102.             bool result = int.TryParse(Console.ReadLine(), out number);
  103.  
  104.             return result;
  105.         }
  106.     }
  107.  
  108.     class Santa : Actor
  109.     {
  110.         public Santa(List<Gift> gifts) : base(gifts)
  111.         {
  112.             gifts.Add(new Gift("Шлем бесстрашия"));
  113.             gifts.Add(new Gift("Башмаки могучего Пенделя"));
  114.             gifts.Add(new Gift("Читерский кубик"));
  115.             gifts.Add(new Gift("Боевая стремянка"));
  116.             gifts.Add(new Gift("Зелье ротовой вони"));
  117.             gifts.Add(new Gift("Зелье идиотской храбрости"));
  118.             gifts.Add(new Gift("Меч песни и пляски"));
  119.             gifts.Add(new Gift("Рапира такнечестности"));
  120.             gifts.Add(new Gift("Паленые доспехи"));
  121.             gifts.Add(new Gift("Паленые доспехи"));
  122.         }
  123.  
  124.         public bool GiveGift(out Gift present)
  125.         {
  126.             bool result = false;
  127.             present = null;
  128.  
  129.             if (TryGetGiftIndex(out int index))
  130.             {
  131.                 present = Gifts[index - 1];
  132.                 result = true;
  133.                 Gifts.RemoveAt(index - 1);
  134.             }
  135.  
  136.             return result;
  137.         }
  138.     }
  139.  
  140.     class Boy : Actor
  141.     {
  142.         public Boy(List<Gift> gifts) : base(gifts)
  143.         {
  144.             gifts.Add(new Gift("Чистый носовой платочек"));
  145.             gifts.Add(new Gift("Жук в коробочке"));
  146.         }
  147.  
  148.         public void TakeGift(Gift present)
  149.         {
  150.             Gifts.Add(new Gift(present.Name));
  151.             Console.WriteLine($"Вы получили подарок - {present.Name}");
  152.         }
  153.     }
  154.  
  155.     class Gift
  156.     {
  157.         public string Name { get; private set; }
  158.  
  159.         public Gift(string name)
  160.         {
  161.             Name = name;
  162.         }
  163.  
  164.         public void ShowDescription()
  165.         {
  166.             Console.WriteLine($"{Name}");
  167.         }
  168.     }
  169. }
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement