Skip to content

Instantly share code, notes, and snippets.

@talatari
Last active November 14, 2023 07:50
Show Gist options
  • Save talatari/dfd9d4f4cd06b3999e90304b44c5d6b7 to your computer and use it in GitHub Desktop.
Save talatari/dfd9d4f4cd06b3999e90304b44c5d6b7 to your computer and use it in GitHub Desktop.
43. Task: Shop
public class Program
{
static void Main()
{
Shop shop = new Shop();
shop.Open();
}
}
class Product
{
public Product(string name, int price)
{
Name = name;
Price = price;
}
public string Name { get; private set; }
public int Price { get; private set; }
}
class Cell
{
public Cell(Product product, int amount)
{
Product = product;
Amount = amount;
}
public Product Product { get; private set; }
public int Amount { get; private set; }
public void IncreaseAmountProduct(int amount)
{
Amount += amount;
}
public void DecreaseAmountProduct(int amount)
{
Amount -= amount;
}
}
class Human
{
public Human(int money)
{
Money = money;
}
protected List<Cell> Cells = new List<Cell>();
public int Money { get; protected set; }
public void ShowMoney()
{
Console.WriteLine($"Money {this} = {Money}");
}
public void ShowProducts()
{
int borderLength = 45;
char borderSymbol = '-';
if (Cells.Count > 0)
{
Console.WriteLine("\n[Product] \t [Price] \t [Amount] ");
foreach (var cell in Cells)
{
Console.WriteLine($" {cell.Product.Name}\t\t {cell.Product.Price}\t\t {cell.Amount}");
}
}
else
{
Console.WriteLine("\nList empty.");
}
Console.WriteLine(new string(borderSymbol, borderLength));
}
}
class Seller : Human
{
public Seller(int money) : base(money)
{
FillCells();
}
public bool TryGetCellProduct(string name, out Cell cell)
{
cell = null;
for (int i = 0; i < Cells.Count; i++)
{
if (Cells[i].Product.Name.ToLower().Contains(name.ToLower()))
{
cell = Cells[i];
return true;
}
}
return false;
}
public int GetPricePerAmount(string name, int amount)
{
foreach (var cell in Cells)
{
if (cell.Product.Name == name && cell.Amount >= amount)
{
return cell.Product.Price * amount;
}
}
return 0;
}
public void Sell(Cell cell, int amount, int money)
{
DecreaseAmountProduct(cell.Product.Name, amount);
IncreaseMoney(money);
}
private void FillCells()
{
Cells.Add(new Cell(new Product(name: "Bread", price: 100), amount: 15));
Cells.Add(new Cell(new Product(name: "Cheese", price: 250), amount: 13));
Cells.Add(new Cell(new Product(name: "Eggs", price: 90), amount: 11));
Cells.Add(new Cell(new Product(name: "Milk", price: 80), amount: 21));
Cells.Add(new Cell(new Product(name: "Meat", price: 330), amount: 17));
}
private void DecreaseAmountProduct(string name, int amount)
{
foreach (var cell in Cells)
{
if (cell.Product.Name == name && cell.Amount >= amount)
{
cell.DecreaseAmountProduct(amount);
return;
}
}
}
private void IncreaseMoney(int money)
{
Money += money;
}
}
class Buyer : Human
{
public Buyer(int money) : base(money) { }
public void Buy(Cell cell, int amount, int money)
{
IncreaseAmountProduct(cell, amount);
DecreaseMoney(money);
}
public bool CanPay(int pricePerAmount)
{
return Money >= pricePerAmount;
}
private void IncreaseAmountProduct(Cell cell, int amount)
{
foreach (var cellBuyer in Cells)
{
if (cellBuyer.Product.Name == cell.Product.Name)
{
cellBuyer.IncreaseAmountProduct(amount);
return;
}
}
Cells.Add(new Cell(new Product(cell.Product.Name, cell.Product.Price), amount));
}
private void DecreaseMoney(int money)
{
Money -= money;
}
}
class Shop
{
public void Open()
{
const string TradeProductCommand = "1";
const string ExitCommand = "2";
bool isOpen = true;
int moneySeller = 0;
int moneyBuyer = 9000;
Seller seller = new Seller(moneySeller);
Buyer buyer = new Buyer(moneyBuyer);
while (isOpen)
{
Console.Clear();
seller.ShowMoney();
seller.ShowProducts();
buyer.ShowMoney();
buyer.ShowProducts();
Console.WriteLine(
$"\nMenu: \n" +
$" {TradeProductCommand} - Trade Product\n" +
$" {ExitCommand} - Exit\n");
Console.Write("Enter command: ");
string userInput = Console.ReadLine();
switch (userInput)
{
case TradeProductCommand:
Trading(seller, buyer);
break;
case ExitCommand:
isOpen = false;
break;
}
}
}
private void Trading(Seller seller, Buyer buyer)
{
Console.Write("\nEnter product name: ");
if (seller.TryGetCellProduct(Console.ReadLine().Replace(" ", ""), out Cell cell))
{
Console.Write("Enter product amount: ");
if (int.TryParse(Console.ReadLine(), out int amount))
{
int pricePerAmount = seller.GetPricePerAmount(cell.Product.Name, amount);
if (pricePerAmount > 0)
{
if (buyer.CanPay(pricePerAmount))
{
seller.Sell(cell, amount, pricePerAmount);
buyer.Buy(cell, amount, pricePerAmount);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment