Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Security.Policy;
- class Program
- {
- static void Main(string[] args)
- {
- Seller seller = new Seller();
- Player player = new Player();
- Shop shop = new Shop(player, seller);
- shop.Work();
- }
- }
- class Shop
- {
- private Player _player = null;
- private Seller _seller = null;
- public Shop(Player player, Seller seller)
- {
- _player = player;
- _seller = seller;
- }
- public void Work()
- {
- const string ShowInventorySellerCommand = "1";
- const string BuyProductCommand = "2";
- const string ShowInventoryPlayerCommand = "3";
- const string IsExitCommand = "4";
- string userInput;
- bool isWorking = true;
- while (isWorking)
- {
- Console.Clear();
- Console.WriteLine($"Игрок зашёл в лавку торговца. У него {_player.ShowMoney()} монет.\n{ShowInventorySellerCommand})Посмотреть товар.\n{BuyProductCommand})Купить товар.\n" +
- $"{ShowInventoryPlayerCommand})Посмотреть свой инвентарь.\n{IsExitCommand})Покинуть лавку.");
- userInput = Console.ReadLine();
- Console.Clear();
- switch (userInput)
- {
- case ShowInventorySellerCommand:
- _seller.ShowInventory();
- break;
- case BuyProductCommand:
- SaleProduct();
- break;
- case ShowInventoryPlayerCommand:
- _player.ShowInventory();
- break;
- case IsExitCommand:
- isWorking = false;
- break;
- }
- }
- }
- public void SaleProduct()
- {
- Console.WriteLine("Что бы вы хотели купить?");
- string userInput = Console.ReadLine();
- _seller.TrySearch(userInput, out Product selectedProduct);
- if(selectedProduct != null)
- {
- if (_player.IsEnoughMoney(selectedProduct))
- {
- _seller.GiveProduct(selectedProduct);
- _player.TakeProduct(selectedProduct);
- }
- }
- }
- }
- class Human
- {
- protected List<Product> Products = new List<Product>();
- protected int Money = 0;
- public void ShowInventory()
- {
- foreach (Product product in Products)
- {
- Console.WriteLine($"Название:{product.Name}|Цена:{product.Price}|Урон:{product.Damage}|Прочность:{product.Endurance}");
- }
- Console.ReadKey();
- }
- }
- class Seller : Human
- {
- public Seller()
- {
- FillProduct();
- }
- public void FillProduct()
- {
- Products.Add(new Product("WoodKnife", 5, 25, 100));
- Products.Add(new Product("IronKnife", 15, 50, 150));
- Products.Add(new Product("GoldKnife", 30, 100, 25));
- }
- public void GiveProduct(Product product)
- {
- Products.Remove(product);
- Money += product.Price;
- }
- public void TrySearch(string productName, out Product selectedProduct)
- {
- Product temporaryProduct = null;
- foreach (var product in Products)
- {
- if (product.Name == productName)
- {
- temporaryProduct = product;
- Console.WriteLine("Такой товар есть в наличии!");
- Console.ReadKey();
- break;
- }
- else
- {
- selectedProduct = null;
- Console.WriteLine("Такого товара нет в наличии!");
- Console.ReadKey();
- break;
- }
- }
- selectedProduct = temporaryProduct;
- }
- }
- class Player : Human
- {
- private Random _random = new Random();
- public Player()
- {
- Money = CreateMoney();
- }
- private int CreateMoney()
- {
- int minMoney = 5;
- int maxMoney = 30;
- int temporaryMoney = _random.Next(minMoney, maxMoney);
- return temporaryMoney;
- }
- public bool IsEnoughMoney(Product product)
- {
- if (Money - product.Price < 0)
- {
- Console.WriteLine("У игрока недостаточно денег. Пускай покупает что-то другое или сваливает.");
- Console.ReadKey();
- return false;
- }
- return true;
- }
- public void TakeProduct(Product product)
- {
- Products.Add(product);
- Money -= product.Price;
- }
- public int ShowMoney()
- {
- return Money;
- }
- }
- class Product
- {
- public Product (string name,int price, int damage, int endurance)
- {
- Name = name;
- Price = price;
- Damage = damage;
- Endurance = endurance;
- }
- public string Name { get; private set; }
- public int Price { get; private set; }
- public int Damage { get; private set; }
- public int Endurance { get; private set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement