Last active
May 5, 2025 19:47
-
-
Save Stas2056/2dabe0281210cd10c1748445c44962f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace HomeworkSupermarket | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Supermarket supermarket = new Supermarket(); | |
supermarket.Work(); | |
} | |
} | |
class Supermarket | |
{ | |
private Queue<Client> _clients = new Queue<Client>(); | |
private List<Product> _products = new List<Product>(); | |
public Supermarket() | |
{ | |
int productsCount = 10; | |
int clientsCount = 10; | |
for (int i = 0; i < productsCount; i++) | |
{ | |
_products.Add(new Product()); | |
} | |
for (int i = 0; i < clientsCount; i++) | |
{ | |
_clients.Enqueue(new Client(_products)); | |
} | |
} | |
public int Money { get; private set; } | |
public void Work() | |
{ | |
while (_clients.Count > 0) | |
{ | |
Console.WriteLine($"Balance: {Money}\nClients in queue: {_clients.Count}\n"); | |
Client currentClient = _clients.Dequeue(); | |
Money += currentClient.PayForBasket(); | |
Console.ReadKey(); | |
Console.Clear(); | |
} | |
Console.WriteLine($"All clients are left, you are earned {Money} money."); | |
Console.ReadKey(); | |
} | |
} | |
class Product | |
{ | |
public Product() | |
{ | |
Price = Utils.GenerateRandomNumber(1, 250); | |
} | |
public int Price { get; private set; } | |
} | |
class Client | |
{ | |
private List<Product> _basket = new List<Product>(); | |
private List<Product> _bag = new List<Product>(); | |
private static int _lastId; | |
public Client(List<Product> productsInSupermarket) | |
{ | |
int productsWant; | |
int minProductsWant = 1; | |
int maxProductsWant = 10; | |
int minMoney = 20; | |
int maxMoney = 2000; | |
Id = ++_lastId; | |
productsWant = Utils.GenerateRandomNumber(minProductsWant, maxProductsWant); | |
Money = Utils.GenerateRandomNumber(minMoney, maxMoney); | |
for (int i = 0; i < productsWant; i++) | |
{ | |
_basket.Add(productsInSupermarket[Utils.GenerateRandomNumber(0, productsInSupermarket.Count - 1)]); | |
} | |
} | |
public int Id { get; private set; } | |
public int Money { get; private set; } | |
public int PayForBasket() | |
{ | |
while (_basket.Count > 0) | |
{ | |
int bascketPrice = CalculateBascketPriceSum(); | |
if (bascketPrice <= Money) | |
{ | |
Money -= bascketPrice; | |
TransferFromBascketToBag(); | |
Console.WriteLine($"Client № {Id} paid for his basket for {bascketPrice} money."); | |
return bascketPrice; | |
} | |
else | |
{ | |
DropProduct(); | |
} | |
} | |
Console.WriteLine($"Client № {Id} does not have enough money to paid for anything and just left empty."); | |
return 0; | |
} | |
private int CalculateBascketPriceSum() | |
{ | |
int bascketPrice=0; | |
foreach (Product product in _basket) | |
{ | |
bascketPrice += product.Price; | |
} | |
return bascketPrice; | |
} | |
private void TransferFromBascketToBag() | |
{ | |
foreach (Product product in _basket) | |
{ | |
_bag.Add(product); | |
} | |
_basket.Clear(); | |
} | |
private void DropProduct() | |
{ | |
Console.WriteLine($"Client № {Id} does not have enough money to paid for his basket " + | |
$"and he return 1 product to market."); | |
_basket.RemoveAt(Utils.GenerateRandomNumber(0, _basket.Count - 1)); | |
} | |
} | |
class Utils | |
{ | |
private static Random s_random = new Random(); | |
public static int GenerateRandomNumber(int min, int max) | |
{ | |
return s_random.Next(min, max + 1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment