private void handlePurchase() { displayItems(); string chosenSlotNumber = panel.GetPurchaseSelection(); if (!inventory.InventoryExists(chosenSlotNumber)) { panel.DisplayError("Your chosen product doesn't exist."); return; } if (!inventory.CanSell(chosenSlotNumber)) { panel.DisplayError("That item is sold out!"); return; } IVendable chosenProduct = inventory.GetVendable(chosenSlotNumber); if (currentMoneyProvided < chosenProduct.Price) { panel.DisplayError("Please insert more money to buy this product."); return; } // Add money to till. currentMoneyProvided = till.Purchase(chosenProduct, currentMoneyProvided); // Remove inventory. inventory.ReduceInventory(chosenSlotNumber); // Output the purchase. panel.DisplayPurchaseMessage(chosenProduct, currentMoneyProvided); }
public Inventory() { string path = Directory.GetCurrentDirectory(); string fileName = "vendingmachine.csv"; try { using (StreamReader sr = new StreamReader(Path.Combine(path, fileName))) { while (!sr.EndOfStream) { string line = sr.ReadLine(); string[] words = line.Split("|"); decimal cost = decimal.Parse(words[2]); IVendable newItem = null; currentInventory.Add(words[0], 5); if (words[3] == "Chip") { newItem = new Chip(words[1], cost, words[0]); } if (words[3] == "Candy") { newItem = new Candy(words[1], cost, words[0]); } if (words[3] == "Drink") { newItem = new Drink(words[1], cost, words[0]); } if (words[3] == "Gum") { newItem = new Gum(words[1], cost, words[0]); } items.Add(words[0], newItem); } } } catch (FileNotFoundException) { Console.WriteLine("File not found. Try again."); } catch (FormatException) { Console.WriteLine("Incorrect format for cost."); } catch (Exception) { Console.WriteLine("Could not load inventory. Try again."); } }
public decimal Purchase(IVendable product, decimal moneyIn) { return(moneyIn - product.Price); }
public void DisplayPurchaseMessage(IVendable vendable, decimal amountRemaining) { Console.WriteLine($"Item: {vendable.Name} Cost: {vendable.Price} Money Remaining: {amountRemaining}"); vendable.GetPurchaseMessage(); }