29.4(Shopping Cart Application) Using the techniques you learned in Section 29.8
ID: 3718684 • Letter: 2
Question
29.4(Shopping Cart Application) Using the techniques you learned in Section 29.8.2, create a simple shopping cart application. Display a list of books as an h:selectOneRadio element. When the user submits the form, store the user’s selection in a @SessionScoped managed bean. Allow the user to return to the list of books and make additional selections. Provide a link to view the shoppingcart. On the shopping cart page, display the list of selections the user made, the price of each book and the total of all books in the cart.
Explanation / Answer
Code:
Interface
//
package cartbook.ejb;
//imprting utilities
import cartbook.util.bokexceptn;
import java.util.List;
import javax.ejb.Remote;
@Remote//overriding remote
public interface CartBook {//makingcart look interface
public void lstartf(String cust) throws bokexceptn;
public void lstartf(//for throwing exceptions
String cust,
String custid) throws bokexceptn;
public void custaddBook(String boktitle);//insering book to cart function
//book ermoving functoin
public void custremoveBook(String boktitle) throws bokexceptn;
public List<String> takeContnt();
//calling removing function /
public void remove();
}
//java class for bookcart
CartBeans.java
package cartbook.ejb;
import cartbook.util.bokexceptn;
import cartbook.util.CustIdlook;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
@Stateful()
//class definition
public class CarBeans implements Cart {
List<String> contents;//list type
//local variables
String customerId;
String customerName;
//checking customer id
public void lstartf(String cust) throws bokexceptn {
if (cust == null) {
throw new bokexceptn("Null cust not allowed.");
} else {
customerName = cust;
}
customerId = "0";
contents = new ArrayList<String>();
}
public void lstartf(
String cust,
String custid) throws bokexceptn {
if (cust == null) {
throw new bokexceptn("Null cust not allowed.");
} else {
customerName = cust;
}
CustIdlook idChecker = new CustIdlook();
if (idChecker.checkin(custid)) {
customerId = custid;
} else {
throw new bokexceptn("Invalid custid: " + custid);
}
contents = new ArrayList<String>();
}
//functio to add book into cart
public void custaddBook(String boktitle) {
contents.add(boktitle);
}
//function to remove books
public void custremoveBook(String boktitle) throws bokexceptn {
boolean result = contents.remove(boktitle);
if (result == false) {
throw new bokexceptn(""" + boktitle + "" not in cart.");
}
}
public List<String> takeContnt() {
return contents;
}
@Remove()//overriding
public void remove() {
contents = null;
}
}
//Util files
package cartbook.util;
//utilities for exceptions
public class bokexceptn extends Exception {
public bokexceptn() {
}
public bokexceptn(String bokmsg) {
super(bokmsg);
}
}
//utility 2
package cartbook.util;
public class CustIdlook {
public CustIdlook() {
}
//checkin id utility
public boolean checkin(String custid) {
boolean lbool = true;
for (int li = 0; li < custid.length(); li++) {
if (Character.isDigit(custid.charAt(li)) == false) {
lbool = false;
}
}
return lbool;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.