-
-
Save GigaOrts/c058c4a2a85d8ed7a925c74c9e51b561 to your computer and use it in GitHub Desktop.
This file contains 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; | |
using System.Linq; | |
using System.Net.NetworkInformation; | |
using System.Runtime.CompilerServices; | |
namespace iJunior | |
{ | |
class MainClass | |
{ | |
public static void Main(string[] args) | |
{ | |
SuperMarket superMarket = new SuperMarket(); | |
superMarket.ReceivCustomers(); | |
} | |
} | |
class SuperMarket | |
{ | |
private List<Product> _products = new List<Product>(); | |
private Random _random = new Random(); | |
private Queue<Client> _clients = new Queue<Client>(); | |
private int _money = 0; | |
public SuperMarket() | |
{ | |
_products.Add(new Product("Сыр", 149)); | |
_products.Add(new Product("Колбаса", 239)); | |
_products.Add(new Product("Хлеб", 99)); | |
_products.Add(new Product("Молоко", 89)); | |
_products.Add(new Product("Яйца", 119)); | |
_products.Add(new Product("Картофель 1кг", 78)); | |
_products.Add(new Product("Шоколад", 129)); | |
_products.Add(new Product("Лимонад", 119)); | |
_products.Add(new Product("Печенье", 47)); | |
_products.Add(new Product("Мясо", 339)); | |
_products.Add(new Product("Гречка", 79)); | |
_products.Add(new Product("Макароны", 79)); | |
} | |
public void ReceivCustomers() | |
{ | |
AddQueueClients(); | |
ServeClients(_clients); | |
} | |
private void ServeClients(Queue<Client> clients) | |
{ | |
bool isQueueServed = true; | |
int customerNumber = 1; | |
while (isQueueServed) | |
{ | |
Client client = clients.Dequeue(); | |
Console.WriteLine($"Покупатель {customerNumber})"); | |
for (int i = 0; i < client.NumbersOfItemsInBasket; i++) | |
{ | |
SellProducts(client.TakeRandomProduct(), client); | |
} | |
if (clients.Count() == 0) | |
{ | |
Console.WriteLine("Все посетители обслужены"); | |
isQueueServed = false; | |
Console.ReadKey(); | |
} | |
Console.WriteLine(); | |
customerNumber++; | |
} | |
} | |
private void AddQueueClients() | |
{ | |
int minClients = 5; | |
int maxClients = 15; | |
int clientsInQueue = _random.Next(minClients, maxClients); | |
for (int i = 0; i < clientsInQueue; i++) | |
{ | |
Client client = new Client(_products); | |
_clients.Enqueue(client); | |
} | |
} | |
private void Sell(Product product, Client client) | |
{ | |
Console.WriteLine($"Продано {product.Name} по цене {product.Cost}"); | |
client.BuyProduct(product); | |
_money += product.Cost; | |
Console.ReadKey(); | |
} | |
private bool CheckEnoughCLientMoney(Product product, Client client) | |
{ | |
return product.Cost >= client.Money; | |
} | |
private void SellProducts(Product product, Client client) | |
{ | |
if (CheckEnoughCLientMoney(product, client)) | |
{ | |
client.RemoveProduct(); | |
} | |
else | |
{ | |
Sell(product, client); | |
} | |
} | |
} | |
class Client | |
{ | |
private int _minProducts = 5; | |
private int _maxProducts = 15; | |
private int _minMoney = 100; | |
private int _maxMoney = 500; | |
private List<Product> _shoppingList = new List<Product>(); | |
private List<Product> _purchasedProducts = new List<Product>(); | |
public int Money { get; private set; } | |
public int NumbersOfItemsInBasket { get; private set; } | |
public Client(List<Product> products) | |
{ | |
Random random = new Random(); | |
Money = random.Next(_minMoney, _maxMoney); | |
FillBasket(products); | |
NumbersOfItemsInBasket = _shoppingList.Count(); | |
} | |
public void RemoveProduct() | |
{ | |
_shoppingList.Remove(TakeRandomProduct()); | |
} | |
public void BuyProduct(Product product) | |
{ | |
_shoppingList.Remove(product); | |
_purchasedProducts.Add(product); | |
Money -= product.Cost; | |
} | |
public Product TakeRandomProduct() | |
{ | |
Random random = new Random(); | |
int randomIndex = random.Next(0, _shoppingList.Count() - 1); | |
int index = 0; | |
foreach (var product in _shoppingList) | |
{ | |
if (index == randomIndex) | |
{ | |
return product; | |
} | |
index++; | |
} | |
return _shoppingList[0]; | |
} | |
private void FillBasket(List<Product> products) | |
{ | |
Random random = new Random(); | |
int countOfProducts = random.Next(_minProducts, _maxProducts); | |
for (int i = 0; i < countOfProducts; i++) | |
{ | |
_shoppingList.Add(TakeProduct(products)); | |
} | |
} | |
private Product TakeProduct(List<Product> products) | |
{ | |
Random random = new Random(); | |
int firstProduct = 0; | |
int lastProduct = products.Count(); | |
int numberOfProduc = random.Next(firstProduct, lastProduct); | |
return products[numberOfProduc]; | |
} | |
} | |
class Product | |
{ | |
public string Name { get; private set; } | |
public int Cost { get; private set; } | |
public Product(string name, int cost) | |
{ | |
Name = name; | |
Cost = cost; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment