OldBeliver

SantaBag_ver01

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