holllowknight

ДЗ Магазин

Apr 6th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.38 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 ConsoleApp
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.WriteLine("Hello World!");
  14.             Dealer dealer = new Dealer(1000);
  15.             Player player = new Player(90);
  16.  
  17.             dealer.ReceiveGoods();
  18.             dealer.ShowGoods();
  19.             player.ShowGoods();
  20.  
  21.             dealer.AskAboutTrading(player);
  22.             dealer.ShowGoods();
  23.             player.ShowGoods();
  24.         }
  25.  
  26.         class Thing
  27.         {
  28.             public string Name { get; private set; }
  29.             public int Price { get; private set; }
  30.  
  31.             public Thing(string name, int price)
  32.             {
  33.                 Name = name;
  34.                 Price = price;
  35.             }
  36.  
  37.             public string Info {
  38.                 get
  39.                 {
  40.                     return $"{Name}\t{Price}";
  41.                 }
  42.             }
  43.         }
  44.  
  45.         class Person
  46.         {
  47.             protected Thing[] _goods;
  48.             public int Money { get; protected set; }
  49.  
  50.             public Person(int money)
  51.             {
  52.                 _goods = new Thing[] { };
  53.                 Money = money;
  54.             }
  55.            
  56.             public void AddThing(Thing thing)
  57.             {
  58.                 Array.Resize(ref _goods, _goods.Length + 1);
  59.                 _goods[_goods.Length - 1] = thing;
  60.             }
  61.  
  62.             public Thing RemoveThing(int index)
  63.             {
  64.                 Thing thing = _goods[index];
  65.                 for (int i = index; i < _goods.Length - 1; i++)
  66.                 {
  67.                     _goods[i] = _goods[i + 1];
  68.                 }
  69.                 Array.Resize(ref _goods, _goods.Length - 1);
  70.                 return thing;
  71.  
  72.             }
  73.  
  74.             public void ShowGoods(string header)
  75.             {
  76.                 Console.WriteLine(header);
  77.                 if (_goods.Length == 0)
  78.                 {
  79.                     Console.WriteLine("-Пусто-");
  80.                 }
  81.                 else
  82.                 {
  83.                     Console.WriteLine("N\tТовар\tЦена");
  84.                     for (int i = 0; i < _goods.Length; i++)
  85.                     {
  86.                         Console.WriteLine($"{i}\t{_goods[i].Info}");
  87.                     }
  88.                 }
  89.                 Console.WriteLine();
  90.             }
  91.         }
  92.  
  93.         class Player : Person
  94.         {
  95.             public Player(int money) : base(money) { }
  96.  
  97.             internal void BuyThing(Thing product)
  98.             {
  99.                 Money -= product.Price;
  100.                 AddThing(product);
  101.             }
  102.  
  103.             public void ShowGoods()
  104.             {
  105.                 base.ShowGoods("Инвентарь игрока:");
  106.             }
  107.         }
  108.  
  109.         class Dealer : Person
  110.         {
  111.             public Dealer(int money) : base(money) { }
  112.  
  113.             public void ReceiveGoods()
  114.             {
  115.                 AddThing(new Thing("Меч", 100));
  116.                 AddThing(new Thing("Щит", 20));
  117.                 AddThing(new Thing("Факел", 2));
  118.             }
  119.  
  120.             public void AskAboutTrading(Player player)
  121.             {
  122.                 int productID;
  123.                 Console.WriteLine("Товар в какой позиции вы хотите приобрести?");
  124.                 productID = Convert.ToInt32(Console.ReadLine());
  125.  
  126.                 if(productID >= _goods.Length)
  127.                 {
  128.                     Console.WriteLine("У меня нет такого товара");
  129.                 }
  130.                 else if (player.Money < _goods[productID].Price)
  131.                 {
  132.                     Console.WriteLine("У тебя недостаточно денег");
  133.                 }
  134.                 else
  135.                 {
  136.                     Thing product = SellThing(productID);
  137.                     player.BuyThing(product);
  138.                 }
  139.             }
  140.  
  141.             private Thing SellThing(int productID)
  142.             {
  143.                 Thing thingForSell = RemoveThing(productID);
  144.                 Money += thingForSell.Price;
  145.                 return thingForSell;
  146.             }
  147.  
  148.             public void ShowGoods()
  149.             {
  150.                 base.ShowGoods("Инвентарь торговца:");
  151.             }
  152.         }
  153.     }
  154. }
Add Comment
Please, Sign In to add comment