Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApp
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Hello World!");
- Dealer dealer = new Dealer(1000);
- Player player = new Player(90);
- dealer.ReceiveGoods();
- dealer.ShowGoods();
- player.ShowGoods();
- dealer.AskAboutTrading(player);
- dealer.ShowGoods();
- player.ShowGoods();
- }
- class Thing
- {
- public string Name { get; private set; }
- public int Price { get; private set; }
- public Thing(string name, int price)
- {
- Name = name;
- Price = price;
- }
- public string Info {
- get
- {
- return $"{Name}\t{Price}";
- }
- }
- }
- class Person
- {
- protected Thing[] _goods;
- public int Money { get; protected set; }
- public Person(int money)
- {
- _goods = new Thing[] { };
- Money = money;
- }
- public void AddThing(Thing thing)
- {
- Array.Resize(ref _goods, _goods.Length + 1);
- _goods[_goods.Length - 1] = thing;
- }
- public Thing RemoveThing(int index)
- {
- Thing thing = _goods[index];
- for (int i = index; i < _goods.Length - 1; i++)
- {
- _goods[i] = _goods[i + 1];
- }
- Array.Resize(ref _goods, _goods.Length - 1);
- return thing;
- }
- public void ShowGoods(string header)
- {
- Console.WriteLine(header);
- if (_goods.Length == 0)
- {
- Console.WriteLine("-Пусто-");
- }
- else
- {
- Console.WriteLine("N\tТовар\tЦена");
- for (int i = 0; i < _goods.Length; i++)
- {
- Console.WriteLine($"{i}\t{_goods[i].Info}");
- }
- }
- Console.WriteLine();
- }
- }
- class Player : Person
- {
- public Player(int money) : base(money) { }
- internal void BuyThing(Thing product)
- {
- Money -= product.Price;
- AddThing(product);
- }
- public void ShowGoods()
- {
- base.ShowGoods("Инвентарь игрока:");
- }
- }
- class Dealer : Person
- {
- public Dealer(int money) : base(money) { }
- public void ReceiveGoods()
- {
- AddThing(new Thing("Меч", 100));
- AddThing(new Thing("Щит", 20));
- AddThing(new Thing("Факел", 2));
- }
- public void AskAboutTrading(Player player)
- {
- int productID;
- Console.WriteLine("Товар в какой позиции вы хотите приобрести?");
- productID = Convert.ToInt32(Console.ReadLine());
- if(productID >= _goods.Length)
- {
- Console.WriteLine("У меня нет такого товара");
- }
- else if (player.Money < _goods[productID].Price)
- {
- Console.WriteLine("У тебя недостаточно денег");
- }
- else
- {
- Thing product = SellThing(productID);
- player.BuyThing(product);
- }
- }
- private Thing SellThing(int productID)
- {
- Thing thingForSell = RemoveThing(productID);
- Money += thingForSell.Price;
- return thingForSell;
- }
- public void ShowGoods()
- {
- base.ShowGoods("Инвентарь торговца:");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment