Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Homework7
- {
- class Program
- {
- static void Main(string[] args)
- {
- Supermarket supermarket = new Supermarket();
- supermarket.ServeCustomers();
- }
- }
- class Product
- {
- public string Label { get; private set; }
- public int Price { get; private set; }
- public Product(string label, int price)
- {
- Label = label;
- Price = price;
- }
- }
- class Customer
- {
- private List<Product> _basket;
- private int _money;
- public Customer(List<Product> basket, int money)
- {
- _basket = basket;
- _money = money;
- }
- public void LayOutProducts()
- {
- int sum = CalculateSum();
- bool enoughMoney = sum < _money;
- if (enoughMoney == true)
- {
- Console.WriteLine($"У поситителя {_money} денег. Его покупка на {sum} и он ее оплачивает");
- }
- else
- {
- int tempSum = sum;
- Console.WriteLine($"У поситителя {_money} денег. Его покупка на {sum}. Ему не хватает {sum - _money}");
- RemoveProducts(sum);
- sum = CalculateSum();
- Console.WriteLine($"Поситителю пришлось оставить товаров на {tempSum - sum}");
- }
- Pay(sum);
- Console.WriteLine($"У поситителя осталось {_money} денег.");
- }
- public int CalculateSum()
- {
- int sum = 0;
- foreach (var product in _basket)
- {
- sum += product.Price;
- }
- return sum;
- }
- private void RemoveProducts(int sum)
- {
- Random random = new Random();
- while (sum >= _money)
- {
- int randomIndex = random.Next(0, _basket.Count);
- sum -= _basket[randomIndex].Price;
- _basket.RemoveAt(randomIndex);
- }
- }
- private void Pay(int sum)
- {
- _money -= sum;
- }
- }
- class Supermarket
- {
- List<Product> _products;
- List<Customer> _customers;
- public Supermarket()
- {
- _products = new List<Product>
- {
- new Product("a",20),
- new Product("b",40),
- new Product("c",60),
- new Product("d",80),
- new Product("e",100),
- new Product("f",120),
- };
- _customers = new List<Customer>
- {
- new Customer(new List<Product>{_products[0],_products[1],_products[0]},79),
- new Customer(new List<Product>{_products[2],_products[3],_products[5]},265),
- new Customer(new List<Product>{_products[2],_products[3],_products[5]},255),
- new Customer(new List<Product>{_products[0],_products[3],_products[5]},210),
- };
- }
- public void ServeCustomers()
- {
- foreach (var customer in _customers)
- {
- customer.LayOutProducts();
- Console.ReadKey();
- Console.Clear();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment