Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Ijunior
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Shop shop = new Shop();
- shop.Work();
- }
- }
- class Shop
- {
- private Seller _seller;
- private Сustomer _customer;
- public Shop()
- {
- _seller = new Seller();
- _customer = new Сustomer();
- }
- public void Work()
- {
- const string CommandShowSellerInfo = "1";
- const string CommandShowСustomerInfo = "2";
- const string CommandTrade = "3";
- const string CommandExit = "4";
- bool isWork = true;
- while (isWork)
- {
- Console.Write($"{CommandShowSellerInfo} - показать товары и баланс продавца" +
- $"\n{CommandShowСustomerInfo} - показать товары и баланс покупателя" +
- $"\n{CommandTrade} - купить товары" +
- $"\n{CommandExit} - выйти из программы" +
- $"\nВведите номер: ");
- switch (Console.ReadLine())
- {
- case CommandShowSellerInfo:
- _seller.ShowInfo();
- break;
- case CommandShowСustomerInfo:
- _customer.ShowInfo();
- break;
- case CommandTrade:
- Trade();
- break;
- case CommandExit:
- isWork = false;
- break;
- default:
- Console.WriteLine("Некорректный ввод");
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- private void Trade()
- {
- _seller.ShowProducts();
- Console.Write("Введите товар для покупки: ");
- if (_seller.TryGetProduct(Console.ReadLine(), out Product product))
- {
- if (_customer.IsMoneyEnough(product.Price))
- {
- _seller.SellProduct(product);
- _customer.BuyProduct(product);
- Console.WriteLine("Товар куплен");
- }
- else
- {
- Console.WriteLine("У покупателя недостаточно денег");
- }
- }
- else
- {
- Console.WriteLine("Такого товара нет");
- }
- }
- }
- class Person
- {
- protected List<Product> Products;
- protected int Money;
- public void ShowInfo()
- {
- ShowBalance();
- Console.WriteLine();
- ShowProducts();
- }
- public void ShowProducts()
- {
- if (Products.Count > 0)
- {
- foreach (var product in Products)
- {
- product.ShowInfo();
- }
- }
- else
- {
- Console.WriteLine("Список пуст");
- }
- }
- private void ShowBalance()
- {
- string separator = new string('-', 10);
- Console.WriteLine($"{separator} \nБаланс: {Money} \n{separator}");
- }
- }
- class Seller : Person
- {
- public Seller()
- {
- Money = 0;
- FillList();
- }
- public bool TryGetProduct(string name, out Product foundProduct)
- {
- foundProduct = null;
- foreach (var product in Products)
- {
- if (product.Name.ToLower() == name.ToLower())
- {
- foundProduct = product;
- return true;
- }
- }
- return false;
- }
- public void SellProduct(Product product)
- {
- Money += product.Price;
- Products.Remove(product);
- }
- private void FillList()
- {
- Products = new List<Product>();
- int minRandomNumber = 10;
- int maxRandomNumber = 100;
- List<string> names = new List<string>()
- { "яблоко", "банан", "груша", "манго", "слива",
- "помидор", "огурец", "кабачок", "редис", "картофель",
- "клубника", "голубика", "черника", "вишня", "ежевика"};
- for (int i = 0; i < names.Count; i++)
- {
- Products.Add(new Product(names[i], Utils.GetRandomNumber(minRandomNumber, maxRandomNumber)));
- }
- }
- }
- class Сustomer : Person
- {
- public Сustomer()
- {
- Products = new List<Product>();
- Money = GetRandomMoney();
- }
- public bool IsMoneyEnough(int price)
- {
- if (Money >= price)
- return true;
- return false;
- }
- public void BuyProduct(Product product)
- {
- Money -= product.Price;
- if (product != null)
- Products.Add(product);
- }
- private int GetRandomMoney()
- {
- int minRandomNumber = 100;
- int maxRandomNumber = 500;
- return Utils.GetRandomNumber(minRandomNumber, maxRandomNumber);
- }
- }
- class Product
- {
- public Product(string name, int price)
- {
- Name = name;
- Price = price;
- }
- public string Name { get; }
- public int Price { get; }
- public void ShowInfo()
- {
- Console.WriteLine($"{Name} - {Price} рублей");
- }
- }
- class Utils
- {
- private static Random s_random = new Random();
- public static int GetRandomNumber(int minValue, int maxValue)
- {
- return s_random.Next(minValue, maxValue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment