Advertisement
holllowknight

Магазин

Apr 26th, 2023 (edited)
1,067
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ConsoleApp
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             const int CommandShowDealerGoods = 1;
  12.             const int CommandShowInventory = 2;
  13.             const int CommandBuy = 3;
  14.             const int CommandExit = 4;
  15.  
  16.             bool isRunning = true;
  17.             Dealer dealer = new Dealer(0);
  18.             Player player = new Player(90);
  19.  
  20.             Console.WriteLine($"{CommandShowDealerGoods} Посмотреть товары продавца");
  21.             Console.WriteLine($"{CommandShowInventory} Посмотреть свои вещи");
  22.             Console.WriteLine($"{CommandBuy} Совершить покупку");
  23.             Console.WriteLine($"{CommandExit} Выйти из программы");
  24.  
  25.             while (isRunning)
  26.             {
  27.                 Console.Write("Введите команду: ");
  28.                 int command = UserUtils.GetNumberFromUser();
  29.  
  30.                 switch (command)
  31.                 {
  32.                     case CommandShowDealerGoods:
  33.                         dealer.ShowGoods();
  34.                         break;
  35.  
  36.                     case CommandShowInventory:
  37.                         player.ShowInventory();
  38.                         break;
  39.  
  40.                     case CommandBuy:
  41.                         player.BuyFrom(dealer);
  42.                         break;
  43.  
  44.                     case CommandExit:
  45.                         isRunning = false;
  46.                         break;
  47.                 }
  48.             }
  49.         }
  50.     }
  51.  
  52.     class UserUtils
  53.     {
  54.         public static int GetNumberFromUser()
  55.         {
  56.             string userInput;
  57.             int userNumber;
  58.             bool isNumber = false;
  59.  
  60.             do
  61.             {
  62.                 userInput = Console.ReadLine();
  63.                 isNumber = int.TryParse(userInput, out userNumber);
  64.             }
  65.             while (isNumber == false);
  66.  
  67.             return userNumber;
  68.         }
  69.     }
  70.  
  71.     class Trader
  72.     {
  73.         protected List<Thing> Goods = new List<Thing>();
  74.         protected int Money;
  75.  
  76.         public Trader(int money)
  77.         {
  78.             Money = money;
  79.         }
  80.  
  81.         public void ShowGoods()
  82.         {
  83.             for (int i = 0; i < Goods.Count; i++)
  84.                 Console.WriteLine($"{i + 1} {Goods[i].Info}");
  85.         }
  86.     }
  87.  
  88.     class Dealer : Trader
  89.     {
  90.         public Dealer(int money) : base(money)
  91.         {
  92.             Goods.AddRange(new List<Thing>{ new Thing("Меч", 100), new Thing("Щит", 20), new Thing("Факел", 2) });
  93.         }
  94.  
  95.         public bool TryGetPrice(int thingIndex, out int price)
  96.         {
  97.             price = 0;
  98.  
  99.             if ((thingIndex < 0) || (thingIndex >= Goods.Count))
  100.                 return false;
  101.  
  102.             price = Goods[thingIndex].Price;
  103.             return true;
  104.         }
  105.  
  106.         public Thing Sell(int thingNumber, int incomingMoney)
  107.         {
  108.             Money += incomingMoney;
  109.             Thing selligThing = Goods.ElementAt(thingNumber);
  110.             Goods.RemoveAt(thingNumber);
  111.             return selligThing;
  112.         }
  113.     }
  114.  
  115.     class Player : Trader
  116.     {
  117.         public Player(int money) : base(money) { }
  118.  
  119.         public void ShowInventory()
  120.         {
  121.             base.ShowGoods();
  122.             Console.WriteLine($"В кошельке: {Money}");
  123.         }
  124.  
  125.         public void BuyFrom(Dealer dealer)
  126.         {
  127.             int price;
  128.             Console.WriteLine("Введите номер вещи, которую хотите купить");
  129.             int thingNumber = UserUtils.GetNumberFromUser() - 1;
  130.  
  131.             if (dealer.TryGetPrice(thingNumber, out price) == true)
  132.             {
  133.                 if (Money < price)
  134.                 {
  135.                     Console.WriteLine("Недостаточно денег");
  136.                 }
  137.                 else
  138.                 {
  139.                     Money -= price;
  140.                     Thing soldThing = dealer.Sell(thingNumber, price);
  141.                     Goods.Add(soldThing);
  142.                     Console.WriteLine($"В инвентарь добавлен {soldThing.Name}");
  143.                 }
  144.             }
  145.             else
  146.             {
  147.                 Console.WriteLine("Такого товара нет");
  148.             }
  149.         }
  150.     }
  151.  
  152.     class Thing
  153.     {
  154.         public Thing(string name, int price)
  155.         {
  156.             Name = name;
  157.             Price = price;
  158.         }
  159.  
  160.         public string Name { get; private set; }
  161.         public int Price { get; private set; }
  162.  
  163.         public string Info
  164.         {
  165.             get
  166.             {
  167.                 return $"{Name}\tЦена:{Price}";
  168.             }
  169.         }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement