Package com.sun.bookstore.database

Examples of com.sun.bookstore.database.Book


                    "Could not get books: " + ex.getMessage());
        }
    }

    public Book getBook(String bookId) throws BookNotFoundException {
        Book requestedBook = em.find(Book.class, bookId);

        if (requestedBook == null) {
            throw new BookNotFoundException("Couldn't find book: " + bookId);
        }
View Full Code Here


        Iterator i = items.iterator();

        try {
            while (i.hasNext()) {
                ShoppingCartItem sci = (ShoppingCartItem) i.next();
                Book bd = (Book) sci.getItem();
                String id = bd.getBookId();
                int quantity = sci.getQuantity();
                buyBook(id, quantity);
            }
        } catch (Exception ex) {
            throw new OrderException("Commit failed: " + ex.getMessage());
View Full Code Here

    public void buyBook(
        String bookId,
        int quantity) throws OrderException {
        try {
            Book requestedBook = em.find(Book.class, bookId);

            if (requestedBook != null) {
                int inventory = requestedBook.getInventory();

                if ((inventory - quantity) >= 0) {
                    int newInventory = inventory - quantity;
                    requestedBook.setInventory(newInventory);
                } else {
                    throw new OrderException(
                            "Not enough of " + bookId
                            + " in stock to complete order.");
                }
View Full Code Here

        }

        /* Handle any pending deletes from the shopping cart and
           indicate the outcome as part of the response */
        String bookId = request.getParameter("Remove");
        Book bd;

        if (bookId != null) {
            try {
                bd = bookDB.getBook(bookId);
                cart.remove(bookId);
                out.println(
                        "<font color=\"#ff00000\" size=\"+2\">"
                        + messages.getString("CartRemoved") + "<strong>"
                        + bd.getTitle() + "</strong> <br> &nbsp; <br>"
                        + "</font>");
            } catch (BookNotFoundException ex) {
                response.reset();
                throw new ServletException(ex);
            }
        } else if (request.getParameter("Clear") != null) {
            cart.clear();
            out.println(
                    "<font color=\"#ff0000\" size=\"+2\"><strong>"
                    + messages.getString("CartCleared")
                    + "</strong> <br>&nbsp; <br> </font>");
        }

        // Print a summary of the shopping cart
        int num = cart.getNumberOfItems();

        if (num > 0) {
            out.println(
                    "<font size=\"+2\">" + messages.getString("CartContents")
                    + num
                    + ((num == 1) ? messages.getString("CartItem")
                                  : messages.getString("CartItems"))
                    + "</font><br>&nbsp;");

            // Return the Shopping Cart
            out.println(
                    "<table summary=\"layout\">" + "<tr>" + "<th align=left>"
                    + messages.getString("ItemQuantity") + "</TH>"
                    + "<th align=left>" + messages.getString("ItemTitle")
                    + "</TH>" + "<th align=left>"
                    + messages.getString("ItemPrice") + "</TH>" + "</tr>");

            Iterator i = cart.getItems()
                             .iterator();
            Currency c = (Currency) session.getAttribute("currency");

            if (c == null) {
                c = new Currency();
                c.setLocale(request.getLocale());
                session.setAttribute("currency", c);
            }

            while (i.hasNext()) {
                ShoppingCartItem item = (ShoppingCartItem) i.next();
                bd = (Book) item.getItem();
                c.setAmount(bd.getPrice());

                out.println(
                        "<tr>" + "<td align=\"right\" bgcolor=\"#ffffff\">"
                        + item.getQuantity() + "</td>"
                        + "<td bgcolor=\"#ffffaa\">" + "<strong><a href=\""
                        + response.encodeURL(
                                request.getContextPath()
                                + "/bookdetails?bookId=" + bd.getBookId())
                        + "\">" + bd.getTitle() + "</a></strong>" + "</td>"
                        + "<td bgcolor=\"#ffffaa\" align=\"right\">"
                        + c.getFormat() + "</td>" + "<td bgcolor=\"#ffffaa\">"
                        + "<strong>" + "<a href=\""
                        + response.encodeURL(
                                request.getContextPath()
                                + "/bookshowcart?Remove=" + bd.getBookId())
                        + "\">" + messages.getString("RemoveItem")
                        + "</a></strong>" + "</td></tr>");
            }

            c.setAmount(cart.getTotal());
View Full Code Here

        // Additions to the shopping cart
        String bookId = request.getParameter("bookId");

        if (bookId != null) {
            try {
                Book book = bookDB.getBook(bookId);
                cart.add(bookId, book);
                out.println(
                        "<p><h3>" + "<font color=\"#ff0000\">"
                        + messages.getString("CartAdded1") + "<i>"
                        + book.getTitle() + "</i> "
                        + messages.getString("CartAdded2") + "</font></h3>");
            } catch (BookNotFoundException ex) {
                response.reset();
                throw new ServletException(ex);
            }
        }

        //Give the option of checking cart or checking out if cart not empty
        if (cart.getNumberOfItems() > 0) {
            out.println(
                    "<p><strong><a href=\""
                    + response.encodeURL(
                            request.getContextPath() + "/bookshowcart") + "\">"
                    + messages.getString("CartCheck")
                    + "</a>&nbsp;&nbsp;&nbsp;" + "<a href=\""
                    + response.encodeURL(
                            request.getContextPath() + "/bookcashier") + "\">"
                    + messages.getString("Buy") + "</a>" + "</p></strong>");
        }

        // Always prompt the user to buy more -- get and show the catalog
        out.println(
                "<br> &nbsp;" + "<h3>" + messages.getString("Choose") + "</h3>"
                + "<center> <table summary=\"layout\">");

        try {
            Collection coll = bookDB.getBooks();

            Iterator i = coll.iterator();
            Currency c = (Currency) session.getAttribute("currency");

            if (c == null) {
                c = new Currency();
                c.setLocale(request.getLocale());
                session.setAttribute("currency", c);
            }

            while (i.hasNext()) {
                Book book = (Book) i.next();
                bookId = book.getBookId();
                c.setAmount(book.getPrice());

                //Print out info on each book in its own two rows
                out.println(
                        "<tr>" + "<td bgcolor=\"#ffffaa\">" + "<a href=\""
                        + response.encodeURL(
                                request.getContextPath()
                                + "/bookdetails?bookId=" + bookId)
                        + "\"> <strong>" + book.getTitle()
                        + "&nbsp; </strong></a></td>"
                        + "<td bgcolor=\"#ffffaa\" rowspan=2>" + c.getFormat()
                        + "&nbsp; </td>" + "<td bgcolor=\"#ffffaa\" rowspan=2>"
                        + "<a href=\""
                        + response.encodeURL(
                                request.getContextPath()
                                + "/bookcatalog?bookId=" + bookId)
                        + "\"> &nbsp;" + messages.getString("CartAdd")
                        + "&nbsp;</a></td></tr>" + "<tr>"
                        + "<td bgcolor=\"#ffffff\">" + "&nbsp; &nbsp;"
                        + messages.getString("By") + "<em> "
                        + book.getFirstName() + " " + book.getSurname()
                        + "</em></td></tr>");
            }
        } catch (BooksNotFoundException ex) {
            response.reset();
            throw new ServletException(ex);
View Full Code Here

        double amount = 0.0;

        for (Iterator i = getItems()
                              .iterator(); i.hasNext();) {
            ShoppingCartItem item = (ShoppingCartItem) i.next();
            Book bookDetails = (Book) item.getItem();

            amount += (item.getQuantity() * bookDetails.getPrice());
        }

        return roundOff(amount);
    }
View Full Code Here

                    "Could not get books: " + ex.getMessage());
        }
    }

    public Book getBook(String bookId) throws BookNotFoundException {
        Book requestedBook = em.find(Book.class, bookId);

        if (requestedBook == null) {
            throw new BookNotFoundException("Couldn't find book: " + bookId);
        }
View Full Code Here

        Iterator i = items.iterator();

        try {
            while (i.hasNext()) {
                ShoppingCartItem sci = (ShoppingCartItem) i.next();
                Book bd = (Book) sci.getItem();
                String id = bd.getBookId();
                int quantity = sci.getQuantity();
                buyBook(id, quantity);
            }
        } catch (Exception ex) {
            throw new OrderException("Commit failed: " + ex.getMessage());
View Full Code Here

    public void buyBook(
        String bookId,
        int quantity) throws OrderException {
        try {
            Book requestedBook = em.find(Book.class, bookId);

            if (requestedBook != null) {
                int inventory = requestedBook.getInventory();

                if ((inventory - quantity) >= 0) {
                    int newInventory = inventory - quantity;
                    requestedBook.setInventory(newInventory);
                } else {
                    throw new OrderException(
                            "Not enough of " + bookId
                            + " in stock to complete order.");
                }
View Full Code Here

                    "Could not get books: " + ex.getMessage());
        }
    }

    public Book getBook(String bookId) throws BookNotFoundException {
        Book requestedBook = em.find(Book.class, bookId);

        if (requestedBook == null) {
            throw new BookNotFoundException("Couldn't find book: " + bookId);
        }
View Full Code Here

TOP

Related Classes of com.sun.bookstore.database.Book

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.