/// <summary> /// Checks to see that valid product code was entered, makes sure user has entered enough money, /// and makes sure the product is not sold out. /// This method then "purchases" the item selected by reducing quantity by 1 in inventory, /// reducing current balance by the cost of the item, records the transaction to the transaction file logger, /// adds the transaction to the sales report, and adds the cost of the item to total sales. /// </summary> /// <param name="slotId"></param> /// <returns></returns> public InventoryItem PurchaseItem(string slotId) { var invItem = _inventory[slotId]; if (_inventory.ContainsKey(slotId) == false) { throw new Exception("Invalid product code. Please try again."); } else if (_inventory[slotId].ItemName.Price > CurrentBalance) { throw new Exception("Insufficient Funds.Please add more money."); } else if (_inventory[slotId].Quantity > 0 && _inventory[slotId].ItemName.Price <= CurrentBalance) { invItem.Quantity--; CurrentBalance -= invItem.ItemName.Price; _totalSales += invItem.ItemName.Price; _transactionFileLogger.RecordGetItem(invItem.ItemName.Name, slotId, invItem.ItemName.Price, CurrentBalance); _salesReport.Add($"{invItem.ItemName.Name}|{ 5 - invItem.Quantity}"); } else if (invItem.Quantity <= 0) { throw new Exception("Sold Out! Make another selection."); } else { throw new Exception("Something went wrong. Please try again"); } return(invItem); }
/// <summary> /// /// </summary> /// <param name="slotId"></param> /// <returns></returns> public InventoryItem GetItem(string slotId) { //string directory = Environment.CurrentDirectory; //string filePath = "Log.txt"; //string fullPath = Path.Combine(directory, filePath); if (_inventory.ContainsKey(slotId) == false) { throw new InvalidSlotIDException("Invalid product code. Please try again."); } else if (_inventory[slotId].ItemName.Price > CurrentBalance) { throw new InsufficientFundsException("Insufficient Funds.Please add more money."); } else if (_inventory[slotId].Quantity > 0 && _inventory[slotId].ItemName.Price <= CurrentBalance) { _inventory[slotId].Quantity--; CurrentBalance -= _inventory[slotId].ItemName.Price; _totalSales += _inventory[slotId].ItemName.Price; _transactionFileLogger.RecordGetItem(_inventory[slotId].ItemName.Name, slotId, _inventory[slotId].ItemName.Price, CurrentBalance); //using (StreamWriter sw = new StreamWriter(fullPath, true)) //{ // sw.WriteLine("{0,-10}{1,-10}{2,-10}{3,-10}", $"{DateTime.Now}", $" {_inventory[slotId].ItemName.Name} {slotId}", $" ${ _inventory[slotId].ItemName.Price}", $" ${CurrentBalance}"); //} //List<string> salesReport = new List<string>(); salesReport.Add($"{_inventory[slotId].ItemName.Name}|{ 5 - _inventory[slotId].Quantity}"); //string salesDirectory = Environment.CurrentDirectory; //string salesFilePath = "SalesReport.txt"; //salesFullPath = Path.Combine(salesDirectory, salesFilePath); //using (StreamWriter sw = new StreamWriter(salesFullPath, true)) //{ // foreach (var item in salesReport) // { // sw.WriteLine(salesReport); // } // sw.WriteLine($"**TOTAL SALES** ${_totalSales}"); //} return(_inventory[slotId]); } else if (_inventory[slotId].Quantity <= 0) { throw new OutOfStockException("Sold Out! Make another selection."); } else { throw new Exception("Something went wrong. Please try again"); } }