/// <summary> /// Performs the action to add to the Balance /// </summary> /// <param name="money">Adds money to the Balance</param> public void FeedMoney(decimal money) { string message = $"FEED MONEY\t{Balance.ToString("C")}\t{money.ToString("C")}\t"; switch (money) { case (1): Balance += 1; break; case (2): Balance += 2; break; case (5): Balance += 5; break; case (10): Balance += 10; break; default: Console.WriteLine("Invalid Entry"); break; } message += Balance.ToString("c"); AuditLog.Log(message); }
/// <summary> /// Update the count of the given product via name. Adds one. /// </summary> /// <param name="name">The name of the product to update.</param> public static void UpdateProductCount(string name) { foreach (SalesReportProduct item in products) { if (item.Name == name) { item.AddOne(); SaveToFile(); return; } } AuditLog.Log("ERROR: No item found!"); }
/// <summary> /// Performs the transaction of the given Product. /// </summary> /// <param name="item">The Product to be purchased.</param> /// <returns>True if successful.</returns> public bool Purchase(Slot itemSlot) { if (Balance >= itemSlot.Item.Price) { string message = $"{itemSlot.Item.Name} {itemSlot.Row}{itemSlot.Column}\t{Balance.ToString("c")}\t{itemSlot.Item.Price.ToString("c")}\t"; Balance -= itemSlot.Item.Price; message += Balance.ToString("c"); AuditLog.Log(message); SalesReport.UpdateProductCount(itemSlot.Item.Name); return(true); } return(false); }
/// <summary> /// Reads in the sales report (if one currently exists) and saves each object into a list of SalesReportProducts for manipulation. If file doesn't exist then it will use the provided currStockedProducts; /// </summary> /// <param name="currStockedProducts">The products currently stocked.</param> public static void ReadIn(List <Product> currStockedProducts) { string currentDirectory = Environment.CurrentDirectory; string fileName = "salesReport.txt"; string fullPath = Path.Combine(currentDirectory, fileName); if (File.Exists(fullPath)) { try { using (StreamReader sr = new StreamReader(fullPath)) { while (!sr.EndOfStream) { string line = sr.ReadLine(); if (line == "") { break; } string[] splitStr = line.Split("|", StringSplitOptions.RemoveEmptyEntries); if (GeneralAssistance.CheckIfIntNumber(splitStr[1])) { foreach (Product item in currStockedProducts) { if (item.Name == splitStr[0]) { products.Add(new SalesReportProduct(splitStr[0], int.Parse(splitStr[1]), item.Price)); } } } } } } catch (IOException e) { AuditLog.Log("ERROR: Ran into a IOException.\t" + e.Message); } } else { foreach (Product item in currStockedProducts) { products.Add(new SalesReportProduct(item.Name, 0, item.Price)); } } }
//public Product Purchase (Product pPrice) //{ // return Balance - pPrice; //} /* * remainingBalance = Balance * 100 * int quarters = remainingBalance / 25 * remainingBalance = (remainingBalance / 100) - (quarters * .25) * 100 * return if remainingBalance is not > 0 * int dimes = remainingBalance / 10 * remainingBalance = (remainingBalance / 100) - (dimes * .1) * 100 * return if remainingBalance is not > 0 * int nickels = remainingBalance / 5 * remainingBalance = (remainingBalance / 100) - (nickels * .05) * 100 * Throw an error if remaining != 0 */ /// <summary> /// Returns the remaining balance to the user /// </summary> /// <returns>Returns string with quarters, dimes and nickels</returns> public string ReturnChange() { decimal remainingBalance; string message = $"GIVE CHANGE\t{Balance.ToString("c")}\t{Balance.ToString("c")}\t"; remainingBalance = Balance * 100; int quarters = (int)(remainingBalance / 25); remainingBalance = ((remainingBalance / 100) - (quarters * .25M)) * 100; if (remainingBalance <= 0) { decimal temp = Balance; Balance = 0; message += Balance.ToString("c"); AuditLog.Log(message); return(temp.ToString("c") + $" in {quarters} quarters"); } int dimes = (int)(remainingBalance / 10); remainingBalance = ((remainingBalance / 100) - (dimes * .10M)) * 100; if (remainingBalance <= 0) { decimal temp = Balance; Balance = 0; message += Balance.ToString("c"); AuditLog.Log(message); return(temp.ToString("c") + $" in {quarters} quarters and {dimes} dimes"); } int nickels = (int)(remainingBalance / 5); remainingBalance = ((remainingBalance / 100) - (nickels * .05M)) * 100; if (remainingBalance <= 0) { decimal temp = Balance; Balance = 0; message += Balance.ToString("c"); AuditLog.Log(message); return(temp.ToString("c") + $"in {quarters} quarters, {dimes} dimes and {nickels} nickels"); } AuditLog.Log("ERROR: Money is still there"); return(""); }
/// <summary> /// Re-stocks the machine via the re-stock file. /// </summary> public void ReStock() { string currDirectory = Environment.CurrentDirectory; string stockInputFile = "vendingmachine.csv"; string fullPath = Path.Combine(currDirectory, stockInputFile); if (File.Exists(fullPath)) { try { using (StreamReader sr = new StreamReader(fullPath)) { Slots = new List <Slot>(); while (!sr.EndOfStream) { // Get the line string line = sr.ReadLine(); string[] splitStr = line.Split("|", StringSplitOptions.RemoveEmptyEntries); if (splitStr.Length == 4) { // Get the row char char row = splitStr[0].ToUpper().Substring(0, 1)[0]; // Get the valid column int int column = -1; if (GeneralAssistance.CheckIfIntNumber(splitStr[0].Substring(1))) { column = int.Parse(splitStr[0].Substring(1)); } else { AuditLog.Log("ERROR: Re-stock file column was not a number!\t" + line); continue; } bool notValid = false; foreach (Slot slot in Slots) { if (slot.Row == row && slot.Column == column) { AuditLog.Log("ERROR: Re-stock file contains a product with the same location as another!\t" + line); notValid = true; break; } } if (notValid) { continue; } // Get product name string name = splitStr[1]; // Get the valid price decimal decimal price = -1; if (GeneralAssistance.CheckIfFloatingPointNumber(splitStr[2])) { price = Decimal.Parse(splitStr[2]); } else { AuditLog.Log("ERROR: Re-stock file price was not a valid floating point number!\t" + line); continue; } // Determine Product type switch (splitStr[3].ToLower()) { case "chip": Slots.Add(new Slot(row, column, new Chip(name, price))); Slots[Slots.Count - 1].Add(5); break; case "candy": Slots.Add(new Slot(row, column, new Candy(name, price))); Slots[Slots.Count - 1].Add(5); break; case "drink": Slots.Add(new Slot(row, column, new Drink(name, price))); Slots[Slots.Count - 1].Add(5); break; case "gum": Slots.Add(new Slot(row, column, new Gum(name, price))); Slots[Slots.Count - 1].Add(5); break; default: AuditLog.Log("ERROR: Re-stock file Product type was not a valid type!\t" + line); break; } } else { AuditLog.Log("ERROR: Re-stock file line does not have the right amount of peices!\t" + line); } } } } catch (IOException e) { AuditLog.Log("ERROR: Ran into a IOException.\t" + e.Message); } } else { AuditLog.Log("ERROR: Re-stock file not found!"); } }