Askor

Hw34

Dec 1st, 2021 (edited)
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Shop
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Shop shop = new Shop();
  14.             shop.ShowCustomersInfo();
  15.             Console.WriteLine();
  16.             shop.Work();
  17.  
  18.             Console.ReadKey();
  19.         }
  20.     }
  21.  
  22.     class Product
  23.     {
  24.         public string Name { get; private set; }
  25.         public int Price { get; private set; }
  26.  
  27.         public Product(string name, int price)
  28.         {
  29.             Name = name;
  30.             Price = price;
  31.         }
  32.     }
  33.  
  34.     class Customer
  35.     {
  36.         private List<Product> _products = new List<Product>();
  37.  
  38.         public int Cash { get; private set; }
  39.  
  40.         public Customer(int cash)
  41.         {
  42.             Fill();
  43.             Cash = cash;
  44.         }
  45.  
  46.         public int GetPrice()
  47.         {
  48.             int finalPrice = 0;
  49.  
  50.             foreach (Product product in _products)
  51.             {
  52.                 finalPrice += product.Price;
  53.             }
  54.  
  55.             return finalPrice;
  56.         }
  57.  
  58.         public void RemoveProduct()
  59.         {
  60.             Random random = new Random();
  61.             int productIndex;
  62.  
  63.             productIndex = random.Next(0, _products.Count);
  64.  
  65.             _products.RemoveAt(productIndex);
  66.         }
  67.  
  68.         public void ShowInfo()
  69.         {
  70.             foreach (Product product in _products)
  71.             {
  72.                 Console.WriteLine($"{product.Name} | Price - {product.Price}");
  73.             }
  74.         }
  75.  
  76.         public void BuyProducts(int price)
  77.         {
  78.             Cash -= price;
  79.         }
  80.  
  81.         private void Fill()
  82.         {
  83.             _products.Add(new Product("Milk", 15));
  84.             _products.Add(new Product("Eggs", 25));
  85.             _products.Add(new Product("Meat", 40));
  86.             _products.Add(new Product("Fish", 30));
  87.             _products.Add(new Product("Sushi", 17));
  88.             _products.Add(new Product("Bread", 27));
  89.         }
  90.     }
  91.  
  92.     class Shop
  93.     {
  94.         private List<Customer> _customers = new List<Customer>();
  95.  
  96.         public Shop()
  97.         {
  98.             CreateQueue(5);
  99.         }
  100.  
  101.         private void CreateQueue(int quantity)
  102.         {
  103.             Random random = new Random();
  104.  
  105.             for (int i = 0; i < quantity; i++)
  106.             {
  107.                 _customers.Add(new Customer(random.Next(100,200)));
  108.             }
  109.  
  110.             Console.WriteLine($"Очередь из {quantity} человек");
  111.         }
  112.  
  113.         public void ShowCustomersInfo()
  114.         {
  115.             for (int i = 0; i < _customers.Count; i++)
  116.             {
  117.                 Console.WriteLine($"\n{i + 1} клиент. Cash - {_customers[i].Cash}");
  118.                 _customers[i].ShowInfo();
  119.             }
  120.             Console.WriteLine("\nДля старта кассы нажмите любую кнопку...");
  121.             Console.ReadLine();
  122.         }
  123.  
  124.         public void Work()
  125.         {
  126.             Console.WriteLine();
  127.             int counter = 1;
  128.  
  129.             foreach (Customer customer in _customers)
  130.             {
  131.                 Console.Clear();
  132.                 Console.WriteLine($"{counter} клиент. Кол-во cash - {customer.Cash}");
  133.                 counter++;
  134.  
  135.                 while (customer.Cash < customer.GetPrice())
  136.                 {
  137.                     customer.RemoveProduct();
  138.                 }
  139.                
  140.                 Console.WriteLine($"Список покупок на сумму {customer.GetPrice()}:");
  141.                 customer.BuyProducts(customer.GetPrice());
  142.                 customer.ShowInfo();
  143.                 Console.WriteLine($"Остаток на счёте: {customer.Cash}");
  144.  
  145.                 Console.WriteLine("\nДля следующего покупателя нажмите любую кнопку...");
  146.                 Console.ReadLine();
  147.             }
  148.         }
  149.     }
  150. }
Add Comment
Please, Sign In to add comment