Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Reflection;
- internal class Program
- {
- static void Main(string[] args)
- {
- Shop shop = new Shop();
- shop.Work();
- }
- }
- public class Product
- {
- public Product(string name, int price)
- {
- Name = name;
- Price = price;
- }
- public string Name { get; private set; }
- public int Price { get; private set; }
- public override string ToString()
- {
- return $"{Name}. Цена: {Price} руб.";
- }
- }
- public class Person
- {
- protected List<Product> Products;
- public Person(int money, string role)
- {
- Money = money;
- Role = role;
- }
- public string Role { get; private set; }
- public int Money { get; protected set; }
- public void ShowInfo()
- {
- Console.WriteLine($"{Role}. Денег: {Money} руб.");
- ShowProducts();
- Console.ReadKey();
- }
- public void ShowProducts()
- {
- Console.WriteLine("Товары: ");
- if (Products.Count > 0)
- {
- for (int i = 0; i < Products.Count; i++)
- {
- Console.WriteLine(i + 1 + ". " + Products[i]);
- }
- }
- else
- {
- Console.WriteLine("Пусто.");
- }
- }
- }
- public class Seller : Person
- {
- public Seller(int money, string role) : base(money, role)
- {
- Products = new List<Product>
- {
- new Product("Яблоко", 50),
- new Product("Банан", 40),
- new Product("Молоко", 80),
- new Product("Хлеб", 30),
- new Product("Сыр", 150),
- new Product("Колбаса", 200),
- new Product("Шоколад", 100),
- new Product("Кофе", 300),
- new Product("Чай", 120),
- new Product("Яйца", 90)
- };
- }
- public bool TryGetProduct(out Product product)
- {
- product = SetProduct();
- if (product != null)
- return true;
- else return false;
- }
- public void AddMoney(int money)
- {
- Money += money;
- }
- private Product SetProduct()
- {
- ShowProducts();
- Console.Write("\nВыбере товар: ");
- int ProductNumber = ReadInt();
- if (ProductNumber > 0 && ProductNumber < Products.Count)
- {
- return Products[ProductNumber - 1];
- }
- return null;
- }
- private int ReadInt()
- {
- int number;
- while (int.TryParse(Console.ReadLine(), out number) == false)
- {
- Console.Write("Ошибка ввода. Повторите: ");
- }
- return number;
- }
- }
- public class Buyer : Person
- {
- public Buyer(int money, string role) : base(money, role)
- {
- Products = new List<Product>();
- }
- public void Buy(Product product)
- {
- Money -= product.Price;
- Products.Add(product);
- }
- public bool CanAfford(Product product)
- {
- return Money >= product.Price;
- }
- }
- public class Shop
- {
- private Seller _seller = new Seller(10000, "Продавец");
- private Buyer _buyer = new Buyer(1000, "Покупатель");
- public void Work()
- {
- const string CommandSellInfoBuyer = "1";
- const string CommandSellInfoSeller = "2";
- const string CommandConductSale = "3";
- const string CommandExit = "Exit";
- bool isRun = true;
- while (isRun)
- {
- Console.Clear();
- Console.WriteLine($"{CommandSellInfoBuyer} - Информация о покупателе.\n" +
- $"{CommandSellInfoSeller} - Информация о продавце.\n" +
- $"{CommandConductSale} - Провести продажу.\n" +
- $"{CommandExit} - Выход.");
- switch (Console.ReadLine())
- {
- case CommandSellInfoBuyer:
- ShowInfoBuyer();
- break;
- case CommandSellInfoSeller:
- ShowInfoSeller();
- break;
- case CommandConductSale:
- ConductSale();
- break;
- case CommandExit:
- isRun = false;
- break;
- default:
- Console.WriteLine("Ошибка ввода");
- Console.ReadKey();
- break;
- }
- }
- Console.WriteLine("Программа завершена.");
- Console.ReadKey();
- }
- private void ConductSale()
- {
- if (_seller.TryGetProduct(out Product product))
- {
- if (_buyer.CanAfford(product))
- {
- _seller.AddMoney(product.Price);
- _buyer.Buy(product);
- Console.WriteLine($"{_buyer.Role} купил {product.Name}.");
- Console.ReadKey();
- }
- else
- {
- Console.WriteLine($"У {_buyer.Role} не достаточно денег.");
- Console.ReadKey();
- }
- }
- else
- {
- Console.WriteLine("Нет товара под этим номером.");
- Console.ReadKey();
- }
- }
- private void ShowInfoSeller()
- {
- _seller.ShowInfo();
- }
- private void ShowInfoBuyer()
- {
- _buyer.ShowInfo();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment