Vapio

task33

May 30th, 2021
1,167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4.  
  5. public class Program
  6. {
  7.     public static void Main()
  8.     {
  9.         int timeSleep = 700;
  10.         Shop shop = new Shop();
  11.  
  12.         while (shop.IsOver() != true)
  13.         {
  14.             shop.Update();
  15.             Thread.Sleep(timeSleep);
  16.         }
  17.     }
  18. }
  19.  
  20. public class Shop
  21. {
  22.     private Customer _customer;
  23.     private Random _random;
  24.     private int _amountClients;
  25.     private int _amountMoneyCashBox;
  26.  
  27.     private int _amountItemsMinimum = 5;
  28.     private int _amountItemsMaximum = 20;
  29.     private int _priceItemMinimum = 10;
  30.     private int _priceItemMaximum = 400;
  31.     private int _walletCustomerMinimum = 100;
  32.     private int _walletCustomerMaximum = 2500;
  33.     private int _amountClientsMinimum = 10;
  34.     private int _amountClientsMaximum = 100;
  35.  
  36.     private string[] _namesItems =
  37.     {
  38.         "Milk", "Bread", "Sausages",
  39.         "Cheese", "Butter", "Chocolate nuts"
  40.     };
  41.  
  42.     public Shop()
  43.     {
  44.         _random = new Random();
  45.         _amountMoneyCashBox = 0;
  46.         _amountClients = _random.Next(_amountClientsMinimum, _amountClientsMaximum);
  47.         CreateCustomer();
  48.     }
  49.  
  50.     public void Update()
  51.     {
  52.         PrintCustomers();
  53.         TryTrade();
  54.     }
  55.  
  56.     public void PrintCustomers()
  57.     {
  58.         Console.Clear();
  59.         Console.WriteLine("Customers stay in order : " + _amountClients);
  60.         Console.WriteLine("Money in cashbox : " + _amountMoneyCashBox);
  61.         Console.WriteLine(_customer);
  62.     }
  63.  
  64.     public void TryTrade()
  65.     {
  66.         if (_customer.PriceTotalItemes <= _customer.Wallet)
  67.         {
  68.             Console.WriteLine("Client bought items");
  69.             _amountMoneyCashBox += _customer.PriceTotalItemes;
  70.             CreateCustomer();
  71.         }
  72.         else
  73.         {
  74.             if (_customer.IsEmptyItems())
  75.             {
  76.                 Console.WriteLine("Customer haven't items to buy.");
  77.                 CreateCustomer();
  78.             }
  79.             else
  80.             {
  81.                 Console.WriteLine("Customer haven't enought money.");
  82.                 int indexItem = _random.Next(0, _customer.AmountItems);
  83.                 _customer.RemoveItem(indexItem);
  84.             }
  85.         }
  86.     }
  87.  
  88.     public bool IsOver()
  89.     {
  90.         return _amountClients < 0;
  91.     }
  92.  
  93.     private void CreateCustomer()
  94.     {
  95.         --_amountClients;
  96.         int amountItems = _random.Next(_amountItemsMinimum, _amountItemsMaximum);
  97.         int wallet = _random.Next(_walletCustomerMinimum, _walletCustomerMaximum);
  98.         _customer = new Customer(wallet);
  99.         string name;
  100.         int price;
  101.         Item item;
  102.  
  103.         for (int i = 0; i < amountItems; ++i)
  104.         {
  105.             name = _namesItems[_random.Next(0, _namesItems.Length)];
  106.             price = _random.Next(_priceItemMinimum, _priceItemMaximum);
  107.             item = new Item(name, price);
  108.             _customer.AddItem(item);
  109.         }
  110.     }
  111. }
  112.  
  113. public class Customer
  114. {
  115.     private List<Item> _items;
  116.  
  117.     public int Wallet { get; private set; }
  118.     public int PriceTotalItemes { get; private set; }
  119.     public int AmountItems => _items.Count;
  120.  
  121.     public Customer(int wallet)
  122.     {
  123.         Wallet = wallet;
  124.         _items = new List<Item>();
  125.     }
  126.  
  127.     public override string ToString()
  128.     {
  129.         string result = "";
  130.         result += "Wallet : " + Wallet + " \n";
  131.         result += "Total price of items : " + PriceTotalItemes + " \n";
  132.  
  133.         for (int i = 0; i < _items.Count; ++i)
  134.             result += String.Format("{0}. {1} \n", i, _items[i]);
  135.  
  136.         return result;
  137.     }
  138.  
  139.     public void AddItem(Item item)
  140.     {
  141.         _items.Add(item);
  142.         PriceTotalItemes += item.Price;
  143.     }
  144.  
  145.     public void RemoveItem(int index)
  146.     {
  147.         PriceTotalItemes -= _items[index].Price;
  148.         _items.RemoveAt(index);
  149.     }
  150.  
  151.     public bool IsEmptyItems()
  152.     {
  153.         return _items.Count <= 0;
  154.     }
  155.  
  156. }
  157.  
  158. public class Item
  159. {
  160.     public string Name { get; private set; }
  161.     public int Price { get; private set; }
  162.  
  163.     public Item(string name, int price)
  164.     {
  165.         Name = name;
  166.         Price = price;
  167.     }
  168.  
  169.     public Item(Item item)
  170.     {
  171.         Name = item.Name;
  172.         Price = item.Price;
  173.     }
  174.  
  175.     public override string ToString()
  176.     {
  177.         return String.Format("{0} - {1:0.00} ", Name, Price);
  178.     }
  179. }
  180.  
Advertisement
Add Comment
Please, Sign In to add comment