Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace BuyerAndSeller;
- class Program
- {
- static void Main(string[] args)
- {
- const string ShowProductCommand = "1";
- const string SellCommand = "2";
- const string ShowBagCommand = "3";
- const string ExitCommand = "4";
- bool isActive = true;
- Buyer buyer = new Buyer();
- Seller seller = new Seller();
- while (isActive)
- {
- buyer.ShowMoney();
- Console.WriteLine("Магазин\n");
- Console.WriteLine($"{ShowProductCommand} - Показать весь ассортимент товаров");
- Console.WriteLine($"{SellCommand} - Купить товар");
- Console.WriteLine($"{ShowBagCommand} - Посмотреть список купленных товаров");
- Console.WriteLine($"{ExitCommand} - Выход\n");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case ShowProductCommand:
- seller.ShowProducts();
- break;
- case SellCommand:
- seller.Sell(buyer);
- break;
- case ShowBagCommand:
- buyer.ShowBag();
- break;
- case ExitCommand:
- isActive = false;
- Console.WriteLine("\nВыход из магазина.\n");
- break;
- default:
- Console.WriteLine("Такой команды нет.\n");
- break;
- }
- Console.ReadKey(true);
- Console.Clear();
- }
- }
- }
- class Product
- {
- public Product(string name, int price)
- {
- Name = name;
- Price = price;
- }
- public string Name { get; private set; }
- public int Price { get; private set; }
- }
- class Cell
- {
- public Cell(Product product, int amount)
- {
- Product = product;
- Amount = amount;
- }
- public Product Product { get; private set; }
- public int Amount { get; private set; }
- public void DecreaseAmount(int amount)
- {
- Amount -= amount;
- }
- }
- class Goods
- {
- public List<Cell> cell = new List<Cell>();
- public Dictionary<string, int> bag = new Dictionary<string, int>();
- public int money = 4000;
- public void ShowProducts()
- {
- foreach (var item in cell)
- {
- Console.WriteLine($"{item.Product.Name} - {item.Amount}шт.\t{item.Product.Price}");
- }
- }
- public void ShowBag()
- {
- foreach (var item in bag)
- {
- Console.WriteLine($"{item.Key} - {item.Value} шт.");
- }
- }
- }
- class Seller : Goods
- {
- public Seller()
- {
- cell.Add(new Cell(new Product("Мана", 12), 67));
- cell.Add(new Cell(new Product("Хилка", 18), 89));
- cell.Add(new Cell(new Product("Берсерк", 60), 5));
- cell.Add(new Cell(new Product("Меч", 1000), 2));
- }
- public void Sell(Buyer buyer)
- {
- string productName;
- int amount;
- ShowProducts();
- Console.Write("Введите название товара, который хотите приобрести: ");
- productName = Console.ReadLine();
- if (TryToFindProduct(productName, out Cell product))
- {
- Console.Write("Введите желаемое количество товара: ");
- amount = ReadInt();
- if (amount > product.Amount)
- {
- Console.WriteLine("В магазине недостаточно данного товара.");
- Console.WriteLine("Вы можете ввести другое количество или выбрать другой товар.");
- }
- else
- {
- if (buyer.Buy(product.Product, amount) == true)
- {
- product.DecreaseAmount(amount);
- }
- }
- }
- }
- private int ReadInt()
- {
- int result;
- while (int.TryParse(Console.ReadLine(), out result) == false)
- {
- Console.WriteLine("Необходимо ввести целое число.\n");
- Console.Write("Введи целое число: ");
- }
- return result;
- }
- private bool TryToFindProduct(string title, out Cell findItem)
- {
- findItem = null;
- foreach (var item in cell)
- {
- if (item.Product.Name.ToLower() == title.ToLower())
- {
- findItem = item;
- return true;
- }
- }
- Console.WriteLine("Такого товара нет. Повторите поиск.");
- return false;
- }
- }
- class Buyer : Goods
- {
- public bool Buy(Product product, int amount)
- {
- int moneyNeedForBuy = product.Price * amount;
- if (money == 0)
- {
- Console.WriteLine("У вас закончились деньги");
- return false;
- }
- else if (moneyNeedForBuy > money)
- {
- Console.WriteLine("Недостаточно денег для покупки.");
- return false;
- }
- else
- {
- money -= moneyNeedForBuy;
- AddToBag(product, amount);
- Console.WriteLine("Товар добавлен в сумку.");
- return true;
- }
- }
- public void AddToBag(Product product, int amount)
- {
- if (bag.ContainsKey(product.Name) == false || bag.Count == 0)
- {
- bag.Add(product.Name, amount);
- }
- else
- {
- foreach (var item in bag)
- {
- if (item.Key == product.Name)
- {
- bag[product.Name] += amount;
- }
- }
- }
- }
- public void ShowMoney()
- {
- Console.SetCursorPosition(0, 30);
- Console.WriteLine($"У вас есть - {money} монет.");
- Console.SetCursorPosition(0, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment