Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Numerics;
- class Program
- {
- static void Main(string[] args)
- {
- int moneyPlayer = 500;
- int moneySeller = 1000;
- Player player = new Player("Bob", moneyPlayer);
- Seller seller = new Seller("Mike", moneySeller);
- Market market = new Market();
- market.Work(player, seller);
- }
- }
- class Market
- {
- public void Work(Player player, Seller seller)
- {
- const int TradeProductCommand = 1;
- const int ShowAllProductsPlayerCommand = 2;
- const int ShowAllProductsSellerCommand = 3;
- const int ExitForMarket = 9;
- string userInput;
- bool isWork = true;
- while (isWork)
- {
- Console.WriteLine($"Добро пожаловать в магазин.");
- Console.WriteLine("Что желаете?\n");
- Console.WriteLine($"{TradeProductCommand} - Купить товар у продавца, " +
- $"\n{ShowAllProductsPlayerCommand} - Показать все товары игрока, " +
- $"\n{ShowAllProductsSellerCommand} - Показать все товары продавца, " +
- $"\n{ExitForMarket} - Выход из магазина. " +
- $"\nУ Вас есть {player.Money} золотых.");
- userInput = Console.ReadLine();
- int.TryParse(userInput, out int selectMenu);
- switch (selectMenu)
- {
- case TradeProductCommand:
- Trade(seller, player);
- break;
- case ShowAllProductsPlayerCommand:
- player.ShowAllProduct();
- break;
- case ShowAllProductsSellerCommand:
- seller.ShowAllProduct();
- break;
- case ExitForMarket:
- isWork = false;
- break;
- default:
- Console.WriteLine($"\aМеню выбора {userInput} не существует!");
- break;
- }
- Console.WriteLine("Нажмите любую клавишу для продолжения...");
- Console.ReadKey();
- Console.Clear();
- }
- }
- private void Trade(Seller seller, Player player)
- {
- Product product;
- if (seller.TryGetProduct(out product) && player.CanPay(product.Price))
- {
- player.Buy(product);
- seller.Sell(product);
- Console.WriteLine("Торговля завершена успешно.");
- }
- else
- {
- Console.WriteLine($"\aТорговля не состоялась.");
- }
- }
- }
- abstract class BaseCharacter
- {
- protected List<Product> Products = new List<Product>();
- public BaseCharacter(string name, int money)
- {
- Name = name;
- Money = money;
- }
- public string Name { get; protected set; }
- public int Money { get; protected set; }
- public void ShowAllProduct()
- {
- Console.WriteLine("Показываю продукты:");
- int counter = 1;
- for (int i = 0; i < Products.Count; i++)
- {
- Console.Write(" " + counter + ": ");
- Products[i].Show();
- counter++;
- }
- }
- }
- class Player : BaseCharacter
- {
- public Player(string name, int money) : base(name, money) { }
- public void Buy(Product product)
- {
- Products.Add(product);
- Money -= product.Price;
- }
- public bool CanPay(int price)
- {
- return Money >= price;
- }
- }
- class Seller : BaseCharacter
- {
- public Seller(string name, int money) : base(name, money)
- {
- Products = Fill();
- }
- public bool TryGetProduct(out Product product)
- {
- bool isProductInStock;
- string userInput;
- product = null;
- Console.WriteLine("Введите порядковый номер товара, который хотите приобрести:");
- userInput = Console.ReadLine();
- if (int.TryParse(userInput, out int idUserInput) == false)
- {
- Console.WriteLine("\aНужно вводить число!");
- return false;
- }
- isProductInStock = idUserInput > 0 && Products.Count >= idUserInput;
- if (isProductInStock)
- {
- product = Products[idUserInput - 1];
- }
- else
- {
- Console.WriteLine($"\aТовара c порядковым номером {userInput} у {Name} нет.");
- }
- return isProductInStock;
- }
- public void Sell(Product product)
- {
- Money += product.Price;
- Products.Remove(product);
- }
- private List<Product> Fill()
- {
- List<Product> products = new List<Product>()
- {
- new Product("Bread", 4, "Nice tasty bread"),
- new Product("Sword", 20, "Sharp, will kill all enemies"),
- new Product("Armor", 35, "Warm and protect"),
- new Product("Shield", 29, "Lightweight and durable"),
- new Product("Arrows", 19, "Sharp regular arrows")
- };
- return products;
- }
- }
- class Product
- {
- public Product(string name, int price, string description)
- {
- Name = name;
- Price = price;
- Description = description;
- }
- public string Name { get; private set; }
- public int Price { get; private set; }
- public string Description { get; private set; }
- public void Show()
- {
- Console.WriteLine($"Имя - {Name}, Цена - {Price}, Описание - {Description}");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement