Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace OOPTask6Shop
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Shop shop = new Shop();
- shop.Work();
- }
- }
- public class Shop
- {
- private Seller _seller;
- private Client _client;
- public Shop()
- {
- _seller = new Seller(0);
- _client = new Client(500);
- }
- public void Work()
- {
- const char CommandExit = 'q' ;
- bool isWorking = true;
- while (isWorking)
- {
- _client.ShowInfo();
- Console.WriteLine();
- _seller.ShowInfo();
- Console.WriteLine();
- if (_seller.IsEmpty)
- {
- Console.WriteLine("У продавца больше нет товаров");
- }
- else
- {
- Product productToBuy = _seller.GetProductToBuy();
- if (_client.EnoughMoney(productToBuy))
- {
- _seller.SellProduct(productToBuy);
- _client.BuyProduct(productToBuy);
- Console.WriteLine("Поздравляем с покупкой");
- }
- else
- {
- Console.WriteLine("У клиента недостаточно денег");
- }
- }
- Console.WriteLine($"Нажмите любую клавишу или \"{CommandExit}\" для выхода");
- if (Console.ReadKey().KeyChar == CommandExit)
- isWorking = false;
- Console.Clear();
- }
- }
- }
- public class Product
- {
- private string _title;
- public Product(string title, int cost)
- {
- _title = title;
- Cost = cost;
- }
- public int Cost { get; private set; }
- public void ShowInfo()
- {
- Console.WriteLine($"товар \"{_title}\" стоимостью {Cost}");
- }
- }
- public class Person
- {
- private protected List<Product> Products = new List<Product>();
- private protected int Money;
- private protected string Title;
- public Person(int money)
- {
- Money = money;
- }
- virtual public void ShowInfo()
- {
- Console.WriteLine($"Я {Title} и у меня {Products.Count} товаров.");
- ShowAllProducts();
- }
- private protected void ShowAllProducts()
- {
- int number = 1;
- foreach (Product product in Products)
- {
- Console.Write($"{number++} - ");
- product.ShowInfo();
- }
- }
- }
- public class Seller : Person
- {
- public Seller(int money): base (money)
- {
- Title = "продавец";
- FillProducts();
- }
- public bool IsEmpty => Products.Count == 0;
- public void SellProduct(Product product)
- {
- Products.Remove(product);
- Money += product.Cost;
- }
- public Product GetProductToBuy()
- {
- int indexProduct = 0;
- bool correctIndexValue = false;
- Console.Write("Введите номер товара для покупки: ");
- while (correctIndexValue == false)
- {
- indexProduct = ReadInt() - 1;
- if (indexProduct >= 0 && indexProduct < Products.Count)
- {
- correctIndexValue = true;
- }
- else
- {
- Console.Write("Некорректный номер товара, попробуйте еще: ");
- }
- }
- return Products[indexProduct];
- }
- private int ReadInt()
- {
- int inputNumber;
- while (int.TryParse(Console.ReadLine(), out inputNumber) == false)
- {
- Console.Write("Это не число, попробуйте еще раз: ");
- }
- return inputNumber;
- }
- private void FillProducts()
- {
- Random random = new Random();
- Products.Add(new Product("будильник", random.Next(5, 100)));
- Products.Add(new Product("телефон", random.Next(5, 100)));
- Products.Add(new Product("лопатка", random.Next(5, 100)));
- Products.Add(new Product("совочек", random.Next(5, 100)));
- Products.Add(new Product("машинка", random.Next(5, 100)));
- Products.Add(new Product("кукла", random.Next(5, 100)));
- Products.Add(new Product("слюнявчик", random.Next(5, 100)));
- Products.Add(new Product("презерватив", random.Next(5, 100)));
- }
- }
- public class Client : Person
- {
- public Client(int money) : base(money)
- {
- Title = "покупатель";
- }
- public bool EnoughMoney(Product product)
- {
- return Money >= product.Cost;
- }
- public override void ShowInfo()
- {
- base.ShowInfo();
- Console.WriteLine($"У меня в кошельке {Money} рублей");
- }
- public void BuyProduct(Product product)
- {
- Products.Add(product);
- Money -= product.Cost;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement