Last active
July 13, 2022 21:39
-
-
Save xjncx/554d3b340750e2184881dc7023ddb9b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace Homework | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
List<Item> items = new List<Item>() | |
{ | |
new Item("milk", 2), | |
new Item("bread", 1), | |
new Item("tomato", 3) | |
}; | |
Basket basket = new Basket(items); | |
Customer Peter = new Customer(basket, 100, "Петя"); | |
Customer Alex = new Customer(basket, 3, "Алеша"); | |
Queue<Customer> customers = new Queue<Customer>(); | |
customers.Enqueue(Peter); | |
customers.Enqueue(Alex); | |
Shop SevenEleven = new Shop(customers); | |
SevenEleven.ServeCustomers(); | |
} | |
} | |
class Shop | |
{ | |
private Queue<Customer> _customers; | |
public Shop(Queue<Customer> customers) | |
{ | |
_customers = customers; | |
} | |
public void ServeCustomers() | |
{ | |
bool isEnoughtMoney = false; | |
int customerBill; | |
foreach (Customer customer in _customers) | |
{ | |
while (isEnoughtMoney == false) | |
{ | |
customerBill = customer.CountTheBill(); | |
if (customerBill > customer.Cash) | |
{ | |
customer.ClearBasket(); | |
Console.WriteLine("Нужно больше золота" + customer.Name); | |
} | |
else | |
{ | |
isEnoughtMoney = true; | |
} | |
} | |
isEnoughtMoney = false; | |
Console.WriteLine("Покупка оплачена!" + customer.Name); | |
} | |
} | |
} | |
class Customer | |
{ | |
private Basket _basket; | |
public string Name { get; private set; } | |
public int Cash { get; private set; } | |
public Customer(Basket basket, int cash, string name) | |
{ | |
_basket = basket; | |
Cash = cash; | |
Name = name; | |
} | |
public int CountTheBill() | |
{ | |
int calculateBill = _basket.CalculateTotalPrice(); | |
return calculateBill; | |
} | |
public void ClearBasket() | |
{ | |
_basket.DeleteRandomStaff(); | |
} | |
} | |
class Item | |
{ | |
private string _name; | |
public int Price { get; private set; } | |
public Item(string name, int price) | |
{ | |
_name = name; | |
Price = price; | |
} | |
} | |
class Basket | |
{ | |
private List<Item> _items; | |
public Basket(List<Item> customerItems) | |
{ | |
_items = customerItems; | |
} | |
public int CalculateTotalPrice() | |
{ | |
int totalPrice = 0; | |
foreach (Item item in _items) | |
{ | |
totalPrice += item.Price; | |
} | |
return totalPrice; | |
} | |
public void DeleteRandomStaff() | |
{ | |
Random random = new Random(); | |
_items.RemoveAt(random.Next(0, _items.Count)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment