Prof. I. Rudowsky CIS53 WZ10
HW#3
Here’s a situation you commonly find in the real-world of programming. Your company has been running some software for a while and decides to improve the implementation. You are given the source code and hopefully some explanation of how the code works. More likely, the programmer who wrote the code no longer works there or may not even remember what she did, so you are on your own.
Your company has been running the Shopping Cart example in Chapter 9 of the textbook which uses an ArrayList of type ItemOrder to store information about each book selected. Namely, the array stores the catalog item number and number of items purchased for each selected book. The book description and price is retrieved from the catalog. The only cookie information being saved is the session id. If the servlet terminates, all the information is lost
They now want to change the existing code (download it from the author’s website) to save a cookie or cookies for each book selected (the item number and number ordered) so that even if the servlet is terminated you can return to it later and continue where you left off.
You can use two cookies per order, for example bookID1 and bookNumberOrdered1, or one cookie, bookOrder1 that has book id and number ordered separated by a space in the cookie value (in which case you will have to parse the string to get both values). You may want to associate the cookie with the book by including the bookid as part of the cookie id when you instantiate it or append the index of the ArrayList to the cookie name to link the two together. Any time a change is made in the array (book deleted or book added) you will have to rewrite the cookies (see next paragraph). If you devise your own scheme, feel free to implement it.
Each time a change is made (a book is removed from the order by placing a 0 in the number column, a new book has been added to the order or the number of books for an item already on the list has changed) the cookie(s) must be rewritten. Remember to set the maxAge each time you write the cookie.
While the servlet is running, the ArrayList should be updated and the cookies should be in synch with the ArrayList at all times. When the servlet starts for the first time it should check for cookies and, if it finds cookies, it should use them to initialize the ArrayList so that a user can continue from where he left off.
Before you begin, make sure you understand the existing code very well. Think carefully about what changes you want to make and how you will implement them. Indicate in the code or in a separate document the changes you made to the code and the type of cookies you are using. Try to minimize changes to the current code by adding new methods to do the additional work at the appropriate places.
Your December bonus depends on how good a job you do. May the force be with you!