Skip to content

Instantly share code, notes, and snippets.

@IozhikLI
Created April 29, 2025 02:52
Show Gist options
  • Save IozhikLI/ecba97d355b811b95d1b199f9c1f3711 to your computer and use it in GitHub Desktop.
Save IozhikLI/ecba97d355b811b95d1b199f9c1f3711 to your computer and use it in GitHub Desktop.
ДЗ: Магазин
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using static System.Reflection.Metadata.BlobBuilder;
namespace store
{
internal class Program
{
static void Main(string[] args)
{
Buyer buyer = new Buyer("Артур", 1500, new List<string> { "Сыр", "Армия клонов", "Машина", "Хлеб", "Мясо", "Луноход" });
Seller seller = new Seller("Иван", 30, new List<Commodity>{
new Commodity("Курица", 240, "Куриное феле 1 килограмм"),
new Commodity("Хлеб", 60, "Батон 200 грамм"),
new Commodity("Книга", 840, "\"Мёртвые души\" том 2"),
new Commodity("Мясо", 400, "Стэйки, 500 грамм"),
new Commodity("Машина", 8800, "Игрушка на радиоуправлении"),
new Commodity("Сыр", 475, "Голландский (контрабанда)")
});
buyer.ShowMoney();
seller.ShowMoney();
seller.ShowCommodities();
buyer.ShowShoppingList();
seller.Sell(buyer);
buyer.ShowCommodities();
buyer.ShowMoney();
seller.ShowMoney();
}
}
class Person
{
protected int _money;
protected List<Commodity> _commodities;
public Person(string name, List<Commodity> commodity, int money)
{
_commodities = commodity;
Name = name;
if (money < 0)
{
_money = 0;
}
else
{
_money = money;
}
}
public Person(string name, int money)
{
_commodities = new List<Commodity>();
Name = name;
_money = money;
}
public string Name { get; private set; }
public void ShowMoney()
{
Console.WriteLine($"{Name} обладает суммой в {_money} денег");
}
public virtual void ShowCommodities()
{
Console.WriteLine($"Вот что имеет {Name}\n");
}
protected void ShowSeparator()
{
Console.WriteLine("\n" + new String('-', 40) + "\n");
}
}
class Seller : Person
{
public Seller(string name, int money, List<Commodity> commodity) : base(name, commodity, money) { }
public void Sell(Buyer buyer)
{
foreach (String product in buyer.ShoppingList)
{
if (FindProduct(product, out int indexProduct))
{
if (buyer.CheckSolvency(_commodities[indexProduct].Price))
{
_money += buyer.Buy(_commodities[indexProduct]);
_commodities.RemoveAt(indexProduct);
}
}
}
}
public override void ShowCommodities()
{
Console.WriteLine("\nОбратите внимание на прилавок");
ShowSeparator();
foreach (Commodity comodity in _commodities)
Console.WriteLine($"{comodity.Name} - {comodity.Price} денег");
ShowSeparator();
}
private bool FindProduct(string product, out int indexProduct)
{
indexProduct = 0;
for (int index = 0; index < _commodities.Count; index++)
{
if (_commodities[index].Name == product)
{
Console.WriteLine($"- {product} стоит {_commodities[index].Price} денег");
indexProduct = index;
return true;
}
}
Console.WriteLine($"- {product} сейчас отсутствует, но скоро будет привоз\n");
return false;
}
}
class Buyer : Person
{
private int _moneyToPay;
public Buyer(string name, int money, List<string> shoppingList) : base(name, money)
{
ShoppingList = shoppingList;
_moneyToPay = 0;
}
public List<string> ShoppingList { get; private set; }
public void ShowShoppingList()
{
Console.Write($"\n{Name} Хочет приобрести:\n| ");
foreach (String purchase in ShoppingList)
{
Console.Write(" " + purchase + " |");
}
Console.WriteLine("\n");
}
public int Buy(Commodity commodity)
{
_money -= _moneyToPay;
_commodities.Add(commodity);
return _moneyToPay;
}
public bool CheckSolvency(int price)
{
if (price >= 0 && _money > price)
{
Console.WriteLine("- Мне по карману, упакуйте пожалуйста\n");
_moneyToPay = price;
return true;
}
Console.WriteLine("- Это очень дорого..\n");
_moneyToPay = 0;
return false;
}
public override void ShowCommodities()
{
base.ShowCommodities();
ShowSeparator();
foreach (Commodity comodity in _commodities)
Console.WriteLine($"{comodity.Name} - {comodity.Description}");
ShowSeparator();
}
}
class Commodity
{
public Commodity(string name, int price, string description)
{
Name = name;
Price = price;
Description = description;
}
public int Price { get; private set; }
public string Name { get; private set; }
public string Description { get; private set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment