Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Task46._2
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Seller seller = new Seller();
- Supermarket supermarket = new Supermarket(seller);
- supermarket.Work();
- }
- }
- class Supermarket
- {
- private Seller _seller;
- private Queue<Customer> _customer = new Queue<Customer>();
- public Supermarket(Seller seller)
- {
- _seller = seller;
- }
- public void Work()
- {
- bool isExit = false;
- string welcomeJob =
- "Добро пожаловать на работу.";
- Console.WriteLine(welcomeJob);
- while (isExit == false)
- {
- CreateQueue();
- bool isActive = true;
- while (isActive)
- {
- Console.Write(
- "Перед вашей кассой очередь из " + _customer.Count + " человек(а)\n" +
- "Пробить - посчитать человека и принять оплату\n" +
- "Уйти - закрыть кассу и пойти домой\n" +
- "Введите команду: ");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case "Пробить":
- ClearanceSale();
- break;
- case "Уйти":
- Leave(ref isExit);
- break;
- default:
- Console.WriteLine("Вы вели не существующую команду.");
- break;
- }
- if (_customer.Count <= 0)
- {
- isActive = false;
- }
- }
- }
- }
- private void Leave(ref bool isExit)
- {
- _customer.Clear();
- isExit = true;
- }
- private void CreateQueue()
- {
- Random random = new Random();
- AddCustomerQueue(random);
- foreach (Customer customer in _customer)
- {
- GiveProductsCustomer(customer, random);
- }
- }
- private void GiveProductsCustomer(Customer customer, Random random)
- {
- Product product;
- int maxProductIndex = _seller.GetMaxIndexProduct();
- int minCountProductNeedCustomers = 1;
- int maxCountProductNeedCustomers = maxProductIndex;
- int indexChooseBuyProduct;
- int randCountProductBuyNeedCustomers;
- randCountProductBuyNeedCustomers = random.Next(minCountProductNeedCustomers, maxCountProductNeedCustomers);
- for (int i = 0; i < randCountProductBuyNeedCustomers; i++)
- {
- indexChooseBuyProduct = random.Next(0, maxProductIndex - 1);
- _seller.FindProduct(indexChooseBuyProduct, out product);
- customer.TakeProduct(product);
- }
- }
- private void ClearanceSale()
- {
- int totalPrice;
- int countProduct;
- bool isSell = false;
- bool haveGoodsInCart = true;
- Customer customer = _customer.Dequeue();
- countProduct = customer.GetCountProduct();
- if (countProduct == 0)
- {
- Console.WriteLine("Покупатель ничего не взял просто хотел посмотреть. ");
- }
- while (isSell == false && haveGoodsInCart)
- {
- Console.WriteLine(customer.Money);
- customer.ShowCart();
- customer.OutputTotalPrice(out totalPrice);
- Console.WriteLine(totalPrice);
- isSell = _seller.TrySellProduct(customer, totalPrice);
- if (isSell == false && countProduct > 0)
- {
- customer.DeleteProduct();
- Console.WriteLine("Покупатель решил убрать один из товаров. ");
- }
- else if (countProduct <= 0)
- {
- haveGoodsInCart = false;
- }
- countProduct = customer.GetCountProduct();
- }
- if (countProduct == 0)
- {
- Console.WriteLine("Клиент не сумел ничего приобрести и ушёл с пустыми руками. ");
- }
- }
- private void AddCustomerQueue(Random random)
- {
- int indexCustomer = 0;
- int minValueCountCustomersQueue = 1;
- int maxValueCountCustomersQueue = 10;
- int valueCountCustomersQueue = random.Next(minValueCountCustomersQueue, maxValueCountCustomersQueue);
- int maxValueMoneyCustomer = 500;
- int minValueMoneyCustomer = 50;
- int moneyCustomer;
- for (int i = 0; i < valueCountCustomersQueue; i++)
- {
- moneyCustomer = random.Next(minValueMoneyCustomer, maxValueMoneyCustomer);
- _customer.Enqueue(new Customer(moneyCustomer, indexCustomer));
- indexCustomer++;
- }
- }
- }
- class Human
- {
- protected List<Product> Products = new List<Product>();
- public int Money { get; private set; }
- public Human(int money)
- {
- Money = money;
- }
- public void ShowInfoProduct()
- {
- if (Products.Count > 0)
- {
- Console.WriteLine("Список: ");
- foreach (Product item in Products)
- {
- item.ShowIndo();
- }
- }
- else
- {
- Console.WriteLine("Пусто.");
- }
- }
- }
- class Seller : Human
- {
- public Seller(int money = 0) : base(money)
- {
- int indexProduct = -1;
- Products.Add(new Product(indexProduct++, "Кола", 30));
- Products.Add(new Product(indexProduct++, "Пельмени", 150));
- Products.Add(new Product(indexProduct++, "Хлеб", 15));
- Products.Add(new Product(indexProduct++, "Мороженное", 90));
- Products.Add(new Product(indexProduct++, "Жвачка", 35));
- Products.Add(new Product(indexProduct++, "Молоко", 70));
- }
- public int GetMaxIndexProduct()
- {
- return Products.Count;
- }
- public void ShowShelf()
- {
- Console.WriteLine("У меня доступны следующие товары: ");
- foreach (var item in Products)
- {
- Console.WriteLine(
- $"=========================================\n" +
- $"Название продукта - {item.Name}\n" +
- $"Цена за 1 шт. ----- {item.Price}\n" +
- $"===========================================");
- }
- Console.WriteLine();
- }
- public void FindProduct(int indexProduct, out Product product)
- {
- product = null;
- foreach (Product findProduct in Products)
- {
- if (findProduct.Index == indexProduct)
- {
- product = findProduct;
- }
- }
- }
- public bool TrySellProduct(Customer customer, int totalPrice)
- {
- if (totalPrice < customer.Money && customer.GetCountProduct() > 0)
- {
- Console.WriteLine("Покупателю хватило денег и он успешно оплатил.");
- return true;
- }
- else if (customer.GetCountProduct() > 0)
- {
- Console.WriteLine("Покупателю не хватило денег. ");
- return false;
- }
- else
- {
- return false;
- }
- }
- }
- class Customer : Human
- {
- public int Index { get; private set; }
- public Customer(int money, int index) : base(money)
- {
- Index = index;
- }
- public void ShowCart()
- {
- if (Products.Count > 0)
- {
- Console.WriteLine("Товары в карзине: ");
- foreach (var item in Products)
- {
- Console.WriteLine(
- $"=========================================\n" +
- $"Название продукта - {item.Name}\n" +
- $"Цена за 1 шт. ----- {item.Price}\n" +
- $"===========================================");
- }
- }
- else
- {
- Console.WriteLine("Тележка пуста пуста. ");
- }
- }
- public void TakeProduct(Product findProduct)
- {
- Products.Add(findProduct);
- }
- public void OutputTotalPrice(out int totalPrice)
- {
- totalPrice = 0;
- foreach (var item in Products)
- {
- totalPrice += item.Price;
- }
- }
- public int GetCountProduct()
- {
- return Products.Count;
- }
- public void DeleteProduct()
- {
- Random rand = new Random();
- Console.WriteLine(Products.Count);
- if (Products.Count > 0)
- {
- int indexProduct = rand.Next(0, Products.Count);
- Products.RemoveAt(indexProduct);
- }
- else
- {
- Products.RemoveAt(0);
- }
- }
- }
- public class Product
- {
- public string Name { get; private set; }
- public int Price { get; private set; }
- public int Index { get; private set; }
- public Product(int index, string name, int price)
- {
- Index = index;
- Name = name;
- Price = price;
- }
- public void ShowIndo()
- {
- Console.WriteLine(
- $"=========================================\n" +
- $"Название продукта - {Name}\n" +
- $"Цена за 1 шт. ----- {Price}\n" +
- $"===========================================");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement