public BookPurchaseInfo BookPurchaseInfo(string totalBudget, Dictionary <string, string> purchaseBook)
        {
            BookPurchaseInfo thisPurchaseInfo = new BookPurchaseInfo();

            if (String.IsNullOrWhiteSpace(totalBudget))
            {
                throw new NullReferenceException("Input field(s) is empty.");
            }
            else
            {
                try
                {
                    thisPurchaseInfo.budget = float.Parse(totalBudget);
                    thisPurchaseInfo.items  = purchaseBook.ToDictionary(
                        x => int.Parse(x.Key),
                        x => int.Parse(x.Value)
                        );
                }
                catch (Exception Ex)
                {
                    throw new Exception(Ex.Message);
                }
            }

            return(thisPurchaseInfo);
        }
        public BookPurchaseResponse PurchaseBooks(BookPurchaseInfo purchaseInfo)
        {
            float totalCost  = 0;
            float thisBudget = purchaseInfo.budget;

            Dictionary <int, int> items = purchaseInfo.items;

            foreach (KeyValuePair <int, int> item in items)
            {
                int thisID  = item.Key;
                int thisQty = item.Value;

                float getCost = getBookCost(thisID);
                int   getQty  = getBookQty(thisID);

                if (getQty < thisQty)
                {
                    thisResponse.result   = false;
                    thisResponse.response = "Not enough stock(s) available. You wanted:" + thisQty + "; stock(s) available: " + getQty;

                    return(thisResponse);
                }

                totalCost += (getCost * thisQty);
            }

            if (totalCost <= thisBudget)
            {
                thisResponse.result = true;

                try
                {
                    float balance = thisBudget - totalCost;
                    thisResponse.response = String.Format("Your change is: ${0:0.00}", balance);
                }
                catch (Exception Ex)
                {
                    throw new FaultException <Exception>(new Exception(Ex.Message));
                }
            }
            else
            {
                thisResponse.result   = false;
                thisResponse.response = "Not enough money. You are $" + String.Format("{0:0.00}", totalCost - thisBudget) + " short.";
            }
            return(thisResponse);
        }
Esempio n. 3
0
        protected void btnPurchase_Click(object sender, EventArgs e)
        {
            string totalBudget = txtTotalBudget.Text;
            Dictionary <string, string> purchasedBooks = new Dictionary <string, string>();

            string bookNo  = txtBookNumber_0.Text;
            string bookQty = txtQty_0.Text;

            purchasedBooks.Add(bookNo, bookQty);

            int  noOfFields = fieldQty;
            bool isBookNo   = false;
            bool isBookQty  = false;

            foreach (Control controls in placeHolderAddField.Controls)
            {
                if (controls is TextBox && controls.ID.Contains("txtBookNumber_"))
                {
                    bookNo   = ((TextBox)controls).Text;
                    isBookNo = true;
                }

                if (controls is TextBox && controls.ID.Contains("txtQty_"))
                {
                    bookQty   = ((TextBox)controls).Text;
                    isBookQty = true;
                }

                if (isBookNo && isBookQty)
                {
                    try
                    {
                        purchasedBooks.Add(bookNo, bookQty);
                        isBookNo  = false;
                        isBookQty = false;
                    }
                    catch (Exception Ex)
                    {
                        System.Windows.Forms.MessageBox.Show(
                            Ex.Message,
                            "Error Occurred",
                            System.Windows.Forms.MessageBoxButtons.OK,
                            System.Windows.Forms.MessageBoxIcon.Error
                            );
                    }
                }
            }

            try
            {
                BookPurchaseInfo     thisBookPurchaseInfo = thisBookPurchaseService.BookPurchaseInfo(totalBudget, purchasedBooks);
                BookPurchaseResponse thisPurchaseResponse = thisBookPurchaseService.PurchaseBooks(thisBookPurchaseInfo);
                txtPurchase.Text = thisPurchaseResponse.response;
            }
            catch (Exception Ex)
            {
                System.Windows.Forms.MessageBox.Show(
                    Ex.Message,
                    "Error Occurred",
                    System.Windows.Forms.MessageBoxButtons.OK,
                    System.Windows.Forms.MessageBoxIcon.Exclamation
                    );
            }
        }