Exemple #1
0
        /// <buyDrugs>
        ///  This method takes from the console information for a drug purchase.
        ///  (existing drug name, drug quantity)
        ///  It increases the quantity on the specified drug from the database with the specified quantity,
        ///  and decreases the balance with the acquire price of the drug multiplied by the selected quantity.
        /// </buyDrugs>
        public void BuyDrugs()
        {
            Drug drug;
            int  quantity;

            try
            {
                Dictionary <string, string> dict = CommandReader.Buy();

                if (dict["drugName"] == "" || dict["quantity"] == "")
                {
                    throw new InvalidOperationException(ConstantStrings.Blank);
                }

                OutputPrinter.Connecting();
                try
                {
                    drug = c.Drugs.Single(e => e.Name == dict["drugName"]);
                }
                catch (Exception)
                {
                    throw new InvalidOperationException(ConstantStrings.Drug + $" with name '{dict["drugName"]}' " + ConstantStrings.NotFound);
                }

                try
                {
                    quantity = int.Parse(dict["quantity"]);
                }
                catch (Exception)
                {
                    throw new InvalidOperationException(ConstantStrings.Quantity + " " + ConstantStrings.ValidNumber);
                }

                if (quantity <= 0)
                {
                    throw new InvalidOperationException(ConstantStrings.Quantity + " " + ConstantStrings.PositiveNumber);
                }

                double price = drug.Acquire_Price * quantity;

                if (price > money.Money_Amount)
                {
                    throw new InvalidOperationException(ConstantStrings.NotEnough + " " + ConstantStrings.Money.ToLower() + "!");
                }

                money.Money_Amount -= price;

                Buy(drug, quantity);
                OutputPrinter.Done();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                OutputPrinter.InvalidCommand();
            }
        }