private void SetProduct(Product newProduct) { Product currentProduct = products.FirstOrDefault(p => p.Name.Equals(newProduct.Name)); if (currentProduct != null) { currentProduct.Quantity = newProduct.Quantity; } }
internal static void DisplayUserPurchase(User user, Product product, int quantity) { Console.Clear(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("You bought " + quantity + " " + product.Name); Console.WriteLine("Your new balance is " + user.Balance.ToString("C")); Console.ResetColor(); }
private void WriteSuccessfulPurchaseMessage(Product product, int purchaseQuantity) { Console.Clear(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("You bought " + purchaseQuantity + " " + product.Name); Console.WriteLine("Your new balance is " + user.Balance.ToString("C")); Console.ResetColor(); }
internal static void DisplayUserInsufficientQuantityMessage(Product product) { Console.Clear(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(); Console.WriteLine("Sorry, " + product.Name + " is out of stock"); Console.ResetColor(); }
public void Purchase(Product product, int quantity) { if (!UserHasFundsForPurchase(product, quantity)) { throw new InsufficientFundsException(); } if (!StoreHasStockForPurchase(product, quantity)) { throw new OutOfStockException(); } product.Quantity = product.Quantity - quantity; user.Balance = user.Balance - product.Price * quantity; dataManager.SaveUser(user); dataManager.SaveProduct(product); }
public static bool isProductOkToPurchase(User currentUser, Product currentProduct, int quantityToPurchase) { bool okToPurchase = true; if (!currentUser.IsUserBalanceSufficient(currentProduct, quantityToPurchase)) { UI.printInsufficientBalance(); okToPurchase = false; } else if (currentProduct.Qty <= quantityToPurchase) { UI.printProductOutOfStock(currentProduct); okToPurchase = false; } else if (quantityToPurchase < 0) { UI.printQuantityLessThanZero(); okToPurchase = false; } return okToPurchase; }
public void SaveProduct(Product product) { SetProduct(product); SaveProducts(); }
private static bool ValidateUserHasEnoughMoney(User user, Product product, int quantity) { if (user.Balance < product.Price * quantity) { PrintInsufficientFundsMessage(); return false; } return true; }
private static void PrintConfirmPurchaseMessage(Product product, User user) { Console.WriteLine(); Console.WriteLine(Resources.msgWantToBuy + product.Name); Console.WriteLine(string.Format(Resources.msgBalance, user.Balance.ToString("C"))); }
private static bool NotEnoughProductQuantityAvailable(Product product, int quantity) { return product.Quantity <= quantity; }
private static void PrintInsufficientStockMessage(Product product) { Console.Clear(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(); if (product.Quantity == 0) { Console.WriteLine(string.Format(Resources.msgOutOfStock, product.Name)); } else { Console.WriteLine(string.Format(Resources.msgInsufficientStock, product.Quantity, product.Name)); } Console.ResetColor(); }
private static void DisplayReceiptMessage(double balance, Product selectedProduct, int requestedQuantity) { Console.Clear(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("You bought " + requestedQuantity + " " + selectedProduct.Name); Console.WriteLine("Your new balance is " + balance.ToString("C")); Console.ResetColor(); }
private static double ProcessPurchase(Product product, double balance, int quantity) { balance = balance - product.Price * quantity; product.Quantity = product.Quantity - quantity; ShowReceipt(product, balance, quantity); return balance; }
public bool UserHasFundsForPurchase(Product product, int quantity) { double totalPurchasePrice = product.Price * quantity; return user.Balance >= totalPurchasePrice; }
private static string GetFormattedProductText(Product product, int productIndex) { return String.Format("{0}: {1} ({2:C})", productIndex, product.Name, product.Price); }
private static void WriteOutOfStockMessage(Product product) { Console.Clear(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(); Console.WriteLine("Sorry, " + product.Name + " is out of stock"); Console.ResetColor(); }
private static bool UserDoesNotHaveEnoughMoneyForPurchase(Product product, double balance, int quantity) { return balance - product.Price * quantity < 0; }
private void WriteProductToPurchaseMessage(Product product) { Console.WriteLine(); Console.WriteLine("You want to buy: " + product.Name); Console.WriteLine("Your balance is " + user.Balance.ToString("C")); }
private static int GetPurchaseQuantity(Product product) { int purchaseQuantity; bool validIntegerEntered = Int32.TryParse(ReadText("Enter amount to purchase: "), out purchaseQuantity); while (!validIntegerEntered) { Console.WriteLine("You have entered an invalid purchase quantity."); } return purchaseQuantity; }
private static void DisplayPurchaseSummary(Product product, double balance) { Console.WriteLine(); Console.WriteLine("You want to buy: " + product.Name); Console.WriteLine("Your balance is " + balance.ToString("C")); }
public bool StoreHasStockForPurchase(Product product, int quantity) { return product.Quantity >= quantity; }
public bool IsUserBalanceSufficient(Product currentProduct, int quantityRequested) { return RemainingBalance - currentProduct.getTotalCost(quantityRequested) >= 0; }
private static double PurchaseProduct(Product product, double balance) { Console.WriteLine(); Console.WriteLine("You want to buy: " + product.Name); Console.WriteLine("Your balance is " + balance.ToString("C")); int quantity = Convert.ToInt32(PromptInput("Enter amount to purchase:")); balance = ValidatePurchase(product, balance, quantity); return balance; }
private static double UpdateUserBalance(double userBalance, Product selectedProduct, int requestedQuantity) { userBalance = userBalance - selectedProduct.Price * requestedQuantity; return userBalance; }
private static void ProcessPurchase(int qty, Product prod, double bal) { // Check if balance - quantity * price is less than 0 if (bal - prod.Price * qty < 0) { OutPutToConsole("You do not have enough money to buy that.", true, true, ConsoleColor.Red); return; } // Check if quantity is less than quantity if (prod.Qty <= qty) { OutPutToConsole("Sorry, " + prod.Name + " is out of stock", true, true, ConsoleColor.Red); return; } // Check if quantity is greater than zero if (qty > 0) { // Balance = Balance - Price * Quantity bal = bal - prod.Price * qty; // Quanity = Quantity - Quantity prod.Qty = prod.Qty - qty; OutPutToConsole("You bought " + qty + " " + prod.Name + Environment.NewLine + "Your new balance is " + bal.ToString("C"), true, true, ConsoleColor.Green); } else { // Quantity is less than zero OutPutToConsole("Purchase cancelled", true, true, ConsoleColor.Yellow); } }
private static void PrintPurchaseSuccessfulMessage(User user, Product product, int quantity) { Console.Clear(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(string.Format(Resources.msgPurchased, quantity, product.Name)); Console.WriteLine(string.Format(Resources.msgNewBalance, user.Balance.ToString("C"))); Console.ResetColor(); }
private static double ValidatePurchase(Product product, double balance, int quantity) { if (product.HasEnoughBalance(balance, quantity)) { ShowMessage(ConsoleColor.Red, "You do not have enough money to buy that."); } else if (product.IsInStock(quantity)) { ShowMessage(ConsoleColor.Red, "Sorry, " + product.Name + " is out of stock"); } else if (quantity <= 0) { ShowMessage(ConsoleColor.Yellow, "Purchase cancelled"); } else { balance = ProcessPurchase(product, balance, quantity); } return balance; }
private static void TakeFundsFromUser(User user, Product product, int quantity) { user.Balance = user.Balance - product.Price * quantity; }
private static void ShowReceipt(Product product, double balance, int quantity) { Console.Clear(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("You bought " + quantity + " " + product.Name); Console.WriteLine("Your new balance is " + balance.ToString("C")); Console.ResetColor(); }
private static bool ValidateSufficientQuantityExists(Product product, int quantity) { if (product.Quantity < quantity) { PrintInsufficientStockMessage(product); return false; } return true; }