Advertisement
OwlyOwl

superMarket

Jul 14th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.34 KB | None | 0 0
  1. using Microsoft.VisualBasic.CompilerServices;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6.  
  7. namespace Supermarket
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Random rand = new Random();
  14.             Shop shop = new Shop(0);
  15.             shop.GetProducts();
  16.  
  17.             Client client1 = new Client(rand.Next(300, 1000));
  18.             Client client2 = new Client(rand.Next(300, 1000));
  19.             Client client3 = new Client(rand.Next(300, 1000));
  20.             Client client4 = new Client(rand.Next(300, 1000));
  21.             Client client5 = new Client(rand.Next(300, 1000));
  22.  
  23.             client1.PutIntoCart(shop.GetRandomItem(), shop.GetRandomItem());
  24.             client2.PutIntoCart(shop.GetRandomItem(), shop.GetRandomItem());
  25.             client3.PutIntoCart(shop.GetRandomItem(), shop.GetRandomItem());
  26.             client4.PutIntoCart(shop.GetRandomItem(), shop.GetRandomItem());
  27.             client5.PutIntoCart(shop.GetRandomItem(), shop.GetRandomItem());
  28.  
  29.             shop.GetInLine(client1);
  30.             shop.GetInLine(client2);
  31.             shop.GetInLine(client3);
  32.             shop.GetInLine(client4);
  33.             shop.GetInLine(client5);
  34.             shop.Work();
  35.         }
  36.     }
  37.  
  38.     class Shop
  39.     {
  40.         public int Money { get; private set; }
  41.         private List<Product> _products = new List<Product>();
  42.         private Queue<Client> _queueLine = new Queue<Client>();
  43.  
  44.         public Shop(int money)
  45.         {
  46.             Money = money;
  47.             List<Product> _products = new List<Product>();
  48.             Queue<Client> _queue = new Queue<Client>();
  49.         }
  50.  
  51.         public void Work()
  52.         {
  53.             while (_queueLine.Count != 0)
  54.             {
  55.                 var currentClient = _queueLine.Dequeue();
  56.                 bool isPaid = false;
  57.                 while (isPaid == false)
  58.                 {
  59.                     if (currentClient.SumUpPrice() <= currentClient.Money)
  60.                     {
  61.                         currentClient.Pay(currentClient.SumUpPrice());
  62.                         Money += currentClient.SumUpPrice();
  63.                         Console.WriteLine("Клиент обслужен!");
  64.                         Console.WriteLine("Денег у магазина: " + Money);
  65.                         isPaid = true;
  66.                     }
  67.                     else
  68.                     {
  69.                         Console.WriteLine("Пришлось кое-что выкинуть...");
  70.                         currentClient.ThrowAway();
  71.                     }
  72.                 }
  73.             }
  74.         }
  75.  
  76.         public void GetProducts()
  77.         {
  78.             _products.Add(new Product("Milk", 55));
  79.             _products.Add(new Product("Bread", 45));
  80.             _products.Add(new Product("Caviar", 340));
  81.             _products.Add(new Product("Potatoe", 30));
  82.             _products.Add(new Product("Cereals", 200));
  83.         }
  84.  
  85.         public Product GetRandomItem()
  86.         {
  87.             Random rand = new Random();
  88.             return _products[rand.Next(0, 5)];
  89.         }
  90.  
  91.         public void GetInLine(Client client)
  92.         {
  93.             _queueLine.Enqueue(client);
  94.         }
  95.     }
  96.  
  97.     class Client
  98.     {
  99.         public int Money { get; private set; }
  100.  
  101.         private List<Product> _cart = new List<Product>();
  102.  
  103.         public Client(int money)
  104.         {
  105.             Money = money;
  106.             List<Product> _cart = new List<Product>();
  107.         }
  108.  
  109.         public void PutIntoCart(Product product1, Product product2)
  110.         {
  111.             _cart.Add(product1);
  112.             _cart.Add(product2);
  113.         }
  114.  
  115.         public int SumUpPrice()
  116.         {
  117.             int sum = 0;
  118.             foreach (var item in _cart)
  119.             {
  120.                 sum += item.Price;
  121.             }
  122.             return sum;
  123.         }
  124.  
  125.         public void Pay(int totalPrice)
  126.         {
  127.             Money -= totalPrice;
  128.         }
  129.  
  130.         public void ThrowAway()
  131.         {
  132.             Random rand = new Random();
  133.             _cart.RemoveAt(rand.Next(0, 1));
  134.         }
  135.     }
  136.  
  137.     class Product
  138.     {
  139.         public string Name { get; private set; }
  140.         public int Price { get; private set; }
  141.  
  142.         public Product(string name, int price)
  143.         {
  144.             Name = name;
  145.             Price = price;
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement