Last active
July 11, 2025 15:01
-
-
Save Zyuzildorf/b464f50107a098912ba9c87f720798a8 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 Supermarket | |
{ | |
internal class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
Supermarket supermarket = new Supermarket(); | |
supermarket.Work(); | |
} | |
} | |
class Supermarket | |
{ | |
private List<Product> _products; | |
private Queue<Client> _clients; | |
private ClientsFactory _factory; | |
private int _money; | |
public Supermarket() | |
{ | |
_products = new List<Product>(); | |
_clients = new Queue<Client>(); | |
_factory = new ClientsFactory(); | |
AddProducts(); | |
AddNewClients(); | |
} | |
public void Work() | |
{ | |
const int CommandServeClient = 1; | |
const int CommandAddClients = 2; | |
const int CommandAddProducts = 3; | |
const int CommandExit = 4; | |
bool isWork = true; | |
int userInput; | |
while (isWork) | |
{ | |
Console.Clear(); | |
Console.WriteLine("Здравствуйте! Это программа администрирования клиентов.\n"); | |
ShowAllProducts(); | |
Console.WriteLine($"\nКлиентов в очереди: {_clients.Count}."); | |
if (_money > 0) | |
{ | |
Console.WriteLine($"Выручка магазина: {_money}$"); | |
} | |
Console.WriteLine($"\nДоступные команды:\n[{CommandServeClient}] - Обслужить клиента." + | |
$"\n[{CommandAddClients}] - Добавить новых клиентов." + | |
$"\n[{CommandAddProducts}] - Добавить новые продукты." + | |
$"\n[{CommandExit}] - Выйти из программы."); | |
Console.Write("\nВвод: "); | |
userInput = UserUtils.ReadInt(); | |
switch (userInput) | |
{ | |
case CommandServeClient: | |
ServeClient(); | |
break; | |
case CommandAddClients: | |
AddNewClients(); | |
break; | |
case CommandAddProducts: | |
AddProducts(); | |
break; | |
case CommandExit: | |
isWork = false; | |
break; | |
default: | |
Console.WriteLine("Такой команды не существует"); | |
break; | |
} | |
} | |
} | |
private void ServeClient() | |
{ | |
Product product; | |
bool isClientFinished = false; | |
if (_clients.Count == 0) | |
{ | |
Console.WriteLine("Нет клиентов в очереди."); | |
UserUtils.ContinueProgram(); | |
return; | |
} | |
Client currentClient = _clients.Dequeue(); | |
while (isClientFinished == false) | |
{ | |
Console.Clear(); | |
Console.WriteLine("Обслуживание клиента.\n"); | |
currentClient.ShowInfo(); | |
if (currentClient.Cart.Count <= 0) | |
{ | |
Console.WriteLine("\nКлиент обслужен."); | |
UserUtils.ContinueProgram(); | |
isClientFinished = true; | |
} | |
else | |
{ | |
Console.Write("\nНажмите клавишу, чтобы продать товар: "); | |
Console.ReadKey(); | |
bool isClientCanPay = currentClient.CanPayForProduct(out product); | |
if (isClientCanPay) | |
{ | |
int income = currentClient.BuyProduct(product); | |
SellProduct(income); | |
} | |
else | |
{ | |
currentClient.DeleteRandomProduct(); | |
} | |
} | |
} | |
} | |
private void SellProduct(int productCost) | |
{ | |
_money += productCost; | |
Console.WriteLine("Товар продан."); | |
} | |
private void ShowAllProducts() | |
{ | |
Console.WriteLine("Все продукты супермаркета:"); | |
foreach (var product in _products) | |
{ | |
product.ShowInfo(); | |
} | |
} | |
private void AddNewClients() | |
{ | |
List<Client> newClients = _factory.CreateClients(_products); | |
for (int i = 0; i < newClients.Count; i++) | |
{ | |
_clients.Enqueue(newClients[i]); | |
} | |
} | |
private void AddProducts() | |
{ | |
Console.Clear(); | |
Console.WriteLine("Добавление продуктов в супермаркет."); | |
Console.Write("\nВведите количество добавляемых продуктов: "); | |
int countOfProducts = UserUtils.ReadPositiveInt(); | |
for (int i = 0; i < countOfProducts; i++) | |
{ | |
AddProduct(); | |
} | |
Console.WriteLine("\nПродукты успешно добавлены."); | |
UserUtils.ContinueProgram(); | |
} | |
private void AddProduct() | |
{ | |
Console.Write("\nВведите название продукта: "); | |
string nameOfProduct = UserUtils.ReadString(); | |
Console.Write("Введите стоимость продукта: "); | |
int costOfProduct = UserUtils.ReadInt(); | |
_products.Add(new Product(nameOfProduct, costOfProduct)); | |
} | |
} | |
class ClientsFactory | |
{ | |
private int _minMoneyAmount; | |
private int _maxMoneyAmount; | |
public ClientsFactory() | |
{ | |
_minMoneyAmount = 0; | |
_maxMoneyAmount = 5000; | |
} | |
public List<Client> CreateClients(List<Product> products) | |
{ | |
List<Client> clients = new List<Client>(); | |
Console.Clear(); | |
Console.WriteLine("Добавление новых клиентов в супермаркет."); | |
Console.Write("\nВведите количество новых клиентов: "); | |
int clientsCount = UserUtils.ReadInt(); | |
for (int i = 0; i < clientsCount; i++) | |
{ | |
clients.Add(new Client(GetRandomListOfProducts(products), | |
UserUtils.GenerateRandomNumber(_minMoneyAmount, _maxMoneyAmount))); | |
} | |
Console.WriteLine("\nКлиенты успешно добавлены."); | |
UserUtils.ContinueProgram(); | |
return clients; | |
} | |
private List<Product> GetRandomListOfProducts(List<Product> products) | |
{ | |
List<Product> cartProducts = new List<Product>(); | |
if (products.Count <= 1) | |
{ | |
cartProducts.Add(products[products.Count - 1]); | |
} | |
else | |
{ | |
int minProducts = 1; | |
int maxProducts = products.Count; | |
int productsInCartCount = UserUtils.GenerateRandomNumber(minProducts, maxProducts); | |
for (int i = 0; i < productsInCartCount; i++) | |
{ | |
cartProducts.Add(products[UserUtils.GenerateRandomNumber(minProducts - 1, maxProducts)]); | |
} | |
} | |
return cartProducts; | |
} | |
} | |
class Client | |
{ | |
private List<Product> _bag; | |
private List<Product> _cart; | |
private int _money; | |
public Client(List<Product> products, int money) | |
{ | |
_bag = new List<Product>(); | |
_money = money; | |
_cart = new List<Product>(products); | |
} | |
public List<Product> Cart => new List<Product>(_cart); | |
public bool CanPayForProduct(out Product product) | |
{ | |
bool isEnoughMoney; | |
product = _cart[_cart.Count - 1]; | |
if (_money >= product.Cost) | |
{ | |
isEnoughMoney = true; | |
} | |
else | |
{ | |
isEnoughMoney = false; | |
} | |
return isEnoughMoney; | |
} | |
public void DeleteRandomProduct() | |
{ | |
int minRandom = 0; | |
int randomProductIndex = UserUtils.GenerateRandomNumber(minRandom, _cart.Count - 1); | |
_cart.RemoveAt(randomProductIndex); | |
Console.WriteLine("\nПокупателю не хватило денег и он на панике выложил " + | |
"случайный продукт из корзины"); | |
Console.ReadKey(); | |
} | |
public int BuyProduct(Product product) | |
{ | |
_money -= product.Cost; | |
_cart.Remove(product); | |
_bag.Add(product); | |
return product.Cost; | |
} | |
public void ShowInfo() | |
{ | |
ShowMoney(); | |
ShowProducts(_cart, "Продукты в корзине покупателя: "); | |
Console.WriteLine(); | |
ShowProducts(_bag, "Продукты в сумке покупателя: "); | |
} | |
private void ShowProducts(List<Product> products, string text) | |
{ | |
Console.WriteLine(text); | |
foreach (var product in products) | |
{ | |
product.ShowInfo(); | |
} | |
} | |
private void ShowMoney() | |
{ | |
Console.WriteLine($"Деньги в кошельке покупателя: {_money}\n"); | |
} | |
} | |
class Product | |
{ | |
private string _name; | |
public Product(string name, int cost) | |
{ | |
_name = name; | |
Cost = cost; | |
} | |
public int Cost { get; private set; } | |
public void ShowInfo() | |
{ | |
Console.WriteLine($"{_name} - {Cost}$"); | |
} | |
} | |
public class UserUtils | |
{ | |
private static Random s_random = new Random(); | |
public static int GenerateRandomNumber(int min, int max) | |
{ | |
int number = s_random.Next(min, max); | |
return number; | |
} | |
public static int ReadPositiveInt() | |
{ | |
int correctIntValue = ReadInt(); | |
while (correctIntValue <= 0) | |
{ | |
Console.Write("Ошибка ввода. Введите значение больше нуля: "); | |
correctIntValue = ReadInt(); | |
} | |
return correctIntValue; | |
} | |
public static int ReadInt() | |
{ | |
int number; | |
string userInput = Console.ReadLine(); | |
while (int.TryParse(userInput, out number) == false) | |
{ | |
Console.Write("Ошибка ввода. Повторите попытку: "); | |
userInput = Console.ReadLine(); | |
} | |
return number; | |
} | |
public static string ReadString() | |
{ | |
string userInput = Console.ReadLine(); | |
while (userInput == string.Empty) | |
{ | |
Console.Write("Ошибка ввода."); | |
userInput = Console.ReadLine(); | |
} | |
return userInput; | |
} | |
public static void ContinueProgram() | |
{ | |
Console.Write("\nНажмите любую клавишу для продолжения: "); | |
Console.ReadKey(); | |
Console.Clear(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment