Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace C_sharp_Light
- {
- class Program
- {
- public static void Main()
- {
- new Shop().Work();
- }
- }
- public static class RandomStatic
- {
- static private Random _rand = new Random();
- static public int GetNext(int min, int max)
- {
- return _rand.Next(min, max);
- }
- }
- public static class Messager
- {
- static public void ShowMessageWithColor(string message, ConsoleColor color, bool delay)
- {
- ConsoleColor defaultColor = Console.ForegroundColor;
- Console.ForegroundColor = color;
- Console.WriteLine(message);
- Console.ForegroundColor = defaultColor;
- if (delay)
- Console.ReadKey();
- }
- }
- public class Shop
- {
- private int _money = 0;
- private Queue<Buyer> _buyers = new Queue<Buyer>();
- public Shop()
- {
- for (int i = 0; i < 20; i++)
- {
- _buyers.Enqueue(new Buyer());
- }
- }
- public void Work()
- {
- while (_buyers.Count > 0)
- {
- Console.Clear();
- Messager.ShowMessageWithColor($"Покупателей осталось - {_buyers.Count} \nДенег у магазина - {_money}\n", ConsoleColor.Yellow, false);
- _money += _buyers.Dequeue().GetPay();
- Console.Write($"\n\nEnter - вызов следующего покупателя");
- Console.ReadKey(true);
- }
- }
- }
- public class Buyer
- {
- private int _money;
- private List<Item> _purchases = new List<Item>();
- public Buyer()
- {
- _money = RandomStatic.GetNext(200, 1300);
- int amountPurchases = RandomStatic.GetNext(1, 10);
- for (int i = 0; i < amountPurchases; i++)
- {
- _purchases.Add(new Item());
- }
- }
- public int GetPay()
- {
- while (_purchases.Count > 0)
- {
- int costAllProduscts = GetCostAllProducts();
- Messager.ShowMessageWithColor($"Цена покупоков составляет - {costAllProduscts}", ConsoleColor.White, false);
- if (ChekSolvency())
- {
- _money -= costAllProduscts;
- Messager.ShowMessageWithColor($"Покупатель сделал покупку на {costAllProduscts} денег", ConsoleColor.Red, false);
- return costAllProduscts;
- }
- else
- {
- RemoveProducts();
- }
- }
- Messager.ShowMessageWithColor("Покупатель ничего не купил.", ConsoleColor.Red, false);
- return 0;
- }
- private bool ChekSolvency()
- {
- return GetCostAllProducts() <= _money;
- }
- private void RemoveProducts()
- {
- while (!ChekSolvency())
- {
- int indexProductForRemove = RandomStatic.GetNext(0, _purchases.Count);
- Messager.ShowMessageWithColor($"Покупатель выложил из корзины - {_purchases[indexProductForRemove].GetInfo()}", ConsoleColor.Yellow, false);
- _purchases.RemoveAt(indexProductForRemove);
- }
- }
- private int GetCostAllProducts()
- {
- int result = 0;
- for (int i = 0; i < _purchases.Count; i++)
- {
- result += _purchases[i].Cost;
- }
- return result;
- }
- }
- public class Item
- {
- public int Cost { get; private set; }
- public string Name { get; private set; }
- public Item()
- {
- Cost = RandomStatic.GetNext(140, 500);
- Name = new string[] { "Хлеб", "Молоко", "Мясо", "Шоколад", "Кофе" }[RandomStatic.GetNext(0, 5)];
- }
- public string GetInfo()
- {
- return $"{Name} за {Cost}";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement