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; }
protected void Button5_Click(object sender, EventArgs e) { ServiceReference1.ServiceClient sc1 = new ServiceReference1.ServiceClient(); BookPurchaseInfo info = new BookPurchaseInfo(); BookPurchaseResponse responce = new BookPurchaseResponse(); Boolean valid = true; try { float budget = float.Parse(TextBox9.Text); if (budget <= 0) { TextBox13.Text = "budget must be positive"; return; } info.budget = budget; info.items = new Dictionary <int, int>(); //info.items.Add(int.Parse(TextBox10.Text), int.Parse(TextBox11.Text)); for (int j = 0; j < count; j++) { TextBox tb1 = (TextBox)Panel1.FindControl("book" + j); TextBox tb2 = (TextBox)Panel1.FindControl("amount" + j); try { int key = int.Parse(tb1.Text); int value = int.Parse(tb2.Text); info.items.Add(key, value); } catch { TextBox13.Text = "Book number or amount is invalid"; } } responce.response = sc1.PurchaseBooks(info.budget, info.items, out responce.result); TextBox12.Text = responce.response; } catch { TextBox13.Text = "budget is invalid"; } }
public BookPurchaseResponse PurchaseBooks(BookPurchaseInfo info) { BookPurchaseResponse responce = new BookPurchaseResponse(); List <Book> books = GetAllBooks(); float budget = info.budget; Dictionary <int, int> items = info.items; float totalPrice = 0.0f; foreach (var item in items) { int key = item.Key; int value = item.Value; if (key > books.Count || key <= 0) { throw new Exception("Input is invalid"); } if (value > books[key - 1].stock) { responce.result = false; responce.response = "No enough stocks"; return(responce); } totalPrice = totalPrice + value * books[key - 1].price; } if (totalPrice > info.budget) { responce.response = "No enough money"; responce.result = false; return(responce); } responce.response = Convert.ToString(info.budget - totalPrice); responce.result = true; return(responce); }