W1thr

ООП-7

Mar 13th, 2021 (edited)
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Homework7
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Supermarket supermarket = new Supermarket();
  11.             supermarket.ServeCustomers();
  12.         }
  13.     }
  14.     class Product
  15.     {
  16.         public string Label { get; private set; }
  17.         public int Price { get; private set; }
  18.  
  19.         public Product(string label, int price)
  20.         {
  21.             Label = label;
  22.             Price = price;
  23.         }
  24.     }
  25.  
  26.     class Customer
  27.     {
  28.         private List<Product> _basket;
  29.         private int _money;
  30.  
  31.         public Customer(List<Product> basket, int money)
  32.         {
  33.             _basket = basket;
  34.             _money = money;
  35.         }
  36.  
  37.         public void LayOutProducts()
  38.         {
  39.             int sum = CalculateSum();
  40.             bool enoughMoney = sum < _money;
  41.  
  42.             if (enoughMoney == true)
  43.             {
  44.                 Console.WriteLine($"У поситителя {_money} денег. Его покупка на {sum} и он ее оплачивает");
  45.             }
  46.             else
  47.             {
  48.                 int tempSum = sum;
  49.                 Console.WriteLine($"У поситителя {_money} денег. Его покупка на {sum}. Ему не хватает {sum - _money}");
  50.                 RemoveProducts(sum);
  51.                 sum = CalculateSum();
  52.                 Console.WriteLine($"Поситителю пришлось оставить товаров на {tempSum - sum}");
  53.             }
  54.  
  55.             Pay(sum);
  56.             Console.WriteLine($"У поситителя осталось {_money} денег.");
  57.  
  58.         }
  59.  
  60.         public int CalculateSum()
  61.         {
  62.             int sum = 0;
  63.  
  64.             foreach (var product in _basket)
  65.             {
  66.                 sum += product.Price;
  67.             }
  68.  
  69.             return sum;
  70.         }
  71.  
  72.         private void RemoveProducts(int sum)
  73.         {
  74.             Random random = new Random();
  75.  
  76.             while (sum >= _money)
  77.             {
  78.                 int randomIndex = random.Next(0, _basket.Count);
  79.  
  80.                 sum -= _basket[randomIndex].Price;
  81.                 _basket.RemoveAt(randomIndex);
  82.             }
  83.         }
  84.  
  85.         private void Pay(int sum)
  86.         {
  87.             _money -= sum;
  88.         }
  89.     }
  90.  
  91.     class Supermarket
  92.     {
  93.         List<Product> _products;
  94.         List<Customer> _customers;
  95.  
  96.         public Supermarket()
  97.         {
  98.             _products = new List<Product>
  99.          {
  100.                 new Product("a",20),
  101.                 new Product("b",40),
  102.                 new Product("c",60),
  103.                 new Product("d",80),
  104.                 new Product("e",100),
  105.                 new Product("f",120),
  106.          };
  107.  
  108.             _customers = new List<Customer>
  109.          {
  110.                 new Customer(new List<Product>{_products[0],_products[1],_products[0]},79),
  111.                 new Customer(new List<Product>{_products[2],_products[3],_products[5]},265),
  112.                 new Customer(new List<Product>{_products[2],_products[3],_products[5]},255),
  113.                 new Customer(new List<Product>{_products[0],_products[3],_products[5]},210),
  114.          };
  115.         }
  116.         public void ServeCustomers()
  117.         {
  118.             foreach (var customer in _customers)
  119.             {
  120.                 customer.LayOutProducts();
  121.                 Console.ReadKey();
  122.                 Console.Clear();
  123.             }
  124.         }
  125.     }
  126.  
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment