# COMP 321 Homework 02 Name: Mark Falcone Can be viewed on HackMD [here ](https://hackmd.io/MaCQPAIHSJ-FDyO_8Qv-NQ?view) ## Problem 1 – Models [6 points] We need to add some model classes to represent a book and a cart entry in our bookstore. Write a Book class to hold the data shown on the search results page plus a unique id, and a CartEntry class that holds the data on the cart page. Since we don’t have a database yet, we’ll hard-code the available books and the entries that are in the cart. Add static variables called allBooks and allEntries to the Book and CartEntry classes, and use static initialization blocks to add entries to those lists. ### Book.java: ```java= import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; public class Book { public String ID = ""; public String Title = ""; public String Author = ""; public double price = 0.0; public static ArrayList<Book> allBooks; public Book(String ID, String Title, String Author, double price) { this.ID = ID; this.price = price; this.Title = Title; this.Author = Author; this.price =price; } static { // whatever code is needed for initialization goes here allBooks = new ArrayList<Book>(); Book theTimeTravelersWhife = new Book("1","The Time Traveler's Whife","Audrey Niffenegger", 8.99 ); Book theNameofTheWind = new Book("2", "The Name of the Wind", "Patrick Rothfuss", 9.99); Book leviathanWakes = new Book("3", "Leviathan Wakes", "james S.A. Corey", 9.99); Book qed = new Book ("4", "QED", "Richard Feynman", 9.48); Book TheEyeoftheWorld = new Book("5", "The Eye of the World", "Robert Jordan", 8.99 ); allBooks.add( theTimeTravelersWhife); allBooks.add( theNameofTheWind); allBooks.add( leviathanWakes); allBooks.add( qed); allBooks.add( TheEyeoftheWorld); } public static void main(String[] args) { // TODO Auto-generated method stub } public String getID() { return ID; } public void setID(String iD) { ID = iD; } public String getTitle() { return Title; } public void setTitle(String title) { Title = title; } public String getAuthor() { return Author; } public void setAuthor(String author) { Author = author; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public ArrayList getBooks() { return allBooks; } } ``` ### CartEntry.java: ``` java= import java.util.ArrayList; public class CartEntry { public Book book; public int quanity ; public static ArrayList<CartEntry> allEntries; public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } public int getQuanity() { return quanity; } public void setQuanity(int quanity) { this.quanity = quanity; } public CartEntry(Book book, int quanity) { this.book = book; this.quanity = quanity; } static { allEntries = new ArrayList<CartEntry>(); } public ArrayList getCart() { return allEntries; } public void addCart(CartEntry entry) { allEntries.add(entry); } } ``` ## Problem 2 – AddToCartServlet [7 points] We will need a servlet that adds a book to the shopping cart. Write an AddToCartServlet class that handles requests that look like /addtocart?id=2. It will add a new CartEntry to the allEntries list in the CartEntry class, or add one to the quantity if one already exists. Since we aren’t wiring our screens up to the servlet yet, you should test it by putting log statements into the servlet and checking to see if you get the output that you expect for various requests. You can make doGet() call doPost() for testing purposes, but in the final version of the application, this servlet would only accept POST requests. ### AddToCartServlet.java: ```Java import java.io.IOException; import java.sql.Date; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class AddToCartServlet */ @WebServlet("/AddToCartServlet") public class AddToCartServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()+"at"+new Date(0)); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String url = "/addtocart?id="; String bookID = request.getParameter("id"); Book temp = null; for(int i =0; i < Book.allBooks.size()-1; i++){ if(Book.allBooks.get(i).getID() == bookID){ temp = Book.allBooks.get(i); break ; } if(CartEntry.allEntries == null) { CartEntry item = new CartEntry(temp, 1); CartEntry.allEntries.add(item); } else { for (int x=0; i < CartEntry.allEntries.size()-1;i++) { if(temp.getID() == CartEntry.allEntries.get(x).getBook().getID() ) { int newQuanity = CartEntry.allEntries.get(x).getQuanity() + 1 ; CartEntry.allEntries.get(x).setQuanity(newQuanity); } else { CartEntry item = new CartEntry(temp, 1); CartEntry.allEntries.add(item); } } } doGet(request, response); } } } ``` ## Problem 3 – UpdateQtyServlet [7 points] We will need a servlet that updates the quantities of the books in our shopping cart. Write an UpdateQtyServlet class that handles requests that look like /updateqty?id=2&qty=2. It will find the matching entry in the allEntries list in the CartEntry class, and update its quantity. If the quantity is set to zero, the entry should be removed from the list. Since we aren’t wiring our screens up to the servlet yet, you should test it by putting log statements into the servlet and checking to see if you get the output that you expect for various requests. You can make doGet() call doPost() for testing purposes, but in the final version of the application, this servlet would only accept POST requests. ### UpdateQtyServlet.java: ``` Java import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class UpdateQtyServlet */ @WebServlet("/UpdateQtyServlet") public class UpdateQtyServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public UpdateQtyServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String bookID = request.getParameter("id"); String quanity = request.getParameter("qty"); Book temp = null; // look through books for (int i = 0; i < Book.allBooks.size() - 1; i++) { if (Book.allBooks.get(i).getID() == bookID) { temp = Book.allBooks.get(i); break; } // if cart is empty add it to cart if (CartEntry.allEntries == null) { CartEntry item = new CartEntry(temp, Integer.parseInt(quanity)); CartEntry.allEntries.add(item); } else { // find item in cart for (int x = 0; i < CartEntry.allEntries.size() - 1; i++) { if (temp.getID() == CartEntry.allEntries.get(x).getBook().getID()) { // check if quanity =0 so we can remove book from the cart if (Integer.parseInt(quanity) == 0) { CartEntry.allEntries.remove(temp); } else { CartEntry.allEntries.get(x).setQuanity(Integer.parseInt(quanity)); } } else { CartEntry item = new CartEntry(temp, Integer.parseInt(quanity)); CartEntry.allEntries.add(item); } } doGet(request, response); } } } } ``` ## Weekly learning and reflection. [5 points] In two to three paragraphs of prose (i.e. sentences, not bullet lists) using APA style citations if needed, summarize and interact with the content that was covered this week in class. In your summary, you should highlight the major topics, theories, practices, and knowledge that were covered. Your summary should also interact with the material through personal observations, reflections, and applications to the field of study. In particular, highlight what surprised, enlightened, or otherwise engaged you. Make sure to include at least one thing that you’re still confused about. In other words, you should think and write critically not just about what was presented but also what you have learned through the session. Feel free to ask questions in this as well since it will be returned to you with answers. I had a hard time with this assignment mostly because I was running into eclipse errors with trying to import servlet and all the packages I also think I am creating my java projects incorrectly. I will see if I can set up an office hour with you so I can sort it out so I can be prepared and up to speed. Another thins I understand the big picture of a servlet and its functions with post and get. However I am still unsertan what the syntax for that is and how to do it. I worked on this with a friend and I think we were able to work through it espically with the slides from chapter 9