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 Home_Work
- {
- class Program
- {
- static void Main(string[] args)
- {
- Player player = new Player(200);
- Trader seller = new Trader(5000);
- string userInput;
- bool isWorking = true;
- while (isWorking)
- {
- Console.WriteLine("Приветсвуем вас в торговом доме Виндсома.\n" +
- "1. Посмотреть свой инвентарь.\n" +
- "2. Посмотреть товары продовца.\n" +
- "3. Выйти.");
- userInput = Console.ReadLine();
- Console.Clear();
- switch (userInput)
- {
- case "1":
- player.ShowInventory();
- Console.ReadKey();
- break;
- case "2":
- seller.OfferToSell(player);
- break;
- case "3":
- isWorking = false;
- break;
- }
- Console.Clear();
- }
- }
- }
- class Person
- {
- protected Inventory Inventory;
- protected int Money;
- public Person(int money = 0)
- {
- Inventory = new Inventory();
- Money = money;
- }
- public void ShowInventory()
- {
- Console.WriteLine($"Золота в наличии {Money}.");
- Inventory.ShowAll();
- }
- }
- class Player : Person
- {
- public Player(int money) : base(money)
- {
- Inventory.AddProduct(new Product("Тренировочный одноручный меч", 20), 1);
- Inventory.AddProduct(new Product("Тренировочный щит", 15), 1);
- Inventory.AddProduct(new Product("Форма рыцаря - новичка", 40), 1);
- Inventory.AddProduct(new Product("Бинт", 4), 2);
- Inventory.AddProduct(new Product("Железная сабля стражи", 50), 1);
- }
- public void Buy(Product product, int quantity)
- {
- Inventory.AddProduct(product, quantity);
- Money -= product.Price * quantity;
- }
- public bool IsSolvent(int purchase)
- {
- return Money >= purchase;
- }
- }
- class Trader : Person
- {
- public Trader(int money) : base(money)
- {
- Inventory.AddProduct(new Product("Меч короля Лича", 2000), 1);
- Inventory.AddProduct(new Product("Одноручный железный клинок рыцарей серебрянного авангарда", 150), 2);
- Inventory.AddProduct(new Product("Щит солнечного колодца", 200), 2);
- Inventory.AddProduct(new Product("Одноручный меч воина клана Ястреба", 70), 10);
- Inventory.AddProduct(new Product("Двухручный меч Кромсатель Драконов", 2000), 1);
- Inventory.AddProduct(new Product("Броня Берсерка", 1500), 1);
- Inventory.AddProduct(new Product("Железный щит стражи", 40), 20);
- Inventory.AddProduct(new Product("Железная сабля стражи", 50), 20);
- }
- public void OfferToSell(Player player)
- {
- ConsoleKeyInfo userKey;
- bool isShowing = true;
- while (isShowing)
- {
- ShowInventory();
- Console.WriteLine("\n1. Если хотите что-то купить.\n" +
- "2. Выйти.");
- userKey = Console.ReadKey(true);
- switch (userKey.Key)
- {
- case ConsoleKey.D1:
- TrySell(player);
- break;
- case ConsoleKey.D2:
- isShowing = false;
- break;
- default:
- Console.WriteLine("Такой команды нет.");
- Console.ReadKey();
- break;
- }
- Console.Clear();
- }
- }
- private void TrySell(Player player)
- {
- Product product;
- int quantity;
- product = Inventory.GetProduct();
- quantity = Inventory.GetQuantity(product.Name);
- if (player.IsSolvent(product.Price * quantity))
- {
- player.Buy(product, quantity);
- Sell(product, quantity);
- Console.WriteLine("Сделка совершена.");
- }
- else
- {
- Console.WriteLine("Нужно больше золота.");
- }
- Console.ReadKey();
- }
- private void Sell(Product product, int quantity)
- {
- Inventory.ReduceProduct(product, quantity);
- Money += product.Price * quantity;
- }
- }
- class Inventory
- {
- private Dictionary<string, StackOfProduct> _products;
- public Inventory()
- {
- _products = new Dictionary<string, StackOfProduct>();
- }
- public int StacksCount => _products.Count();
- public void AddProduct(Product product, int quantity)
- {
- if (_products.ContainsKey(product.Name))
- {
- _products[product.Name].AddQuantity(quantity);
- }
- else
- {
- _products.Add(product.Name, new StackOfProduct(product, quantity));
- }
- }
- public void ShowAll()
- {
- int index = 1;
- Console.WriteLine($"В наличии: ");
- foreach (var product in _products)
- {
- Console.Write($"\n№{index}. ");
- product.Value.ShowInfo();
- index++;
- }
- }
- public void ReduceProduct(Product product, int quantity)
- {
- _products[product.Name].ReduceAmount(quantity);
- if (_products[product.Name].IsEmpty)
- {
- _products.Remove(product.Name);
- }
- }
- public Product GetProduct()
- {
- Product product = null;
- bool isGetIndex = false;
- int index;
- string userInput;
- while (isGetIndex == false)
- {
- Console.Write("Введите индекс товара: ");
- userInput = Console.ReadLine();
- if (Int32.TryParse(userInput, out index) && index > 0 && index <= StacksCount)
- {
- string productName = _products.ElementAt(index - 1).Value.Name;
- int productPrice = _products.ElementAt(index - 1).Value.PriceForOne;
- product = new (productName, productPrice);
- isGetIndex = true;
- }
- else
- {
- Console.WriteLine("Такого индекса нет.");
- }
- }
- return product;
- }
- public int GetQuantity(string productName)
- {
- bool isGetQuaintity = false;
- int quantity = 0;
- string userInput;
- while (isGetQuaintity == false)
- {
- Console.Write("Введите кол-во товара: ");
- userInput = Console.ReadLine();
- if (Int32.TryParse(userInput, out quantity))
- {
- if (quantity >= 0 && _products[productName].HaveQuanity(quantity))
- {
- isGetQuaintity = true;
- }
- else
- {
- Console.WriteLine("Такого кол-ва нет.");
- }
- }
- else
- {
- Console.WriteLine("Это даже не число.");
- }
- }
- return quantity;
- }
- }
- class StackOfProduct
- {
- private Product _product;
- private int _quantity;
- public StackOfProduct(Product product, int quantity)
- {
- _product = product;
- _quantity = quantity;
- }
- public string Name => _product.Name;
- public int PriceForOne => _product.Price;
- public int Quantity => _quantity;
- public bool IsEmpty => _quantity <= 0;
- public void AddQuantity(int quantity)
- {
- _quantity += quantity;
- }
- public void ReduceAmount(int quantity)
- {
- _quantity -= quantity;
- }
- public void ShowInfo()
- {
- Console.Write($"{Name} | Стоит {PriceForOne} золота/шт | Кол-во: {Quantity}");
- }
- public bool HaveQuanity(int quanity)
- {
- return _quantity >= quanity;
- }
- }
- class Product
- {
- private string _name;
- private int _price;
- public Product(string name, int price)
- {
- _name = name;
- _price = price;
- }
- public string Name => _name;
- public int Price => _price;
- }
- }
Add Comment
Please, Sign In to add comment