Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Posted the previous question a few minutes ago) This program extends the earlie

ID: 3858583 • Letter: #

Question

(Posted the previous question a few minutes ago) This program extends the earlier "Online shopping cart" program.

(1) Extend the ItemToPurchase class per the following specifications:

       Private fields

             string itemDescription - Initialized in default constructor to "none"

      Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)

      Public member methods

            setDescription() mutator & getDescription() accessor (2 pts)

            printItemCost() - Outputs the item name followed by the quantity, price, and subtotal

            printItemDescription() - Outputs the item name and description

Ex. of printItemCost() output:


Ex. of printItemDescription() output:


(2) Create two new files:

    ShoppingCart.java - Class definition

    ShoppingCartManager.java - Contains main() method

Build the ShoppingCart class with the following specifications. Note: Some can be method stubs (empty methods) initially, to be completed in later steps.

    Private fields

              String customerName - Initialized in default constructor to "none"

             String currentDate - Initialized in default constructor to "January 1, 2016"

             ArrayList cartItems

     Default constructor

     Parameterized constructor which takes the customer name and date as parameters (1 pt)

     Public member methods

            getCustomerName() accessor (1 pt)

            getDate() accessor (1 pt)

            addItem()

                   Adds an item to cartItems array. Has parameter ItemToPurchase. Does not return anything.

            removeItem()

                    Removes item from cartItems array. Has a string (an item's name) parameter. Does not return anything.

                    If item name cannot be found, output this message: Item not found in cart. Nothing removed.

            modifyItem()

                   Modifies an item's description, price, and/or quantity. Has parameter ItemToPurchase. Does not return anything.

                   If item can be found (by name) in cart, check if parameter has default values for description, price, and quantity. If not, modify item in cart.

                   If item cannot be found (by name) in cart, output this message: Item not found in cart. Nothing modified.

          getNumItemsInCart() (2 pts)

                  Returns quantity of all items in cart. Has no parameters.

         getCostOfCart() (2 pts)

                  Determines and returns the total cost of items in cart. Has no parameters.

         printTotal()

                  Outputs total of objects in cart.

                   If cart is empty, output this message: SHOPPING CART IS EMPTY

        printDescriptions()

                 Outputs each item's description.


Ex. of printTotal() output:


Ex. of printDescriptions() output:


(3) In main(), prompt the user for a customer's name and today's date. Output the name and date. Create an object of type ShoppingCart. (1 pt)

Ex.


(4) Implement the printMenu() method. printMenu() has a ShoppingCart parameter, and outputs a menu of options to manipulate the shopping cart. Each option is represented by a single character. Build and output the menu within the method.

If the an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call printMenu() in the main() method. Continue to execute the menu until the user enters q to Quit. (3 pts)

Ex:


(5) Implement Output shopping cart menu option. (3 pts)

Ex:


(6) Implement Output item's description menu option. (2 pts)

Ex.


(7) Implement Add item to cart menu option. (3 pts)

Ex:


(8) Implement Remove item menu option. (4 pts)

Ex:


(9) Implement Change item quantity menu option. Hint: Make new ItemToPurchase object and use ItemToPurchase modifiers before using modifyItem() method. (5 pts)

Ex:

Explanation / Answer

The shopping Cart program in JSP is as follows. I tried to make it as simple as I can. Hope you'll like it.

CrtItemBeans.java

public class CrtItmBeans {
private String partNo;
private String strModelDesc;
private double prices;
private int quant;
private double ttlCst;

public String getPrtNo() {
return partNo;
}
public void setPrtNo(String partNo) {
this.partNo = partNo;
}
public String getMdl() {
return strModelDesc;
}
public void setMdl(String strModelDesc) {
this.strModelDesc = strModelDesc;
}
public double getUC() {
return prices;
}
public void setUC(double prices) {
this.prices = prices;
}
public int getQuant() {
return quant;
}
public void setQuant(int quantity) {
quant = quantity;
}
public double getTtl() {
return ttlCst;
}
public void setTtl(double ttlCst) {
this.ttlCst = ttlCst;
}
}

BeansForCart.java

import java.util.ArrayList;


public class BeansForCart {
private ArrayList crtItems = new ArrayList();
private double dbOrdTtl ;
  
public int getitemCnt() {
return crtItems.size();
}
  
public void delCrtItem(String indices) {
int iniceItem = 0;
try {
iniceItem = Integer.parseInt(indices);
crtItems.remove(iniceItem - 1);
OrderTtl();
} catch(NumberFormatException nf) {
System.out.println("Error while deleting cart item: "+nf.getMessage());
nf.printStackTrace();
}
}
  
public void alterCart(String indices, String quants) {
double ttlCst = 0.0;
double prices = 0.0;
int quant = 0;
int iniceItem = 0;
CrtItmBeans crtItm = null;
try {
iniceItem = Integer.parseInt(indices);
quant = Integer.parseInt(quants);
if(quant>0) {
crtItm = (CrtItmBeans)crtItems.get(iniceItem-1);
prices = crtItm.getUC();
ttlCst = prices*quant;
crtItm.setQuant(quant);
crtItm.setTtl(ttlCst);
OrderTtl();
}
} catch (NumberFormatException nf) {
System.out.println("Error while updating cart: "+nf.getMessage());
nf.printStackTrace();
}

}
  
public void addingItem(String mdll, String desc,
String units, String quants) {
double ttlCst = 0.0;
double prices = 0.0;
int quant = 0;
CrtItmBeans crtItm = new CrtItmBeans();
try {
prices = Double.parseDouble(units);
quant = Integer.parseInt(quants);
if(quant>0) {
ttlCst = prices*quant;
crtItm.setPrtNo(mdll);
crtItm.setMdl(desc);
crtItm.setUC(prices);
crtItm.setQuant(quant);
crtItm.setTtl(ttlCst);
crtItems.add(crtItm);
OrderTtl();
}
  
} catch (NumberFormatException nf) {
System.out.println("Error while parsing from String to primitive types: "+nf.getMessage());
nf.printStackTrace();
}
}
  
public void addingItem(CrtItmBeans crtItm) {
crtItems.add(crtItm);
}
  
public CrtItmBeans getCrt(int iniceItem) {
CrtItmBeans crtItm = null;
if(crtItems.size()>iniceItem) {
crtItm = (CrtItmBeans) crtItems.get(iniceItem);
}
return crtItm;
}
  
public ArrayList getCrts() {
return crtItems;
}
public void setCartItems(ArrayList crtItems) {
this.crtItems = crtItems;
}
public double getOrderCount() {
return dbOrdTtl;
}
public void setOrderTotal(double dbOrdTtl) {
this.dbOrdTtl = dbOrdTtl;
}
  
protected void OrderTtl() {
double dblTotal = 0;
for(int cnt=0;cnt<crtItems.size();cnt++) {
CrtItmBeans crtItm = (CrtItmBeans) crtItems.get(cnt);
dblTotal+=crtItm.getTtl();
  
}
setOrderTotal(dblTotal);
}

}

crtHandler.java

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class crtHandler extends HttpServlet {
  
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

String act = req.getParameter("action");


if(act!=null && !act.equals("")) {
if(act.equals("add")) {
addCrt(req);
} else if (act.equals("Update")) {
alteringCrt(req);
} else if (act.equals("Delete")) {
deleteCart(req);
}
}
res.sendRedirect("../Shopping.jsp");
}
  
protected void deleteCart(HttpServletRequest req) {
HttpSession sess = req.getSession();
String indices = req.getParameter("itemIndice");
BeansForCart bfc = null;

Object objCrt = sess.getAttribute("cart");
if(objCrt!=null) {
bfc = (BeansForCart) objCrt ;
} else {
bfc = new BeansForCart();
}
bfc.delCrtItem(indices);
}
  
protected void alteringCrt(HttpServletRequest req) {
HttpSession sess = req.getSession();
String quants = req.getParameter("quantity");
String indices = req.getParameter("itemIndice");
  
BeansForCart bfc = null;

Object objCrt = sess.getAttribute("cart");
if(objCrt!=null) {
bfc = (BeansForCart) objCrt ;
} else {
bfc = new BeansForCart();
}
bfc.alterCart(indices, quants);
}
  
protected void addCrt(HttpServletRequest req) {
HttpSession sess = req.getSession();
String mdll = req.getParameter("modelNo");
String desc = req.getParameter("description");
String strPrice = req.getParameter("price");
String quants = req.getParameter("quantity");

BeansForCart bfc = null;

Object objCrt = sess.getAttribute("cart");

if(objCrt!=null) {
bfc = (BeansForCart) objCrt ;
} else {
bfc = new BeansForCart();
sess.setAttribute("cart", bfc);
}

bfc.addingItem(mdll, desc, strPrice, quants);
}

}

