Exemple #1
0
        /// <sell>
        ///  This method takes from the console information for a sell.
        ///  (buyer nickname, drug name, drug quantity)
        ///  It decreases the quantity on the specified drug from the database with the specified quantity,
        ///  increases the balance with the sell price of the drug multiplied by the selected quantity,
        ///  and increases the dealer's 'money_brought_this_month'.
        /// </sell>
        public void Sell()
        {
            Dealer dealer;
            Buyer  buyer;
            Drug   drug;
            int    quantity;

            try
            {
                Dictionary <string, string> dict = CommandReader.Sell();
                OutputPrinter.Connecting();

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

                try
                {
                    buyer = c.Buyers.Single(e => e.Nickname == dict["bNickname"]);
                }
                catch (Exception)
                {
                    throw new InvalidOperationException(ConstantStrings.Buyer + " " + ConstantStrings.NotFound);
                }

                dealer = c.Dealers.Single(e => e.Id == buyer.DealerId);

                try
                {
                    drug = c.Drugs.Single(e => e.Name == dict["drugName"]);
                }
                catch (Exception)
                {
                    throw new InvalidOperationException(ConstantStrings.Drug + " " + 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);
                }

                if (quantity > drug.Quantity)
                {
                    throw new InvalidOperationException(ConstantStrings.NotEnough + $" {drug.Name}!");
                }

                double price = drug.Sell_Price * quantity;

                money.Money_Amount += price;
                dealer.Money_Brought_This_Month += price;
                Sel(drug, quantity);
                OutputPrinter.Done();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                OutputPrinter.InvalidCommand();
            }
        }