Skip to content

Instantly share code, notes, and snippets.

@emak4rov
Created February 6, 2025 22:59
Show Gist options
  • Save emak4rov/731fa5e5653bb86c58a37e29aae9fd7d to your computer and use it in GitHub Desktop.
Save emak4rov/731fa5e5653bb86c58a37e29aae9fd7d to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace HWShopV3
{
internal class Program
{
static void Main(string[] args)
{
StockListFactory factory = new StockListFactory();
Seller seller = new Seller("Васёк", 0, factory.Create());
Buyer buyer = new Buyer("Женёк", 100, new List<Item>());
Shop shop = new Shop(seller, buyer);
buyer.ShowAllInfo();
seller.ShowAllInfo();
shop.MakeDeal();
buyer.ShowAllInfo();
seller.ShowAllInfo();
}
}
class Shop
{
private Seller _seller;
private Buyer _buyer;
public Shop(Seller seller, Buyer buyer)
{
_seller = seller;
_buyer = buyer;
}
public void MakeDeal()
{
Item item = _seller.OfferChoice();
if (item != null && _seller.CheckSolvency(item, _buyer))
{
_seller.Sell(item);
_buyer.Buy(item);
}
else
{
Console.WriteLine("Сделка не состоялась");
}
}
}
class Human
{
protected string Role;
protected string Name;
protected int Money;
protected List<Item> Items;
public Human(string name, int money, List<Item> items)
{
Name = name;
Money = money;
Items = items;
}
public int GetMoney => Money;
public void ShowAllInfo()
{
Console.Write($"Меня зовут {Name}, я {Role}, у меня ");
ShowMoney();
ShowInventory();
}
private void ShowMoney()
{
Console.WriteLine($"осталось {Money} монет");
}
private void ShowInventory()
{
Console.WriteLine("У меня в инвентаре:");
if (Items.Count == 0)
{
Console.WriteLine("Пустота");
}
else
{
foreach (Item item in Items)
{
Console.WriteLine($"{item.Name}, цена - {item.Price} монет");
}
}
}
}
class Seller : Human
{
public Seller(string name, int money, List<Item> items) : base(name, money, items)
{
Role = "продавец";
}
public Item OfferChoice()
{
int chosenIndex;
Console.WriteLine("Что приглянулось?");
for (int i = 0; i < Items.Count; i++)
{
Console.WriteLine($"{i + 1}: {Items[i].Name}");
}
Console.WriteLine($"{Items.Count + 1} - {int.MaxValue}: Ничего");
chosenIndex = Utils.ReadInt();
if (chosenIndex > 0 && chosenIndex <= Items.Count)
{
int index = chosenIndex - 1;
return GetItem(index);
}
else
{
Console.WriteLine("Выбрано ничего.\nПотрачено 0 денег, заходите ещё. Или нет.");
return null;
}
}
public bool CheckSolvency(Item item, Buyer buyer)
{
if (item.Price <= buyer.GetMoney)
{
return true;
}
else
{
Console.WriteLine("Не хватат");
return false;
}
}
public void Sell(Item item)
{
int price = item.Price;
Items.Remove(item);
TakeMoney(price);
}
private void TakeMoney(int money)
{
Money += money;
}
private Item GetItem(int index)
{
Item item = Items[index];
return item;
}
}
class Buyer : Human
{
public Buyer(string name, int money, List<Item> items) : base(name, money, items)
{
Role = "покупатель";
}
public void Buy(Item item)
{
int price = item.Price;
GiveMoney(price);
TakeItem(item);
}
private void TakeItem(Item item)
{
Items.Add(item);
}
private void GiveMoney(int price)
{
Money -= price;
}
}
class StockListFactory
{
private int _quantityEveryFruit;
private List<Item> _items;
public StockListFactory()
{
SetFruits();
SetQuantity();
}
public List<Item> Create()
{
List<Item> items = new List<Item>();
for (int i = 0; i < _items.Count; i++)
{
for (int j = 0; j < _quantityEveryFruit; j++)
{
items.Add(new Item(_items[i].Name, _items[i].Price));
}
}
return items;
}
private void SetFruits()
{
_items = new List<Item>
{
new Item("Яблоко", 5),
new Item("Апельсин", 150),
new Item("Лимон", 8)
};
}
private void SetQuantity()
{
_quantityEveryFruit = 3;
}
}
class Item
{
public Item(string name, int price)
{
Name = name;
Price = price;
}
public string Name { get; private set; }
public int Price { get; private set; }
}
static class Utils
{
public static int ReadInt()
{
int number;
do
{
Console.WriteLine("Введите номер.");
}
while (int.TryParse(Console.ReadLine(), out number) == false);
return number;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment