Skip to content

Instantly share code, notes, and snippets.

@Dead-in-side
Last active April 23, 2024 14:50
Show Gist options
  • Save Dead-in-side/82322bc5a99e0e7c28f80c760b689c29 to your computer and use it in GitHub Desktop.
Save Dead-in-side/82322bc5a99e0e7c28f80c760b689c29 to your computer and use it in GitHub Desktop.
namespace IJunior
{
public class Program
{
private static void Main(string[] args)
{
Supermarket supermarket = new Supermarket();
supermarket.Start();
}
}
public enum ConsoleCommand
{
AddProducts = 1,
AddCustomers,
ServeCustomer,
Exit
}
public class Supermarket
{
private Queue<Customer> _customers = new Queue<Customer>();
private List<Product> _products = new List<Product>();
public int Money { get; private set; } = 0;
public void Start()
{
bool isOpen = true;
Console.WriteLine("Добро пожаловать в Управление Супермаркетом");
while (isOpen)
{
ShowInfo();
Console.WriteLine((int)ConsoleCommand.AddProducts + ". Добавить новые товары");
Console.WriteLine((int)ConsoleCommand.AddCustomers + ". Добавть новых клиентов");
Console.WriteLine((int)ConsoleCommand.ServeCustomer + ". Обслужить клиента");
Console.WriteLine((int)ConsoleCommand.Exit + ". Выход");
if (int.TryParse(Console.ReadLine(), out int userInput))
{
switch (userInput)
{
case (int)ConsoleCommand.AddProducts:
AddProducts();
break;
case (int)ConsoleCommand.AddCustomers:
AddCustomers();
break;
case (int)ConsoleCommand.ServeCustomer:
ServeCustomer();
break;
case (int)ConsoleCommand.Exit:
isOpen = false;
break;
default:
Console.WriteLine("Некорректный ввод");
break;
}
}
Console.ReadLine();
Console.Clear();
}
}
private void ServeCustomer()
{
if (_customers.Count > 0)
{
Customer client = _customers.Dequeue();
bool hePaid = false;
while (hePaid == false)
{
if (client.TryBuyProducts(out int purchaseAmount))
{
Money += purchaseAmount;
hePaid = true;
}
}
}
}
private void AddProducts()
{
Console.WriteLine("Сколько видов товаров прибыло сегодня?");
if (int.TryParse(Console.ReadLine(), out int numberProducts) && numberProducts >= 0)
{
for (int i = numberProducts; i > 0; i--)
{
Console.WriteLine("Введите наименование прибывшего товара");
string name = Console.ReadLine();
Console.WriteLine("Установите цену: ");
if (int.TryParse(Console.ReadLine(), out int price) && price >= 0)
{
_products.Add(new Product(name, price));
}
else
{
Console.WriteLine("Некорректный ввод");
}
}
}
}
private void AddCustomers()
{
Console.WriteLine("Введите кол-во прибывших клиентов");
if (int.TryParse(Console.ReadLine(), out int numberCustomers) && numberCustomers >= 0)
{
for (int i = 0; i < numberCustomers; i++)
{
Customer customer = new Customer();
customer.ChooseProducts(_products);
_customers.Enqueue(customer);
}
}
}
private void ShowInfo()
{
Console.WriteLine("Товары: ");
for (int i = 0; i < _products.Count; i++)
{
int number = i + 1;
string info = _products[i].ShowInfo();
Console.WriteLine(number + ". " + info);
}
Console.WriteLine($"Клентов в очереди: {_customers.Count}.\nКол-во денег в кассе: " + Money);
}
}
public class Customer
{
private Random _random = new Random();
private List<Product> _boughtProducts = new List<Product>();
private List<Product> _productsInBasket = new List<Product>();
public Customer()
{
int lowRandomLimit = 50;
int highRandomLimit = 100;
Money = _random.Next(lowRandomLimit, highRandomLimit);
}
public int Money { get; private set; }
public void ChooseProducts(List<Product> products)
{
int lowRandomLimit = 0;
int highRandomLimit = products.Count;
for (int i = 0; i < _random.Next(lowRandomLimit, highRandomLimit); i++)
{
_productsInBasket.Add(products[_random.Next(lowRandomLimit, highRandomLimit)]);
}
}
public bool TryBuyProducts(out int price)
{
price = 0;
foreach (var product in _productsInBasket)
{
price += product.Price;
}
bool moneyEnough = true;
if (Money > price)
{
BuyProducts(price);
return moneyEnough;
}
else
{
Console.WriteLine("Недостаточно средств");
RemoveRandomProduct();
return moneyEnough = false;
}
}
private void BuyProducts( int price)
{
foreach (var product in _productsInBasket)
{
_boughtProducts.Add(product);
}
_productsInBasket.Clear();
Money -= price;
}
private void RemoveRandomProduct()
{
int lowRandomLimit = 0;
int highRandomLimit = _productsInBasket.Count;
_productsInBasket.Remove(_productsInBasket[_random.Next(lowRandomLimit, highRandomLimit)]);
}
}
public class Product
{
public Product(string name, int price)
{
Name = name;
Price = price;
}
public string Name { get; }
public int Price { get; }
public string ShowInfo() => Name + ": " + Price + " монет";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment