public BookPurchaseResponse PurchaseBooks(BookPurchaseInfo info)
        {
            BookPurchaseResponse response = new BookPurchaseResponse();

            bool inStock = true;

            float price = 0.0f;

            List <Book> bookList = getAllBook();

            float budget = info.budget;
            Dictionary <int, int> items = info.items;

            foreach (KeyValuePair <int, int> entry in items)
            {
                Book b   = bookList[entry.Key - 1];
                int  qty = entry.Value;
                if (qty > b.Stock)
                {
                    inStock = false;
                }
                price += qty * b.Price;
            }

            if (!inStock)
            {
                response.result    = false;
                response.response += " No enough stocks !";
            }

            if (price > budget)
            {
                response.result    = false;
                response.response += " “No enough money !";
            }

            if (inStock && price <= budget)
            {
                response.result   = true;
                response.response = (budget - price).ToString();
            }

            return(response);
        }
Beispiel #2
0
        protected void btnPurchase_Click(object sender, EventArgs e)
        {
            int budget       = Convert.ToInt32(budgettxt.Text);
            int numberOfRows = Table1.Rows.Count;


            IBookPurchaseWebService bookPurchase = new BookPurchaseWebService();
            BookPurchaseInfo        info         = new BookPurchaseInfo();

            info.budget = budget;
            Dictionary <int, int> items = new Dictionary <int, int>();

            for (int i = 1; i <= Table1.Rows.Count; i++)
            {
                TextBox numbertxt = (TextBox)FindControl("numbertxt" + i);
                TextBox amounttxt = (TextBox)FindControl("amounttxt" + i);
                items.Add(Convert.ToInt32(numbertxt.Text), Convert.ToInt32(amounttxt.Text));
            }

            info.items = items;
            BookPurchaseResponse response = bookPurchase.PurchaseBooks(info);

            responsetxt.Text = response.response;
        }