protected void PurchaseBook(object sender, EventArgs e) { float f; bool var = true; if (!(float.TryParse(budget.Text, out f))) { outputPurchase.Text = "Wrong Input"; } else { float Budget = float.Parse(budget.Text); Dictionary <int, int> buy = new Dictionary <int, int>(); //buy.Add(Int32.Parse(TextBox1.Text), Int32.Parse(TextBox2.Text)); Console.WriteLine("rows"); Console.WriteLine(Table3.Rows.Count); Label11.Text = (Table3.Rows.Count).ToString(); for (var i = 0; i < Table3.Rows.Count; i++) { try { string numID = "numBook" + (i); string qtyID = "quantity" + (i); TableRow row = Table3.Rows[i]; Label10.Text = ((TextBox)row.Cells[1].FindControl(numID)).Text; Label9.Text = ((TextBox)row.Cells[3].FindControl(qtyID)).Text; string textCell = ((TextBox)row.Cells[1].FindControl(numID)).Text; string textCell2 = ((TextBox)row.Cells[3].FindControl(qtyID)).Text; int n; if (textCell == "" || textCell2 == "") { buy.Add(Int32.Parse(textCell), Int32.Parse(textCell2)); } else if (!(Int32.TryParse(textCell, out n)) || !(Int32.TryParse(textCell2, out n))) { var = false; break; } else { buy.Add(Int32.Parse(textCell), Int32.Parse(textCell2)); } } catch { } } if (var == true) { BookPurchaseResponse output = service.BookPurchaseInfo(Budget, buy); outputPurchase.Text = output.response; } else { outputPurchase.Text = "wrong input"; } } }
// change "books.txt" the path to the location of books.txt public BookPurchaseResponse BookPurchaseInfo(float budget, Dictionary <int, int> items) { List <IBook> books = this.GetAllBooks(); float price = 0; BookPurchaseResponse response = new BookPurchaseResponse(); foreach (int key in items.Keys) { if (key > (books.Count)) { response.result = false; response.response = "The book does not exist"; return(response); } else if (books[key - 1].Stock < items[key]) { response.result = false; response.response = "No enough stocks"; return(response); } price = price + (books[key - 1].Price * items[key]); } if (price > budget) { response.result = false; response.response = "No enough money"; return(response); } float result = budget - price; response.result = true; response.response = Convert.ToString(result); return(response); }