

import java.util.zip.*; 



import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.*;
import java.util.zip.*;

public class ShoppingCartItem extends HttpServlet
	{

    public void doGet (HttpServletRequest req, HttpServletResponse res)
    {
	// instance variable are not thread safe
	String cartoption = null;

	try
	{
	//declare some work fields
		
	//declare and retrieve the parameter values	
		
 		
		cartoption	= req.getParameter( "cartoption" ).trim();
		

	 
		res.setContentType("text/html");
		PrintWriter out = new PrintWriter( res.getOutputStream( )  );
		out.println("<html> <head> <title>Shopping Cart</title><BASE TARGET=bottompanel>");

		out.println("<meta http-equiv='window-target' content='bottompanel'>");


		out.println("<link rel='stylesheet' type='text/css' href='../book.css' /></head>");
		out.println("<body align='center'>");
		out.println("<h1>");
		out.println("<image src='../images/BookNookStack3.gif' width='213' height='96' border=0 align='left'>");
		out.println("Shopping Cart Contents...<br />");
		out.println("</h1>");
		out.println("<image src='../images/BookNookStar.gif' width='85' height='34' border=0 alt='' >");
		out.println("<p>");
		out.println("<center>Your Shopping Cart currently Contains the following items:</center>");
		
		out.println("</p>");

		//retrieve the session object
		HttpSession session = req.getSession(true);	
	
	
		System.out.println(cartoption);
		
		
			if (cartoption.equals("view"))
			{
			doView(session, out);
			  
			}
			 
			else

			if (cartoption.equals("add"))
			{

			doAdd(session, out, req);
		
			}
 
			else
			if (cartoption.equals("delete"))	
			{
			
			doDelete(session, out, req);
		
			}//end if

			else
			{
			out.println("<center>Invalid operation found = " + cartoption + "</center>");
			}

		

		
		
			

			out.println( "</table > ");

			 

			out.println( "<br /> <br /> <br /><center > ");



				
			 
			out.println("<input type='button' name='searchbutton' value='Product Search'style='font : 14pt Arial Black; background : lightblue' onclick= 'location.href=\"../productlook.html\"' />");
	
			 
	
			out.println("<input type='button' name='backbutton' value='Next Item' style='font : 14pt Arial Black; background : lightblue' onclick='history.back();' />");
	
	 
			out.println("<input type='button' name='orderbutton' value='Checkout' style='font : 14pt Arial Black; background : lightblue' onclick= 'location.href=\"OrderEntryServlet\"' >");
			
			out.println( "</center > ");
		 

		
		
			out.println( "</body></html>");
			out.flush( );
			out.close( );


	} // end of try

	catch (Exception e)
	{
		System.out.println("Exception has occurred see stack trace below");
		e.printStackTrace();
	} // end of catch

    } // end of doGet


    public void doPost (HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException	
	{
        doGet (request, response);
    } // end of doPost



	public void doView(HttpSession session, PrintWriter out)
		{

		Map myOrders = null;

			if (session.isNew() )
			{
			out.println("<center>Sorry there is no item in your cart</center>");
			} // end of new session
		else
			{
			myOrders = (Map) session.getAttribute("cart");
			if (myOrders == null)
				{
					out.println("<center>Sorry there is no item in your cart</center>");
				}
			else
				{

					doDisplayCart(session, out);
				}

			} // end of not new session
		} // end of doView

	public void doAdd(HttpSession session, PrintWriter out, HttpServletRequest req)
		{
		 
		Map myOrders = null;
		String title = null;
		String author = null;
		String isbn = null;
		double cost = 0;
		int qty= 0;
		
			if (session.isNew() )
			{
		 	
			 myOrders = new HashMap();
	
			
			} // end of newSession
			else
			{

			myOrders = (Map) session.getAttribute("cart");
			if (myOrders == null )

			{

			myOrders = new HashMap();

			} // end of myOrders is null



			}
			Book b = new Book();


			title		= req.getParameter( "title" );
			author		= req.getParameter( "author" );
			isbn		= req.getParameter( "isbn" );



			// need to validate the numeric qty come in 


			try
			{
			qty			= Integer.parseInt(req.getParameter( "qty" ));
			}
			catch (NumberFormatException e)
			{
			qty = 1;
			out.println("<center>Your quantity has been changed to 1. <br /> If you need more than 1, please make any correction you need  ....</center>"); 
			} // end of catch






			cost		= Double.parseDouble(req.getParameter( "cost"));

			//create an instance of a book and assign it values
			//retrieved from the form
			 
			b.setTitle(title);
			b.setAuthor(author);
			b.setISBN(isbn);
			b.setCost(cost);
			b.setQty(qty);

			myOrders.put(isbn, b);

			session.setAttribute("cart", myOrders);

			doDisplayCart(session, out);

		} // end of doAdd

	public void doDelete(HttpSession session, PrintWriter out, HttpServletRequest req)
		{

		String isbn = null;
		Map myOrders = (Map) session.getAttribute("cart");
		isbn		= req.getParameter( "isbn" );
	 
	
		// remove from the List
		myOrders.remove(isbn);

		if (myOrders == null)
				{
					out.println("<center>Your Cart is empty....</center>");
				}
			else
				{

					doDisplayCart(session, out);
				}


		} // end of doDelete


	public void doDisplayCart (HttpSession session, PrintWriter out)
		{
			Map myOrders = (Map) session.getAttribute("cart");	
			Collection collect = myOrders.values();			 
			Iterator it = collect.iterator();
			int count = 0;
			
		
			while (it.hasNext())
			{
				
			   if (count == 0)
			   {
				out.println( "<table width='80%' bgcolor='#99CCFF' border='1' cellpadding='6' cellspacing='6' align='center'  class='msgclass'>");
				out.println( "<tr> ");
				out.println( "<td>Title</td>");
				out.println( "<td>Author</td>");
				out.println( "<td>ISBN</td>");
				out.println( "<td>Price</td>");
				out.println( "<td>Quantity</td>");
				out.println( "<td>Shopping Cart</td>");
				out.println( "</tr> ");



			   } // end of printheading












				Book temp = (Book)it.next();
				out.println("<tr><td>" + temp.getTitle() + "</td> <td>" + temp.getAuthor() + "</td><td>" + temp.getISBN() +  "</td><td>" + temp.getCost()  + "</td><td>" + temp.getQty() + "</td>");
				out.println("<td><input type='button' name='cartoption' value='Remove From Cart' onclick='location.href=\"ShoppingCartItem?cartoption=delete&isbn="+ temp.getISBN()+ "\"' /></td></tr>");
				
				count ++;
			} //end of while loop
			if (count == 0 )
				{
					out.println("<center>Your cart is empty.... </center>");
				}

		} // end of doDisplayCart



} // end of ShoppingCartItem

