Esempio n. 1
0
    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";
    }
Esempio n. 2
0
    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)
    {
        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";
            }
        }
    }
Esempio n. 4
0
 protected void BookList_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (BookList.SelectedValue != "-1")
     {
         Book book = BookCatalogDataAccess.GetBookById(BookList.SelectedValue);
         lblPrice.Text       = String.Format("{0:C}", book.Price);
         lblDescription.Text = book.Description;
     }
     else
     {
         lblPrice.Text       = "";
         lblDescription.Text = "";
     }
 }
Esempio n. 5
0
 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;
     }
 }
Esempio n. 6
0
    protected void btnAddToCart_Click(object sender, EventArgs e)
    {
        Validate();
        if (Page.IsValid)
        {
            Book book     = BookCatalogDataAccess.GetBookById(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;
        }
    }
    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 = "";
        }
    }
    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);
    }
Esempio n. 10
0
    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";
            }
        }
    }