Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace ConsoleApp
- {
- class Program
- {
- static void Main(string[] args)
- {
- const int CommandShowDealerGoods = 1;
- const int CommandShowInventory = 2;
- const int CommandBuy = 3;
- const int CommandExit = 4;
- bool isRunning = true;
- Dealer dealer = new Dealer(0);
- Player player = new Player(90);
- Console.WriteLine($"{CommandShowDealerGoods} Посмотреть товары продавца");
- Console.WriteLine($"{CommandShowInventory} Посмотреть свои вещи");
- Console.WriteLine($"{CommandBuy} Совершить покупку");
- Console.WriteLine($"{CommandExit} Выйти из программы");
- while (isRunning)
- {
- Console.Write("Введите команду: ");
- int command = UserUtils.GetNumberFromUser();
- switch (command)
- {
- case CommandShowDealerGoods:
- dealer.ShowGoods();
- break;
- case CommandShowInventory:
- player.ShowInventory();
- break;
- case CommandBuy:
- player.BuyFrom(dealer);
- break;
- case CommandExit:
- isRunning = false;
- break;
- }
- }
- }
- }
- class UserUtils
- {
- public static int GetNumberFromUser()
- {
- string userInput;
- int userNumber;
- bool isNumber = false;
- do
- {
- userInput = Console.ReadLine();
- isNumber = int.TryParse(userInput, out userNumber);
- }
- while (isNumber == false);
- return userNumber;
- }
- }
- class Trader
- {
- protected List<Thing> Goods = new List<Thing>();
- protected int Money;
- public Trader(int money)
- {
- Money = money;
- }
- public void ShowGoods()
- {
- for (int i = 0; i < Goods.Count; i++)
- Console.WriteLine($"{i + 1} {Goods[i].Info}");
- }
- }
- class Dealer : Trader
- {
- public Dealer(int money) : base(money)
- {
- Goods.AddRange(new List<Thing>{ new Thing("Меч", 100), new Thing("Щит", 20), new Thing("Факел", 2) });
- }
- public bool TryGetPrice(int thingIndex, out int price)
- {
- price = 0;
- if ((thingIndex < 0) || (thingIndex >= Goods.Count))
- return false;
- price = Goods[thingIndex].Price;
- return true;
- }
- public Thing Sell(int thingNumber, int incomingMoney)
- {
- Money += incomingMoney;
- Thing selligThing = Goods.ElementAt(thingNumber);
- Goods.RemoveAt(thingNumber);
- return selligThing;
- }
- }
- class Player : Trader
- {
- public Player(int money) : base(money) { }
- public void ShowInventory()
- {
- base.ShowGoods();
- Console.WriteLine($"В кошельке: {Money}");
- }
- public void BuyFrom(Dealer dealer)
- {
- int price;
- Console.WriteLine("Введите номер вещи, которую хотите купить");
- int thingNumber = UserUtils.GetNumberFromUser() - 1;
- if (dealer.TryGetPrice(thingNumber, out price) == true)
- {
- if (Money < price)
- {
- Console.WriteLine("Недостаточно денег");
- }
- else
- {
- Money -= price;
- Thing soldThing = dealer.Sell(thingNumber, price);
- Goods.Add(soldThing);
- Console.WriteLine($"В инвентарь добавлен {soldThing.Name}");
- }
- }
- else
- {
- Console.WriteLine("Такого товара нет");
- }
- }
- }
- class Thing
- {
- public Thing(string name, int price)
- {
- Name = name;
- Price = price;
- }
- public string Name { get; private set; }
- public int Price { get; private set; }
- public string Info
- {
- get
- {
- return $"{Name}\tЦена:{Price}";
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement