Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Linq;
- using System.Numerics;
- using System.Reflection.Metadata;
- namespace Магазин_2
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Shop shop = new Shop();
- shop.Work();
- }
- }
- class Person
- {
- protected List<Product> Products = new List<Product>();
- protected Person(int money)
- {
- Money = money;
- }
- public int Money { get; protected set; }
- public virtual void ShowProductList()
- {
- foreach (Product product in Products)
- {
- product.ShowProduct();
- }
- Console.WriteLine();
- }
- protected bool IsListEmpty()
- {
- if (Products.Count == 0 || Products == null)
- {
- return true;
- }
- return false;
- }
- }
- class Product
- {
- public Product(string name, int price, int id)
- {
- Name = name;
- Price = price;
- Id = id;
- }
- public string Name { get; private set; }
- public int Price { get; private set; }
- public int Id { get; private set; }
- public void ShowProduct()
- {
- Console.WriteLine($"ID: {Id} Название: {Name} Цена: {Price}$");
- }
- }
- class Seller : Person
- {
- public Seller(int money) : base(money)
- {
- FillProductList();
- }
- public override void ShowProductList()
- {
- if (IsListEmpty())
- {
- Console.WriteLine("Товар в продаже отсутствует\n");
- }
- else
- {
- base.ShowProductList();
- }
- }
- private void FillProductList()
- {
- Products.Add(new Product("AK-74", 1500, 1));
- Products.Add(new Product("СВД", 2500, 2));
- Products.Add(new Product("АС «Вал»", 3500, 3));
- }
- public Product TryGetProduct()
- {
- Product foundProduct;
- if (IsListEmpty())
- {
- Console.WriteLine("Товаров нет\n");
- return null;
- }
- else
- {
- Console.WriteLine("Введите id товара для покупки: ");
- int id = Utility.ReturnValidateInputNumber();
- foreach (var product in Products)
- {
- if (product.Id == id)
- {
- foundProduct = product;
- return foundProduct;
- }
- }
- Console.WriteLine("\nТовар не найден\n");
- return null;
- }
- }
- public void SellProduct(Product product)
- {
- if (IsListEmpty())
- {
- return;
- }
- else
- {
- Money += product.Price;
- Products.Remove(product);
- Console.WriteLine($"Товар с ID {product.Id} «{product.Name}» куплен\n");
- }
- }
- }
- class Buyer : Person
- {
- public Buyer(int money) : base(money) { }
- public override void ShowProductList()
- {
- if (IsListEmpty())
- {
- Console.WriteLine("Инвентарь пуст\n");
- Console.WriteLine($"Деньги покупателя: {Money}\n");
- }
- else
- {
- Console.WriteLine("Товар в инвентаре:");
- base.ShowProductList();
- Console.WriteLine($"Деньги покупателя: {Money}\n");
- }
- }
- public void BuyProduct(Product product)
- {
- Money -= product.Price;
- Products.Add(product);
- }
- public bool CanPay(Product product)
- {
- return Money >= product.Price;
- }
- }
- class Shop
- {
- public void Work()
- {
- const string ShowProductMenu = "1";
- const string BuyProductMenu = "2";
- const string ShowInventoryMenu = "3";
- const string ExitProgram = "4";
- int sellerMoney = 0;
- int buyerMoney = 5000;
- Seller seller = new Seller(sellerMoney);
- Buyer buyer = new Buyer(buyerMoney);
- bool isRun = true;
- while (isRun)
- {
- Console.WriteLine($"{ShowProductMenu} - Показать товар в наличии\n{BuyProductMenu} - Купить\n{ShowInventoryMenu} - Показ инвентарь\n{ExitProgram} - Выход");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case ShowProductMenu:
- seller.ShowProductList();
- break;
- case BuyProductMenu:
- Trade(buyer, seller);
- break;
- case ShowInventoryMenu:
- buyer.ShowProductList();
- break;
- case ExitProgram:
- isRun = false;
- break;
- default:
- Console.WriteLine("Неверная команда");
- break;
- }
- }
- }
- private void Trade(Buyer buyer, Seller seller)
- {
- Product product = seller.TryGetProduct();
- if (product == null)
- {
- return;
- }
- else if (buyer.CanPay(product))
- {
- seller.SellProduct(product);
- buyer.BuyProduct(product);
- Console.WriteLine($"Деньги продавца: {seller.Money}");
- Console.WriteLine($"Деньги покупателя: {buyer.Money}\n");
- }
- else
- {
- Console.WriteLine("Недостаточно денег для покупки\n");
- }
- }
- }
- class Utility
- {
- public static int ReturnValidateInputNumber()
- {
- int number;
- while (int.TryParse(Console.ReadLine(), out number) == false)
- {
- Console.WriteLine("Введено не число, попробуйте еще раз: ");
- }
- return number;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement