RedFlys

Home work - trader

Nov 15th, 2021 (edited)
891
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.74 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 Home_Work
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Player player = new Player(200);
  14.             Trader seller = new Trader(5000);
  15.             string userInput;
  16.             bool isWorking = true;
  17.  
  18.             while (isWorking)
  19.             {
  20.                 Console.WriteLine("Приветсвуем вас в торговом доме Виндсома.\n" +
  21.                   "1. Посмотреть свой инвентарь.\n" +
  22.                   "2. Посмотреть товары продовца.\n" +
  23.                   "3. Выйти.");
  24.                 userInput = Console.ReadLine();
  25.                 Console.Clear();
  26.  
  27.                 switch (userInput)
  28.                 {
  29.                     case "1":
  30.                         player.ShowInventory();
  31.                         Console.ReadKey();
  32.                         break;
  33.  
  34.                     case "2":
  35.                         seller.OfferToSell(player);
  36.                         break;
  37.  
  38.                     case "3":
  39.                         isWorking = false;
  40.                         break;
  41.                 }
  42.  
  43.                 Console.Clear();
  44.             }
  45.         }
  46.     }
  47.  
  48.     class Person
  49.     {
  50.         protected Inventory Inventory;
  51.         protected int Money;
  52.  
  53.         public Person(int money = 0)
  54.         {
  55.             Inventory = new Inventory();
  56.             Money = money;
  57.         }
  58.  
  59.         public void ShowInventory()
  60.         {
  61.             Console.WriteLine($"Золота в наличии {Money}.");
  62.             Inventory.ShowAll();
  63.         }
  64.     }
  65.  
  66.     class Player : Person
  67.     {
  68.         public Player(int money) : base(money)
  69.         {
  70.             Inventory.AddProduct(new Product("Тренировочный одноручный меч", 20), 1);
  71.             Inventory.AddProduct(new Product("Тренировочный щит", 15), 1);
  72.             Inventory.AddProduct(new Product("Форма рыцаря - новичка", 40), 1);
  73.             Inventory.AddProduct(new Product("Бинт", 4), 2);
  74.             Inventory.AddProduct(new Product("Железная сабля стражи", 50), 1);
  75.         }
  76.  
  77.         public void Buy(Product product, int quantity)
  78.         {
  79.             Inventory.AddProduct(product, quantity);
  80.             Money -= product.Price * quantity;
  81.         }
  82.  
  83.         public bool IsSolvent(int purchase)
  84.         {
  85.             return Money >= purchase;
  86.         }
  87.     }
  88.  
  89.     class Trader : Person
  90.     {
  91.         public Trader(int money) : base(money)
  92.         {
  93.             Inventory.AddProduct(new Product("Меч короля Лича", 2000), 1);
  94.             Inventory.AddProduct(new Product("Одноручный железный клинок рыцарей серебрянного авангарда", 150), 2);
  95.             Inventory.AddProduct(new Product("Щит солнечного колодца", 200), 2);
  96.             Inventory.AddProduct(new Product("Одноручный меч воина клана Ястреба", 70), 10);
  97.             Inventory.AddProduct(new Product("Двухручный меч Кромсатель Драконов", 2000), 1);
  98.             Inventory.AddProduct(new Product("Броня Берсерка", 1500), 1);
  99.             Inventory.AddProduct(new Product("Железный щит стражи", 40), 20);
  100.             Inventory.AddProduct(new Product("Железная сабля стражи", 50), 20);
  101.         }
  102.  
  103.         public void OfferToSell(Player player)
  104.         {
  105.             ConsoleKeyInfo userKey;
  106.             bool isShowing = true;
  107.  
  108.             while (isShowing)
  109.             {
  110.                 ShowInventory();
  111.  
  112.                 Console.WriteLine("\n1. Если хотите что-то купить.\n" +
  113.                     "2. Выйти.");
  114.                 userKey = Console.ReadKey(true);
  115.  
  116.                 switch (userKey.Key)
  117.                 {
  118.                     case ConsoleKey.D1:
  119.                         TrySell(player);
  120.                         break;
  121.  
  122.                     case ConsoleKey.D2:
  123.                         isShowing = false;
  124.                         break;
  125.  
  126.                     default:
  127.                         Console.WriteLine("Такой команды нет.");
  128.                         Console.ReadKey();
  129.                         break;
  130.                 }
  131.  
  132.                 Console.Clear();
  133.             }
  134.         }
  135.  
  136.         private void TrySell(Player player)
  137.         {
  138.             Product product;
  139.             int quantity;
  140.  
  141.             product = Inventory.GetProduct();
  142.             quantity = Inventory.GetQuantity(product.Name);
  143.  
  144.             if (player.IsSolvent(product.Price * quantity))
  145.             {
  146.                 player.Buy(product, quantity);
  147.                 Sell(product, quantity);
  148.  
  149.                 Console.WriteLine("Сделка совершена.");
  150.             }
  151.             else
  152.             {
  153.                 Console.WriteLine("Нужно больше золота.");
  154.             }
  155.  
  156.             Console.ReadKey();
  157.         }
  158.  
  159.         private void Sell(Product product, int quantity)
  160.         {
  161.             Inventory.ReduceProduct(product, quantity);
  162.             Money += product.Price * quantity;
  163.         }
  164.     }
  165.  
  166.     class Inventory
  167.     {
  168.         private Dictionary<string, StackOfProduct> _products;
  169.  
  170.         public Inventory()
  171.         {
  172.             _products = new Dictionary<string, StackOfProduct>();
  173.         }
  174.  
  175.         public int StacksCount => _products.Count();
  176.  
  177.         public void AddProduct(Product product, int quantity)
  178.         {
  179.             if (_products.ContainsKey(product.Name))
  180.             {
  181.                 _products[product.Name].AddQuantity(quantity);
  182.             }
  183.             else
  184.             {
  185.                 _products.Add(product.Name, new StackOfProduct(product, quantity));
  186.             }
  187.         }
  188.  
  189.         public void ShowAll()
  190.         {
  191.             int index = 1;
  192.  
  193.             Console.WriteLine($"В наличии: ");
  194.  
  195.             foreach (var product in _products)
  196.             {
  197.                 Console.Write($"\n№{index}. ");
  198.                 product.Value.ShowInfo();
  199.  
  200.                 index++;
  201.             }
  202.         }
  203.  
  204.         public void ReduceProduct(Product product, int quantity)
  205.         {  
  206.             _products[product.Name].ReduceAmount(quantity);          
  207.  
  208.             if (_products[product.Name].IsEmpty)
  209.             {
  210.                 _products.Remove(product.Name);
  211.             }
  212.         }
  213.  
  214.         public Product GetProduct()
  215.         {
  216.             Product product = null;
  217.             bool isGetIndex = false;
  218.  
  219.             int index;
  220.             string userInput;
  221.  
  222.             while (isGetIndex == false)
  223.             {
  224.                 Console.Write("Введите индекс товара: ");
  225.                 userInput = Console.ReadLine();
  226.  
  227.                 if (Int32.TryParse(userInput, out index) && index > 0 && index <= StacksCount)
  228.                 {
  229.                     string productName = _products.ElementAt(index - 1).Value.Name;
  230.                     int productPrice = _products.ElementAt(index - 1).Value.PriceForOne;
  231.  
  232.                     product = new (productName, productPrice);
  233.                     isGetIndex = true;
  234.                 }
  235.                 else
  236.                 {
  237.                     Console.WriteLine("Такого индекса нет.");
  238.                 }
  239.             }
  240.  
  241.             return product;
  242.         }
  243.  
  244.         public int GetQuantity(string productName)
  245.         {
  246.             bool isGetQuaintity = false;
  247.             int quantity = 0;
  248.  
  249.             string userInput;
  250.  
  251.             while (isGetQuaintity == false)
  252.             {
  253.                 Console.Write("Введите кол-во товара: ");
  254.                 userInput = Console.ReadLine();
  255.  
  256.                 if (Int32.TryParse(userInput, out quantity))
  257.                 {
  258.                     if (quantity >= 0 && _products[productName].HaveQuanity(quantity))
  259.                     {
  260.                         isGetQuaintity = true;
  261.                     }
  262.                     else
  263.                     {
  264.                         Console.WriteLine("Такого кол-ва нет.");
  265.                     }
  266.                 }
  267.                 else
  268.                 {
  269.                     Console.WriteLine("Это даже не число.");
  270.                 }
  271.             }
  272.  
  273.             return quantity;
  274.         }
  275.     }
  276.  
  277.     class StackOfProduct
  278.     {
  279.         private Product _product;
  280.         private int _quantity;
  281.  
  282.         public StackOfProduct(Product product, int quantity)
  283.         {
  284.             _product = product;
  285.             _quantity = quantity;
  286.         }
  287.  
  288.         public string Name => _product.Name;
  289.         public int PriceForOne => _product.Price;
  290.         public int Quantity => _quantity;
  291.         public bool IsEmpty => _quantity <= 0;
  292.  
  293.         public void AddQuantity(int quantity)
  294.         {
  295.             _quantity += quantity;
  296.         }
  297.  
  298.         public void ReduceAmount(int quantity)
  299.         {            
  300.             _quantity -= quantity;
  301.            
  302.         }
  303.  
  304.         public void ShowInfo()
  305.         {
  306.             Console.Write($"{Name} | Стоит {PriceForOne} золота/шт | Кол-во: {Quantity}");
  307.         }
  308.  
  309.         public bool HaveQuanity(int quanity)
  310.         {
  311.             return _quantity >= quanity;
  312.         }
  313.     }
  314.  
  315.     class Product
  316.     {
  317.         private string _name;
  318.         private int _price;
  319.  
  320.         public Product(string name, int price)
  321.         {
  322.             _name = name;
  323.             _price = price;
  324.         }
  325.  
  326.         public string Name => _name;
  327.         public int Price => _price;
  328.     }
  329. }
Add Comment
Please, Sign In to add comment