public static bool CreatePurchaseEntry(Snacks snackBeingSold, decimal balanceBefore) { string directory = Directory.GetCurrentDirectory(); string fileName = "Log.txt"; string fullPath = Path.Combine(directory, fileName); bool wroteAuditEntry; try { if (!File.Exists(fullPath)) { using (StreamWriter sw = File.CreateText(fullPath)); } using (StreamWriter sw = File.AppendText(fullPath)) { sw.Write(DateTime.Now); sw.Write($" {snackBeingSold.SnackName} {snackBeingSold.SnackCode}"); sw.Write($" ${balanceBefore} ${balanceBefore - snackBeingSold.SnackPrice}\n"); wroteAuditEntry = true; } } catch (Exception ex) { Console.WriteLine(ex.Message); wroteAuditEntry = false; } return(wroteAuditEntry); }
public bool PurchaseTransaction(Snacks snackBeingSold) { bool transactionSuccessful; if (snackBeingSold.SnackPrice > CurrentBalance) { transactionSuccessful = false; Console.WriteLine("You didn't feed me enough money to buy that. Munny pweeeez!"); } else { transactionSuccessful = true; AuditEntry.CreatePurchaseEntry(snackBeingSold, CurrentBalance); CurrentBalance -= snackBeingSold.SnackPrice; } return(transactionSuccessful); }
//Removed the Dictionary return type, because the Dispense() already has access to the Dictionary //Changed the return type to Snacks, so we can return the snack for testing purposes public Snacks Dispense(string userEnteredCode) { if (Inventory.ContainsKey(userEnteredCode) && Inventory[userEnteredCode].SnackQuantity > 0) { Snacks selectedSnack = Inventory[userEnteredCode]; selectedSnack.SnackQuantity -= 1; Inventory[userEnteredCode] = selectedSnack; //moneyFunctions.PurchaseTransaction(selectedSnack); Console.WriteLine(selectedSnack.Message()); return(selectedSnack); } else if (Inventory.ContainsKey(userEnteredCode) && Inventory[userEnteredCode].SnackQuantity == 0) { Console.WriteLine("Sorry, that one is sold out!"); return(null); } else { Console.WriteLine("Hm, coudln't find that code... Let's go back to the Purchasing Menu"); return(null); } }