diff --git a/Caerservlet.java b/Caerservlet.java new file mode 100644 index 0000000..bb64771 --- /dev/null +++ b/Caerservlet.java @@ -0,0 +1,34 @@ + + +import java.io.IOException; +import java.io.PrintWriter; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + + +@WebServlet("/cart") +public class Caerservlet extends HttpServlet { + + /** + * + * @param request + * @param response + * @throws ServletException + * @throws IOException + */ + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException + { + Cart cart = (Cart) request.getSession().getAttribute("cart"); + if (cart == null) + { + cart = new Cart(); + request.getSession().setAttribute("cart", cart); + } + request.getRequestDispatcher("cart.jsp").forward(request, response); + } +} diff --git a/Cart.java b/Cart.java new file mode 100644 index 0000000..9de80e0 --- /dev/null +++ b/Cart.java @@ -0,0 +1,41 @@ + +import java.util.*; + + + public class Cart + { + private List items; + + public Cart() + { + items = new ArrayList<>(); + } + + public void addItem(Product product) + { + items.add(product); + } + public void removeItem(Product product) + { + items.remove(product); + } + + + + public List getItems() { + return items; + } + + public double getTotalPrice() + { + double total = 0.0; + for (Product product : items) + { + total += product.getPrice(); + } + return total; + } +} + + + diff --git a/Product.java b/Product.java new file mode 100644 index 0000000..5eeb709 --- /dev/null +++ b/Product.java @@ -0,0 +1,13 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +/** + * + * @author LENOVO + */ +class Product { + +} diff --git a/Productservlet.java b/Productservlet.java new file mode 100644 index 0000000..2039662 --- /dev/null +++ b/Productservlet.java @@ -0,0 +1,22 @@ +import java.util.*; +import java.io.IOException; +import java.io.PrintWriter; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet("/products") +public class Productservlet extends HttpServlet { + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + List products = getProductListFromDatabase(); // Retrieve product list from database + request.setAttribute("products", products); + request.getRequestDispatcher("product-list.jsp").forward(request, response); + } + + private List getProductListFromDatabase() { + return null; + // Retrieve product list from the database and return it + } +} diff --git a/Shop.java b/Shop.java new file mode 100644 index 0000000..ee495d2 --- /dev/null +++ b/Shop.java @@ -0,0 +1,7 @@ + +public class Shop +{ + private String title; + private double price; + +} diff --git a/cart.jsp b/cart.jsp new file mode 100644 index 0000000..4e19d0e --- /dev/null +++ b/cart.jsp @@ -0,0 +1,27 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> + + + + Shopping Cart + + +

Shopping Cart

+ + + + + + + + + <% for (Product product : ((Cart) request.getSession().getAttribute("cart")).getItems()) { %> + + + + + <% } %> + +
NamePrice
<%= product.getName() %><%= product.getPrice() %>
+

Total Price: <%= ((Cart) request.getSession().getAttribute("cart")).getTotalPrice() %>

+ + diff --git a/productlist.jsp b/productlist.jsp new file mode 100644 index 0000000..f003f7d --- /dev/null +++ b/productlist.jsp @@ -0,0 +1,16 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> + + + + Product List + + +

Product List

+
    + <% for (Product product : (List) request.getAttribute("products")) { %> +
  • <%= product.getName() %> - <%= product.getPrice() %>
  • + <% } %> +
+ View Cart + +