Modelling.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Model List</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p><font size="3" face="Verdana, Arial, Helvetica, sans-serif"><strong>Models
</strong></font></p>
<a href="/ShoppingCart.jsp" mce_href="ShoppingCart.jsp">View Cart</a>
<p/>
<table width="75%" border="1">
<tr>
<td><form name="modelDetail1" method="POST" action="servlet/crtHandler">
<font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Model:</strong>
TF-Model1</font><input type="hidden" name="modelNo" value="TF-MODEL1">
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Description:</strong>
imaginary model 1. </font><input type="hidden" name="description" value=" imaginary model 1."></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Quantity:<input type="text" size="2" value="1" name="quantity"></strong></font></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Price:</strong>
$10.00</font><input type="hidden" name="price" value="10"></p><input type="hidden" name="action" value="add"><input type="submit" name="addCrt" value="Add To Cart">
</form></td>
<td><form name="modelDetail2" method="POST" action="servlet/crtHandler"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Model</strong>:
TF-Model2 </font><input type="hidden" name="modelNo" value="TF-MODEL2">
<font face="Verdana, Arial, Helvetica, sans-serif">
<p><font size="2"><strong>Description</strong>: imaginary model
2. </font><input type="hidden" name="description" value=" imaginary model 2."></p>
<p><font size="2"><strong>Quantity</strong>: <input type="text" size="2" value="1" name="quantity"></font></p>
<p><font size="2"><strong>Price</strong>: $20.00<input type="hidden" name="price" value="20"></font></p>
<input type="hidden" name="action" value="add">
<input type="submit" name="addCrt" value="Add To Cart">
</font></form></td>
</tr>
<tr>
<td><form name="modelDetail3" method="POST" action="servlet/crtHandler"><p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Model:</strong>
TF-Model3</font><input type="hidden" name="modelNo" value="TF-MODEL3"></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Description:</strong>
imaginary model 3. </font><input type="hidden" name="description" value=" imaginary model 3."></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Quantity:</strong></font> <input type="text" size="2" value="1" name="quantity"></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Price: $30.00</strong></font><input type="hidden" name="price" value="30"></p> <input type="hidden" name="action" value="add">
<input type="submit" name="addCrt" value="Add To Cart">
</form></td>
<td><form name="modelDetail4" method="POST" action="servlet/crtHandler"><p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Model</strong>:
TF-Model4</font><input type="hidden" name="modelNo" value="TF-MODEL4"></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Description</strong>:
imaginary model 4. </font><input type="hidden" name="description" value=" imaginary model 4."></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Quantity</strong>:</font> <input type="text" size="2" value="1" name="quantity"></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Price</strong>: $40.00</font><input type="hidden" name="price" value="40"></p>
<input type="hidden" name="action" value="add"><input type="submit" name="addCrt" value="Add To Cart"></form></td>
</tr>
</table>
<p> </p>
</body>
</html>

