Last active
August 12, 2025 19:01
-
-
Save Strelok78/217eb55b1bc232f672d17fa471726c98 to your computer and use it in GitHub Desktop.
Store application (buyer, trader, product)
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
| namespace iJuniorPractice | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| List<Product> productsInStore = new List<Product> | |
| { | |
| new Product(1, 20, "Apple"), | |
| new Product(2, 100, "Coconut"), | |
| new Product(3, 10, "Cucumber"), | |
| new Product(4, 40, "Pineapple") | |
| }; | |
| Trader trader = new Trader(100, productsInStore); | |
| Buyer buyer = new Buyer(50, new List<Product>()); | |
| Shop shop = new Shop(trader, buyer); | |
| shop.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 Shop | |
| { | |
| private const int CommandShowStoreInfo = 1; | |
| private const int CommandBuyProduct = 2; | |
| private const int CommandShowBuyerInventory = 3; | |
| private const int CommandExit = 0; | |
| private Trader _trader; | |
| private Buyer _buyer; | |
| private bool _isWork; | |
| public Shop(Trader trader, Buyer buyer, bool isWork = true) | |
| { | |
| _trader = trader; | |
| _buyer = buyer; | |
| _isWork = isWork; | |
| } | |
| public void Play() | |
| { | |
| while (_isWork) | |
| { | |
| ShowMenu(); | |
| int input = Utils.ReadInt("Please enter command: "); | |
| switch (input) | |
| { | |
| case CommandShowStoreInfo: | |
| _trader.ShowInfo(); | |
| break; | |
| case CommandBuyProduct: | |
| Trade(_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 Trade(Buyer buyer, Trader trader) | |
| { | |
| trader.ShowInfo(); | |
| int productId = buyer.GetProductIdToBuy(); | |
| if (trader.TryGetProductById(productId, out Product product)) | |
| { | |
| if (buyer.TryToBuy(product) && trader.TryToSell(product)) | |
| { | |
| Console.WriteLine("Purchase successful!"); | |
| } | |
| else | |
| { | |
| Console.WriteLine("Purchase failed!"); | |
| } | |
| } | |
| } | |
| } | |
| class Character | |
| { | |
| protected List<Product> Products; | |
| protected Character(int moneyAmount, List<Product> products) | |
| { | |
| MoneyAmount = moneyAmount; | |
| Products = products; | |
| } | |
| public int MoneyAmount { get; protected set; } | |
| public void ShowInfo() | |
| { | |
| Console.WriteLine($"Balance: {MoneyAmount}, Product list:"); | |
| if (Products.Count == 0) | |
| { | |
| Console.WriteLine("Inventory is empty."); | |
| } | |
| else | |
| { | |
| foreach (var product in Products) | |
| { | |
| product.WriteInfo(); | |
| } | |
| } | |
| } | |
| } | |
| class Buyer : Character | |
| { | |
| public Buyer(int moneyAmount, List<Product> products) : base(moneyAmount, products) | |
| { | |
| } | |
| public int GetProductIdToBuy() | |
| { | |
| int productId; | |
| productId = Utils.ReadInt("Please enter product ID: "); | |
| return productId; | |
| } | |
| public bool TryToBuy(Product product) | |
| { | |
| if (MoneyAmount >= product.Price) | |
| { | |
| MoneyAmount -= product.Price; | |
| Products.Add(product); | |
| return true; | |
| } | |
| Console.WriteLine("Not enough money."); | |
| return false; | |
| } | |
| } | |
| class Trader : Character | |
| { | |
| public Trader(int moneyAmount, List<Product> products) : base(moneyAmount, products) | |
| { | |
| } | |
| public bool TryGetProductById(int productId, out Product product) | |
| { | |
| foreach (Product productInInventory in Products) | |
| { | |
| if (productInInventory.Id == productId) | |
| { | |
| product = productInInventory; | |
| return true; | |
| } | |
| } | |
| Console.WriteLine("Incorrect product id. Try again."); | |
| product = null; | |
| return false; | |
| } | |
| public bool TryToSell(Product product) | |
| { | |
| if (product == null) | |
| { | |
| Console.WriteLine("Product is null."); | |
| return false; | |
| } | |
| MoneyAmount += product.Price; | |
| Products.Remove(product); | |
| return true; | |
| } | |
| } | |
| class Product | |
| { | |
| public Product(int id, int price, string name) | |
| { | |
| Id = id; | |
| Price = price; | |
| Name = name; | |
| } | |
| public int Id { get; private set; } | |
| public int Price { get; private set; } | |
| public string Name { get; private set; } | |
| public void WriteInfo() | |
| { | |
| Console.WriteLine($"ID: {Id}, Title: {Name}, Price: {Price}"); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment