Пример #1
0
 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();
 }
Пример #3
0
 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();
 }
Пример #5
0
        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);
        }
Пример #6
0
        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;
        }
Пример #7
0
 public void SaveProduct(Product product)
 {
     SetProduct(product);
     SaveProducts();
 }
Пример #8
0
 private static bool ValidateUserHasEnoughMoney(User user, Product product, int quantity)
 {
     if (user.Balance < product.Price * quantity)
     {
         PrintInsufficientFundsMessage();
         return false;
     }
     return true;
 }
Пример #9
0
 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")));
 }
Пример #10
0
 private static bool NotEnoughProductQuantityAvailable(Product product, int quantity)
 {
     return product.Quantity <= quantity;
 }
Пример #11
0
 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();
 }
Пример #12
0
 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();
 }
Пример #13
0
        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;
        }
Пример #14
0
 public bool UserHasFundsForPurchase(Product product, int quantity)
 {
     double totalPurchasePrice = product.Price * quantity;
     return user.Balance >= totalPurchasePrice;
 }
Пример #15
0
 private static string GetFormattedProductText(Product product, int productIndex)
 {
     return String.Format("{0}: {1} ({2:C})", productIndex, product.Name, product.Price);
 }
Пример #16
0
 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();
 }
Пример #17
0
 private static bool UserDoesNotHaveEnoughMoneyForPurchase(Product product, double balance, int quantity)
 {
     return balance - product.Price * quantity < 0;
 }
Пример #18
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"));
 }
Пример #19
0
        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;            
        }
Пример #20
0
 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"));
 }
Пример #21
0
 public bool StoreHasStockForPurchase(Product product, int quantity)
 {
     return product.Quantity >= quantity;
 }
Пример #22
0
 public bool IsUserBalanceSufficient(Product currentProduct, int quantityRequested)
 {
     return RemainingBalance - currentProduct.getTotalCost(quantityRequested) >= 0;
 }
Пример #23
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;
        }
Пример #24
0
 private static double UpdateUserBalance(double userBalance, Product selectedProduct, int requestedQuantity)
 {
     userBalance = userBalance - selectedProduct.Price * requestedQuantity;
     return userBalance;
 }
Пример #25
0
        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);

            }
        }
Пример #26
0
 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();
 }
Пример #27
0
        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;
        }
Пример #28
0
 private static void TakeFundsFromUser(User user, Product product, int quantity)
 {
     user.Balance = user.Balance - product.Price * quantity;
 }
Пример #29
0
 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();
 }
Пример #30
0
 private static bool ValidateSufficientQuantityExists(Product product, int quantity)
 {
     if (product.Quantity < quantity)
     {
         PrintInsufficientStockMessage(product);
         return false;
     }
     return true;
 }