Shopping.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Shopping Cart</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<p><font face="Verdana, Arial, Helvetica, sans-serif"><strong>Shopping Cart</strong></font></p>
<p><a href="/ModelList.jsp" mce_href="ModelList.jsp">Model List</a> </p>
<table width="75%" border="1">
<tr bgcolor="#CCCCCC">
<td><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Model
Description</font></strong></td>
<td><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Quantity</font></strong></td>
<td><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Unit
Price</font></strong></td>
<td><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Total</font></strong></td>
</tr>
<jsp:useBean id="cart" scope="sess" class="in.shoppingcart.beans.BeansForCart" />
<c:if test="${cart.lineItemCount==0}">
<tr>
<td colspan="4"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">- Cart is currently empty -<br/>
</tr>
</c:if>
<c:forEach var="crtItm" items="${cart.cartItems}" varStatus="cnt">
<form name="item" method="POST" action="servlet/crtHandler">
<tr>
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b><c:out value="${crtItm.partNumber}"/></b><br/>
<c:out value="${crtItm.modelDescription}"/></font></td>
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><input type='hidden' name='itemIndice' value='<c:out value="${cnt.count}"/>'><input type='text' name="quantity" value='<c:out value="${crtItm.quantity}"/>' size='2'> <input type="submit" name="action" value="Update">
<br/> <input type="submit" name="action" value="Delete"></font></td>
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">$<c:out value="${crtItm.unitCost}"/></font></td>
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">$<c:out value="${crtItm.totalCost}"/></font></td>
</tr>
</form>
</c:forEach>
<tr>
<td colspan="2"> </td>
<td> </td>
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Subtotal: $<c:out value="${cart.orderTotal}"/></font></td>
</tr>
</table>
</body>
</html>

Hence, this is the complete code for the shopping cart in JSP and Servlet.

Please rate the answer if it helped.....Thankyou

Hope it helps....