/// <summary> /// Purchase an item from the machine /// </summary> /// <param name="slotID">The slot identifier</param> /// <returns>The vended product</returns> public VendingMachineItem Purchase(string slotID) { // Translate the slot to all uppercase for comparison slotID = slotID.ToUpper(); // check if there is anything at the slot if (GetQuantityOfSlot(slotID) > 0) { // is there enough money? if (_currentBalance > GetCostOfSlot(slotID)) { // decrement the balance by the cost of the item _currentBalance -= GetCostOfSlot(slotID); // get the item from the slot VendingMachineItem purchasedItem = GetItemOutOfSlot(slotID); // log the purchase FileFunctions.LogTransaction(purchasedItem.itemName + " " + slotID, purchasedItem.price, _currentBalance); // return the item to the purchaser return(purchasedItem); } // If not enough money, through InsufficientFundsException throw new InsufficientFundsException(); } // if there is nothing in slot, throw and OutOfStockException throw new OutOfStockException(); }
/// <summary> /// Method to return change from the vending machine /// </summary> /// <returns>A changedrawer object containing the change breakdown</returns> public ChangeDrawer ReturnChange() { // Log the transaction. Do it here since the CurrentBalance will change after MakeChange is called FileFunctions.LogTransaction("GIVE CHANGE", CurrentBalance, 0M); ChangeDrawer changeDrawer = new ChangeDrawer(); changeDrawer.MakeChange(CurrentBalance); return(changeDrawer); }
/// <summary> /// Public method to add money to the vending machine's balance /// </summary> /// <param name="dollars">Amount of dollars to add.</param> /// <returns>The machine balance after the money is added</returns> public decimal FeedMoney(int dollars) { try { _currentBalance += dollars; } catch (Exception e) { throw e; } // Log it FileFunctions.LogTransaction("FEED MONEY", (decimal)dollars, CurrentBalance); return(CurrentBalance); }
/// <summary> /// Public method to add money to the vending machine's balance /// </summary> /// <param name="dollars">Amount of dollars to add.</param> /// <returns>The machine balance after the money is added</returns> public decimal FeedMoney(int dollars) { // check for negative values. Need to check here in case this class is used elsewhere if (dollars < 0) { throw new InvalidDollarAmount(); } try { _currentBalance += dollars; } catch (Exception e) { throw e; } // Log it FileFunctions.LogTransaction("FEED MONEY", (decimal)dollars, CurrentBalance); return(CurrentBalance); }