protected void drpBookSelection_SelectedIndexChanged(object sender, EventArgs e) { if (drpBookSelection.SelectedValue != "-1") { string bookId = drpBookSelection.SelectedItem.Value; Book selectedBook = BookCatalogDataAccess.GetBookById(bookId); //todo: Add selected book to the session List <Book> books = Session["books"] as List <Book>; if (books == null) { books = new List <Book>(); Session["books"] = books; } books.Add(selectedBook); //todo: Display the selected book's description and price lblDescription.Text = selectedBook.Description; lblPrice.Text = "$" + selectedBook.Price.ToString(); } else { //todo: Set description and price to blank lblDescription.Text = ""; lblPrice.Text = ""; } }
protected void btnAddToCart_Click(object sender, EventArgs e) { //Todo: Add your code here if (!Page.IsValid) { return; } var shoppingCart = (ShoppingCart)Session["cart"]; var availableBooks = (List <Book>)Session["books"]; Book selectedBook = BookCatalogDataAccess.GetBookById(drpBookSelection.SelectedValue); BookOrder bookOrder = new BookOrder(selectedBook, int.Parse(txtQuantity.Text)); // updated the selected book list shoppingCart.AddBookOrder(bookOrder); // make a selected book unavailable //availableBooks.Remove(availableBooks.Where(b => b.Id == selectedBook.Id).FirstOrDefault<Book>()); drpBookSelection.Items.Remove(drpBookSelection.Items.FindByValue(selectedBook.Id)); availableBooks.RemoveAll(b => b.Id == selectedBook.Id); // display messages lblDescription.Text = $"{txtQuantity.Text} copy of {selectedBook.Title} is added to the shopping cart"; lblNumItems.Text = shoppingCart.NumOfItems.ToString(); drpBookSelection.SelectedValue = "-1"; }
protected void btnAddToCart_Click(object sender, EventArgs e) { if (drpBookSelection.SelectedValue != "-1" && Session["shoppingcart"] != null) { //////todo: Retrieve selected book from the session var book = BookCatalogDataAccess.GetBookById(drpBookSelection.SelectedValue); ////todo: get user entered quqntity int quantity = Int16.Parse(txtQuantity.Text); ////todo: Create a book order with selected book and quantity BookOrder order = new BookOrder(book, quantity); ////todo: Retrieve to cart from the session var cart = (ShoppingCart)Session["shoppingcart"]; ////todo: Add book order to the shopping cart cart.AddBookOrder(order); ////todo: Remove the selected item from the dropdown list drpBookSelection.Items.RemoveAt(drpBookSelection.SelectedIndex); ////todo: Set the dropdown list's selected value as "-1" drpBookSelection.ClearSelection(); txtQuantity.Text = string.Empty; ////todo: Set the description to show title and quantity of the book user added to the shopping cart lblDescription.Text = "You have added " + quantity + " copies of: " + book.Title; ////todo: Update the number of items in shopping cart displayed next to the link to ShoppingCartView.aspx if (cart.NumOfItems == 0) { lblNumItems.Text = "empty"; } else if (cart.NumOfItems == 1) { lblNumItems.Text = "1 item"; } else { lblNumItems.Text = cart.NumOfItems.ToString() + " items"; } } }
protected void BookList_SelectedIndexChanged(object sender, EventArgs e) { if (BookList.SelectedValue != "-1") { Book book = BookCatalogDataAccess.GetABookById(BookList.SelectedValue); lblPrice.Text = String.Format("{0:C}", book.Price); lblDescription.Text = book.Description; } else { lblPrice.Text = ""; lblDescription.Text = ""; } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) //on 1st Page Load { //get all books in the catalog. List <Book> books = BookCatalogDataAccess.GetAllBooks(); foreach (Book book in books) { //todo: Populate dropdown list selections ListItem item = new ListItem(book.Title, book.Id); drpBookSelection.Items.Add(item); } } ShoppingCart shoppingcart = null; if (Session["shoppingcart"] == null) { shoppingcart = new ShoppingCart(); //todo: add cart to the session Session["shoppingcart"] = shoppingcart; } else { //todo: retrieve cart from the session shoppingcart = Session["shoppingcart"] as ShoppingCart; foreach (BookOrder order in shoppingcart.BookOrders) { string bookId = order.Book.Id; //todo: Remove the book in the order from the dropdown list ListItem itemToRemove = drpBookSelection.Items.FindByValue(bookId); drpBookSelection.Items.Remove(itemToRemove); } } if (shoppingcart.NumOfItems == 0) { lblNumItems.Text = "empty"; } else if (shoppingcart.NumOfItems == 1) { lblNumItems.Text = "1 item"; } else { lblNumItems.Text = shoppingcart.NumOfItems.ToString() + " items"; } }
protected void drpBookSelection_SelectedIndexChanged(object sender, EventArgs e) { //Todo: Add your code here if (drpBookSelection.SelectedValue != "-1") { Book selectedBook = BookCatalogDataAccess.GetBookById(drpBookSelection.SelectedValue); lblPrice.Text = selectedBook.Price.ToString(); lblDescription.Text = selectedBook.Description; } else { lblDescription.Visible = false; lblPrice.Visible = false; } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { cart.EmptyShoppingCart(); //get all books in the catalog. ArrayList books = BookCatalogDataAccess.GetAllBooks(); BookList.DataSource = books; BookList.DataTextField = "Title"; BookList.DataValueField = "Id"; BookList.DataBind(); BookList.Items.Insert(0, new ListItem("Select a Book ...", "-1")); } btnViewCart.Text = "View Cart (" + cart.NumOfItems + " items)"; }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ////get all books in the catalog. List <Book> books = BookCatalogDataAccess.GetAllBooks(); foreach (Book book in books) ////todo: Populate dropdown list selections { ListItem item = new ListItem(book.Title, book.Id); drpBookSelection.Items.Add(item); } } ShoppingCart cart = null; if (Session["shoppingcart"] == null) { cart = new ShoppingCart(); ////todo: add cart to the session YES Session["shoppingcart"] = cart; } else { ////todo: retrieve cart from the session YES cart = (ShoppingCart)Session["shoppingcart"]; foreach (BookOrder order in cart.BookOrders) { //todo: Remove the book in the order from the dropdown list // Removing it here seems redundant when I remove when after adding them //to my cart in the btnAddToCart_Click() method? } } if (cart.NumOfItems == 0) { lblNumItems.Text = "empty"; } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //get all books in the catalog - OK List <Book> books = BookCatalogDataAccess.GetAllBooks(); foreach (Book book in books) { //todo: Populate dropdown list selections - OK ListItem item = new ListItem(book.Title, book.Id); //creating a new list with book title drpBookSelection.Items.Add(item); //printing the list } } ShoppingCart shoppingcart = null; if (Session["shoppingcart"] == null) { shoppingcart = new ShoppingCart(); //todo: add cart to the session OK Session["shoppingcart"] = shoppingcart; } else { //todo: retrieve cart from the session OK shoppingcart = (ShoppingCart)Session["shoppingcart"]; foreach (BookOrder order in shoppingcart.BookOrders) { //todo: Remove the book in the order from the dropdown list - OK drpBookSelection.Items.Remove(drpBookSelection.Items.FindByValue(order.Book.Id)); } } if (shoppingcart.NumOfItems == 0) { lblNumItems.Text = "empty"; } else if (shoppingcart.NumOfItems == 1) { lblNumItems.Text = "1 item"; } else { lblNumItems.Text = shoppingcart.NumOfItems.ToString() + " items"; } }
protected void btnAddToCart_Click(object sender, EventArgs e) { Validate(); if (Page.IsValid) { Book book = BookCatalogDataAccess.GetABookById(BookList.SelectedValue); int quantity = Int16.Parse(txtQauntity.Text); BookOrder order = new BookOrder(book, quantity); cart.AddBookOrder(order); BookList.Items.RemoveAt(BookList.SelectedIndex); string amount = quantity > 1 ? "copies" : "copy"; lblDescription.Text = quantity + " " + amount + " of " + book.Title + " have been added to your cart"; btnViewCart.Text = "View Cart (" + cart.NumOfItems + " items)"; BookList.ClearSelection(); txtQauntity.Text = ""; } }
protected void drpBookSelection_SelectedIndexChanged(object sender, EventArgs e) { if (drpBookSelection.SelectedValue != "-1") { string bookId = drpBookSelection.SelectedItem.Value; Book selectedBook = BookCatalogDataAccess.GetBookById(bookId); //Add selected book to the session Session["book"] = selectedBook; //Display the selected book's description and price lblDescription.Text = selectedBook.Description; lblPrice.Text = "$" + selectedBook.Price; } else { //Set description and price to blank lblDescription.Text = string.Empty; lblPrice.Text = string.Empty; } }
public static ArrayList RetrieveAllBookOrders() { ArrayList orders = new ArrayList(); StreamReader sr = null; try { string path = HttpContext.Current.Request.PhysicalApplicationPath; string filePath = path + @"\App_Data\BookOrderList.txt"; if (!File.Exists(filePath)) { return(orders); } FileStream bookOrderList = new FileStream(filePath, FileMode.Open, FileAccess.Read); sr = new StreamReader(bookOrderList); while (!sr.EndOfStream) { string bookId = sr.ReadLine(); int quantity = int.Parse(sr.ReadLine()); Book book = BookCatalogDataAccess.GetBookById(bookId); BookOrder bookOrder = new BookOrder(book, quantity); orders.Add(bookOrder); } } catch (Exception) { ClearBookOrderList(); } finally { if (sr != null) { sr.Close(); } } return(orders); }
protected void drpBookSelection_SelectedIndexChanged(object sender, EventArgs e) { if (drpBookSelection.SelectedValue != "-1") { string bookId = drpBookSelection.SelectedItem.Value; Book selectedBook = BookCatalogDataAccess.GetBookById(bookId); ////todo: Add selected book to the session ///Why do I add the selected book to the session when the index changes and not when added to cart? //todo: Display the selected book's description and price lblPrice.Text = String.Format("{0:C}", selectedBook.Price); lblDescription.Text = selectedBook.Description; } else { //todo: Set description and price to blank YES lblPrice.Text = ""; lblDescription.Text = ""; } }
protected void btnAddToCart_Click(object sender, EventArgs e) { if (drpBookSelection.SelectedValue != "-1" && Session["shoppingcart"] != null) { string bookId = drpBookSelection.SelectedItem.Value; Book selectedBook = BookCatalogDataAccess.GetBookById(bookId); //todo: Retrieve selected book from the session //Book book = Session["book"] as Book; //todo: get user entered quqntity int quantity = int.Parse(txtQuantity.Text); //todo: Create a book order with selected book and quantity BookOrder order = new BookOrder(selectedBook, quantity); //todo: Retrieve to cart from the session ShoppingCart cart = Session["shoppingcart"] as ShoppingCart; //todo: Add book order to the shopping cart cart.AddBookOrder(order); //todo: Remove the selected item from the dropdown list drpBookSelection.Items.Remove(drpBookSelection.SelectedItem); //todo: Set the dropdown list's selected value as "-1" //drpBookSelection.SelectedValue = "-1"; //todo: Set the description to show title and quantity of the book user added to the shopping cart lblDescription.Text = quantity.ToString() + " copy(s) of " + selectedBook.Title + " is added to the shopping cart"; //todo: Update the number of items in shopping cart displayed next to the link to ShoppingCartView.aspx if (cart.NumOfItems == 0) { lblNumItems.Text = "empty"; } else if (cart.NumOfItems == 1) { lblNumItems.Text = "1 item"; } else { lblNumItems.Text = cart.NumOfItems.ToString() + " items"; } } }
protected void Page_Load(object sender, EventArgs e) { //Todo: Add you code here List <Book> availableBooks = BookCatalogDataAccess.GetAllBooks();; ShoppingCart shoppingCart; if (Session["books"] == null) { Session["books"] = availableBooks; } else { availableBooks = (List <Book>)Session["books"]; } if (Session["cart"] == null) { shoppingCart = new ShoppingCart(); Session["cart"] = shoppingCart; lblNumItems.Text = "empty"; } else { shoppingCart = (ShoppingCart)Session["cart"]; lblNumItems.Text = shoppingCart.NumOfItems.ToString(); } // display the available book list if (!IsPostBack) { for (int i = 0; i < availableBooks.Count; i++) { drpBookSelection.Items.Add(new ListItem(availableBooks[i].Title, availableBooks[i].Id)); } } }
protected void drpBookSelection_SelectedIndexChanged(object sender, EventArgs e) { if (drpBookSelection.SelectedValue != "-1") { //todo: Display the selected book's description and price OK List <Book> books = BookCatalogDataAccess.GetAllBooks(); //creating the list of all books string bookId = drpBookSelection.SelectedItem.Value; //creating the string ID for the selected item int bookIdInt = int.Parse(drpBookSelection.SelectedItem.Value) - 1; //parsing ID into int Book selectedBook = books[bookIdInt]; //selecting book from list String bookDescription = selectedBook.Description; //creating description string lblDescription.Text = bookDescription; //adding the description information String bookPrice = selectedBook.Price.ToString(); //creating price string lblPrice.Text = bookPrice; //adding the price information //todo: Add selected book to the session OK Session["selectedBooksSession"] = selectedBook; } else { //todo: Set description and price to blank OK lblDescription.Text = ""; lblPrice.Text = ""; } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //get all books in the catalog. List <Book> books = BookCatalogDataAccess.GetAllBooks(); foreach (Book book in books) { //todo: Populate dropdown list selections ShoppingCart shoppingCart = (ShoppingCart)Session["shoppingcart"]; bool has = false; if (shoppingCart != null && shoppingCart.BookOrders != null) { foreach (BookOrder bookOrder in shoppingCart.BookOrders) { if (book.Id.Equals(bookOrder.Book.Id)) { has = true; break; } } } if (!has) { drpBookSelection.Items.Add(new ListItem(book.Title, book.Id + "")); } } } ShoppingCart shoppingcart = null; if (Session["shoppingcart"] == null) { shoppingcart = new ShoppingCart(); //todo: add cart to the session Session["shoppingcart"] = shoppingcart; } else { //todo: retrieve cart from the session shoppingcart = (ShoppingCart)Session["shoppingcart"]; foreach (BookOrder order in shoppingcart.BookOrders) { //todo: Remove the book in the order from the dropdown list //done } } if (shoppingcart.NumOfItems == 0) { lblNumItems.Text = "empty"; } else if (shoppingcart.NumOfItems == 1) { lblNumItems.Text = "1 item"; } else { lblNumItems.Text = shoppingcart.NumOfItems.ToString() + " items"; } }