Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.VisualBasic.CompilerServices;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Supermarket
- {
- class Program
- {
- static void Main(string[] args)
- {
- Random rand = new Random();
- Shop shop = new Shop(0);
- shop.GetProducts();
- Client client1 = new Client(rand.Next(300, 1000));
- Client client2 = new Client(rand.Next(300, 1000));
- Client client3 = new Client(rand.Next(300, 1000));
- Client client4 = new Client(rand.Next(300, 1000));
- Client client5 = new Client(rand.Next(300, 1000));
- client1.PutIntoCart(shop.GetRandomItem(), shop.GetRandomItem());
- client2.PutIntoCart(shop.GetRandomItem(), shop.GetRandomItem());
- client3.PutIntoCart(shop.GetRandomItem(), shop.GetRandomItem());
- client4.PutIntoCart(shop.GetRandomItem(), shop.GetRandomItem());
- client5.PutIntoCart(shop.GetRandomItem(), shop.GetRandomItem());
- shop.GetInLine(client1);
- shop.GetInLine(client2);
- shop.GetInLine(client3);
- shop.GetInLine(client4);
- shop.GetInLine(client5);
- shop.Work();
- }
- }
- class Shop
- {
- public int Money { get; private set; }
- private List<Product> _products = new List<Product>();
- private Queue<Client> _queueLine = new Queue<Client>();
- public Shop(int money)
- {
- Money = money;
- List<Product> _products = new List<Product>();
- Queue<Client> _queue = new Queue<Client>();
- }
- public void Work()
- {
- while (_queueLine.Count != 0)
- {
- var currentClient = _queueLine.Dequeue();
- bool isPaid = false;
- while (isPaid == false)
- {
- if (currentClient.SumUpPrice() <= currentClient.Money)
- {
- currentClient.Pay(currentClient.SumUpPrice());
- Money += currentClient.SumUpPrice();
- Console.WriteLine("Клиент обслужен!");
- Console.WriteLine("Денег у магазина: " + Money);
- isPaid = true;
- }
- else
- {
- Console.WriteLine("Пришлось кое-что выкинуть...");
- currentClient.ThrowAway();
- }
- }
- }
- }
- public void GetProducts()
- {
- _products.Add(new Product("Milk", 55));
- _products.Add(new Product("Bread", 45));
- _products.Add(new Product("Caviar", 340));
- _products.Add(new Product("Potatoe", 30));
- _products.Add(new Product("Cereals", 200));
- }
- public Product GetRandomItem()
- {
- Random rand = new Random();
- return _products[rand.Next(0, 5)];
- }
- public void GetInLine(Client client)
- {
- _queueLine.Enqueue(client);
- }
- }
- class Client
- {
- public int Money { get; private set; }
- private List<Product> _cart = new List<Product>();
- public Client(int money)
- {
- Money = money;
- List<Product> _cart = new List<Product>();
- }
- public void PutIntoCart(Product product1, Product product2)
- {
- _cart.Add(product1);
- _cart.Add(product2);
- }
- public int SumUpPrice()
- {
- int sum = 0;
- foreach (var item in _cart)
- {
- sum += item.Price;
- }
- return sum;
- }
- public void Pay(int totalPrice)
- {
- Money -= totalPrice;
- }
- public void ThrowAway()
- {
- Random rand = new Random();
- _cart.RemoveAt(rand.Next(0, 1));
- }
- }
- class Product
- {
- public string Name { get; private set; }
- public int Price { get; private set; }
- public Product(string name, int price)
- {
- Name = name;
- Price = price;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement