Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace CSLight
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Shop shop = new Shop(new Buyer());
- shop.Trade();
- }
- }
- class Shop
- {
- private Seller _seller = new Seller();
- private Buyer _buyer;
- public Shop(Buyer buyer)
- {
- _buyer = buyer;
- }
- public void Trade()
- {
- const string CommandEndTrade = "Да";
- bool isTrading = true;
- while (isTrading)
- {
- ShowSellerInventory();
- TryExchangeProduct();
- ShowBuyerInventory();
- Console.WriteLine("Закончить торговлю?");
- string userInput = Console.ReadLine();
- if(userInput == CommandEndTrade)
- {
- isTrading = false;
- }
- }
- Console.ReadKey();
- }
- public void ShowSellerInventory()
- {
- Console.WriteLine("Товары на прилавке:");
- _seller.ShowProducts();
- }
- public void ShowBuyerInventory()
- {
- Console.WriteLine("У покупателя " + _buyer.Money + " денег");
- Console.WriteLine("Инвентарь покупателя:");
- _buyer.ShowProducts();
- }
- public void TryExchangeProduct()
- {
- Console.WriteLine("Выберите продукт для покупки");
- if (int.TryParse(Console.ReadLine(), out int id) && id < _seller.GetProductsCount())
- {
- Product product = _seller.GetProduct(id);
- int price = product.Price;
- if (_buyer.TryBuyProduct(product))
- {
- _buyer.Buy(product);
- _seller.SellProduct(product);
- _buyer.DecreaseMoney(price);
- Console.WriteLine("Покупка завершена");
- }
- else
- {
- Console.WriteLine("У покупателя не хватает денег");
- }
- }
- }
- }
- class Buyer : Person
- {
- public Buyer()
- {
- int startingMoney = 50;
- IncreaseMoney(startingMoney);
- }
- public bool TryBuyProduct(Product product)
- {
- return product.Price <= Money;
- }
- public void Buy(Product product)
- {
- Bag.AddProduct(product);
- DecreaseMoney(product.Price);
- }
- }
- class Seller : Person
- {
- public Seller()
- {
- int fruitsPrice = 10;
- int breadPrice = 5;
- int vegetablePrice = 10;
- int meatPrice = 30;
- Bag.AddProduct(new Product("Яблоки", fruitsPrice));
- Bag.AddProduct(new Product("Черешня", fruitsPrice));
- Bag.AddProduct(new Product("Апельсин", fruitsPrice));
- Bag.AddProduct(new Product("Хлеб", breadPrice));
- Bag.AddProduct(new Product("Булочка", breadPrice));
- Bag.AddProduct(new Product("Пирожок", breadPrice));
- Bag.AddProduct(new Product("Помидоры", vegetablePrice));
- Bag.AddProduct(new Product("Огурцы", vegetablePrice));
- Bag.AddProduct(new Product("Мясо", meatPrice));
- Console.WriteLine("На прилавке" + Bag.ProductsCount);
- }
- public void SellProduct(Product product)
- {
- Bag.RemoveProduct(product);
- IncreaseMoney(product.Price);
- }
- }
- class Person
- {
- protected Inventory Bag = new Inventory();
- public int Money { get; private set; }
- public int GetProductsCount()
- {
- return Bag.ProductsCount;
- }
- public void ShowProducts()
- {
- for (int i = 0; i < Bag.ProductsCount; i++)
- {
- Console.WriteLine($"{i} - {Bag.GetProduct(i).Name} - {Bag.GetProduct(i).Price}");
- }
- }
- public void IncreaseMoney(int value)
- {
- if (value > 0)
- {
- Money += value;
- }
- }
- public void DecreaseMoney(int value)
- {
- if (value > 0)
- {
- Money -= value;
- if (Money < 0)
- {
- Money = 0;
- }
- }
- }
- public Product GetProduct(int id)
- {
- return Bag.GetProduct(id);
- }
- }
- class Inventory
- {
- private List<Product> _products = new List<Product>();
- public int ProductsCount
- {
- get { return _products.Count; }
- }
- public void AddProduct(Product product)
- {
- _products.Add(product);
- }
- public void RemoveProduct(Product product)
- {
- _products.Remove(product);
- }
- public Product GetProduct(int id)
- {
- return _products[id];
- }
- }
- class Product
- {
- public Product(string name, int price)
- {
- Name = name;
- Price = price;
- }
- public string Name { get; private set; }
- public int Price { get; private set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement