Last active
May 19, 2025 10:41
-
-
Save VladislavSavich/6e50abf72b1b3e4fa46ebc19f922c86d to your computer and use it in GitHub Desktop.
C# Junior Магазин
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; | |
using System.Linq; | |
namespace CSLight | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Shop shop = new Shop(); | |
shop.Work(); | |
} | |
} | |
class Shop | |
{ | |
private Merchant _merchant; | |
private Bayer _bayer; | |
public Shop() | |
{ | |
_merchant = new Merchant(0); | |
_bayer = new Bayer(1000); | |
} | |
public void Work() | |
{ | |
const string CommandTrade = "1"; | |
const string CommandExit = "Exit"; | |
string userInput; | |
bool isWork = true; | |
while (isWork) | |
{ | |
Console.WriteLine("-МАГАЗИН-"); | |
Console.WriteLine($"{CommandTrade} - Купить товары"); | |
Console.WriteLine($"{CommandExit} - выход"); | |
Console.WriteLine(new string('-', 25)); | |
Console.WriteLine("ИНФОРМАЦИЯ О ПРОДАВЦЕ:"); | |
_merchant.ShowCash(); | |
_merchant.ShowProducts(); | |
Console.WriteLine("\nИНФОРМАЦИЯ О ПОКУПАТЕЛЕ:"); | |
_bayer.ShowCash(); | |
_bayer.ShowProducts(); | |
Console.WriteLine(new string('-', 25)); | |
userInput = Console.ReadLine(); | |
switch (userInput) | |
{ | |
case CommandTrade: | |
Trade(_merchant, _bayer); | |
break; | |
case CommandExit: | |
isWork = false; | |
break; | |
} | |
Console.ReadKey(); | |
Console.Clear(); | |
} | |
} | |
private void Trade(Merchant merchant, Bayer bayer) | |
{ | |
int number; | |
Console.Clear(); | |
Console.WriteLine("Выберите продукт для покупки:"); | |
_merchant.ShowProducts(); | |
number = ReadInt(); | |
if (number <= 0 || number > _merchant.ProductsCount) | |
{ | |
Console.WriteLine("Продукта под таким номером не существует!"); | |
} | |
else | |
{ | |
number -= 1; | |
if (bayer.IsEnoughMoney(merchant.GetProduct(number))) | |
{ | |
bayer.BuyProduct(merchant.SellProduct(number)); | |
} | |
} | |
} | |
private int ReadInt() | |
{ | |
int number; | |
while (int.TryParse(Console.ReadLine(), out number) == false) | |
{ | |
Console.WriteLine("Некорректный ввод! Попробуйте снова!"); | |
} | |
return number; | |
} | |
} | |
class Human | |
{ | |
protected List<Product> Products; | |
protected int Money; | |
public Human(int money) | |
{ | |
Products = new List<Product>(); | |
Money = money; | |
} | |
public void ShowProducts() | |
{ | |
int productsCounter = 1; | |
Console.WriteLine("Продукты: "); | |
for (int i = 0; i < Products.Count; i++) | |
{ | |
Console.Write($"{productsCounter}."); | |
Products[i].ShowInfo(); | |
productsCounter++; | |
} | |
} | |
public void ShowCash() | |
{ | |
Console.WriteLine($"Деньги: {Money} руб."); | |
} | |
} | |
class Merchant : Human | |
{ | |
public Merchant(int money) : base(money) | |
{ | |
Products = new List<Product>() { new Product("Яблоко", 50), new Product("Апельсин", 100), new Product("Манго", 1000) }; | |
} | |
public int ProductsCount => Products.Count; | |
public Product GetProduct(int number) | |
{ | |
return Products[number]; | |
} | |
public Product SellProduct(int number) | |
{ | |
Product productForSale = null; | |
productForSale = Products[number]; | |
Products.RemoveAt(number); | |
Money += productForSale.Price; | |
return productForSale; | |
} | |
} | |
class Bayer : Human | |
{ | |
private int _moneyToPay; | |
public Bayer(int money) : base(money) { } | |
public bool IsEnoughMoney(Product product) | |
{ | |
_moneyToPay = product.Price; | |
if (Money >= _moneyToPay) | |
{ | |
return true; | |
} | |
else | |
{ | |
Console.WriteLine("Недостаточно денег("); | |
_moneyToPay = 0; | |
return false; | |
} | |
} | |
public void BuyProduct(Product product) | |
{ | |
Money -= _moneyToPay; | |
Products.Add(product); | |
} | |
} | |
class Product | |
{ | |
public Product(string name, int price) | |
{ | |
Name = name; | |
Price = price; | |
} | |
public string Name { get; private set; } | |
public int Price { get; private set; } | |
public void ShowInfo() | |
{ | |
Console.WriteLine($"|Товар: {Name}, цена - {Price}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment