Skip to content

Instantly share code, notes, and snippets.

@Strelok78
Last active June 26, 2025 20:57
Show Gist options
  • Save Strelok78/217eb55b1bc232f672d17fa471726c98 to your computer and use it in GitHub Desktop.
Save Strelok78/217eb55b1bc232f672d17fa471726c98 to your computer and use it in GitHub Desktop.
Store application (buyer, trader, product)
using System;
using System.Collections.Generic;
namespace iJuniorPractice
{
class Program
{
static void Main(string[] args)
{
AppController appController = new AppController();
appController.Play();
}
}
static class Utils
{
public static int ReadInt(string message = "")
{
int number;
string input;
Console.WriteLine(message);
input = Console.ReadLine();
while (int.TryParse(input, out number) == false)
{
Console.WriteLine("Incorrect input. Try again.");
input = Console.ReadLine();
}
return number;
}
public static string ReadString(string message = "")
{
string input;
Console.WriteLine(message);
input = Console.ReadLine();
while (input == "")
{
Console.WriteLine("Incorrect input, line is empty. Try again.");
input = Console.ReadLine();
}
return input;
}
}
class AppController
{
private const int CommandShowStoreInfo = 1;
private const int CommandBuyProduct = 2;
private const int CommandShowBuyerInventory = 3;
private const int CommandExit = 0;
public void Play()
{
Dictionary<int, Product> productsInStore = new Dictionary<int, Product>
{
{ 1, new Product(1, 20, "Apple", 5) },
{ 2, new Product(2, 100, "Coconut", 1) },
{ 3, new Product(3, 10, "Cucumber", 10) },
{ 4, new Product(4, 40, "Pineapple", 2) }
};
Trader trader = new Trader(100, productsInStore);
Buyer buyer = new Buyer(1000, new Dictionary<int, Product>());
bool isWork = true;
while (isWork)
{
ShowMenu();
int input = Utils.ReadInt("Please enter command: ");
switch (input)
{
case CommandShowStoreInfo:
trader.ShowProducts();
break;
case CommandBuyProduct:
HandleBuyProduct(buyer, trader);
break;
case CommandShowBuyerInventory:
buyer.ShowInfo();
break;
case CommandExit:
isWork = false;
break;
default:
Console.WriteLine("Incorrect input.");
break;
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
Console.Clear();
}
Console.WriteLine("Exiting...");
}
private void ShowMenu()
{
Console.WriteLine($"Main Menu:");
Console.WriteLine($"{CommandShowStoreInfo} - See the products in the shop");
Console.WriteLine($"{CommandBuyProduct} - Buy a product");
Console.WriteLine($"{CommandShowBuyerInventory} - Show Inventory");
Console.WriteLine($"{CommandExit} - Exit");
}
private void HandleBuyProduct(Buyer buyer, Trader trader)
{
trader.ShowProducts();
if (buyer.TryToBuyProduct(out int amount, out int productId))
{
Product product = trader.GetProductById(productId);
if (product != null && trader.TryToSell(product, amount, buyer))
{
buyer.Buy(amount, product);
Console.WriteLine("Purchase successful!");
}
}
}
}
class Trade
{
protected Trade(int moneyAmount, Dictionary<int, Product> products)
{
MoneyAmount = moneyAmount;
Products = products;
}
public int MoneyAmount { get; protected set; }
public Dictionary<int, Product> Products { get; protected set; }
}
class Buyer : Trade
{
public Buyer(int moneyAmount, Dictionary<int, Product> products) : base(moneyAmount, products)
{
}
public bool TryToBuyProduct(out int amount, out int productId)
{
productId = Utils.ReadInt("Please enter product ID: ");
amount = Utils.ReadInt("Please enter the amount you want to buy: ");
if (amount <= 0)
{
Console.WriteLine("Amount must be greater than zero.");
return false;
}
return true;
}
public void Buy(int amount, Product product)
{
if (MoneyAmount >= product.Price * amount)
{
MoneyAmount -= product.Price * amount;
if (Products.ContainsKey(product.Id))
{
Products[product.Id].IncreaseAmountBy(amount);
}
else
{
Products.Add(product.Id, new Product(product.Id, product.Price, product.Name, amount));
}
}
else
{
Console.WriteLine("Not enough money.");
}
}
public void ShowInfo()
{
Console.WriteLine($"Balance: {MoneyAmount}, Product list:");
if (Products.Count == 0)
{
Console.WriteLine("Inventory is empty.");
}
else
{
foreach (var product in Products.Values)
{
product.GetInfo();
}
}
}
}
class Trader : Trade
{
public Trader(int moneyAmount, Dictionary<int, Product> products) : base(moneyAmount, products)
{
}
public Product GetProductById(int productId)
{
if (Products.TryGetValue(productId, out Product product))
{
return product;
}
Console.WriteLine("Incorrect product id. Try again.");
return null;
}
public bool TryToSell(Product product, int amount, Buyer buyer)
{
if (product == null)
{
return false;
}
if (amount > product.Count)
{
Console.WriteLine("Not enough products in stock.");
return false;
}
if (buyer.MoneyAmount < product.Price * amount)
{
Console.WriteLine("Buyer does not have enough money.");
return false;
}
product.DecreaseAmountBy(amount);
MoneyAmount += product.Price * amount;
if (product.Count == 0)
{
Products.Remove(product.Id);
}
return true;
}
public void ShowProducts()
{
if (Products.Count == 0)
{
Console.WriteLine("Store is empty.");
}
else
{
foreach (var product in Products)
{
Console.Write($"{product.Key}. ");
product.Value.GetInfo();
}
}
}
}
class Product
{
public Product(int id, int price, string name, int count)
{
Id = id;
Price = price;
Name = name;
Count = count;
}
public int Id { get; private set; }
public int Price { get; private set; }
public string Name { get; private set; }
public int Count { get; private set; }
public void GetInfo()
{
Console.WriteLine($"{Name}, Price: {Price}, Count: {Count}");
}
public void IncreaseAmountBy(int amount)
{
Count += amount;
}
public void DecreaseAmountBy(int amount)
{
Count -= amount;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment