Shopping Cart Application using Java

145 Views Asked by At

I'm working on a Java challenge. I want to create an application that displays 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 shopping cart. 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.

This is what I have so far:

SelectionsBean.java

package sessiontracking;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean( name="selectionsBean" )
@SessionScoped
public class SelectionsBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private final Map<String, Double> pricesMap;

    private List<String> selections;

    public SelectionsBean() {
        pricesMap = new HashMap<>();
        pricesMap.put("Java How to Program", 139.95);
        pricesMap.put("C++ How to Program", 119.95);
        pricesMap.put("iPhone for Programmers: An App-Driven Approach", 49.95);
        pricesMap.put("Android for Programmers: An App-Driven Approach", 49.95);

        // initialize selections list
        selections = new ArrayList<>();
    }

    public void addToCart(String selection) {
        selections.add(selection);
    }

    public void removeFromCart(String selection) {
        selections.remove(selection);
    }

    public double getTotalPrice() {
        double totalPrice = 0.0;
        for (String selection : selections) {
            totalPrice += pricesMap.get(selection);
        }
        return totalPrice;
    }

    public List<String> getSelections() {
        return selections;
    }

    public void setSelections(List<String> selections) {
        this.selections = selections;
  
}


index.html

<?xml version='1.0' encoding='UTF-8' ?>
<!-- index.xhtml -->
<!-- Allow the user to select a book -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Book Selection Page</title>
</h:head>
<h:body>
<h1>Welcome to the Bookstore!</h1>
<p>You have selected #{selectionsBean.Size} book(s).
</p>
<h3>Select a Book and Press Submit</h3>
<h:form>
<h:selectOneRadio id="bookSelectOneRadio" required="true"
requiredMessage="Please choose a book, then press Submit"
value="#{selectionsBean.selectedBook}">
<f:selectItem itemValue="java" itemLabel="Java How to Program"/>
<f:selectItem itemValue="cpp" itemLabel="C++ How to Program"/>
<f:selectItem itemValue="iphone"
itemLabel="iPhone for Programmers: An App-Driven Approach"/>
<f:selectItem itemValue="android"
itemLabel="Android for Programmers: An App-Driven Approach"/>
</h:selectOneRadio>
<p><h:commandButton value="Submit"/></p>
</h:form>
<p><h:outputLink value="cart.xhtml">
Click here to view your shopping cart
</h:outputLink></p>
</h:body>
</html>

cart.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- cart.xhtml -->
<!-- Display the user's shopping cart -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Shopping Cart</title>
</h:head>
<h:body>
<h1>Shopping Cart</h1>
<h:dataTable value="#{cartBean.cart}" var="item">
<h:column>
<f:facet name="header">Book Title</f:facet>
#{item.title}
</h:column>
<h:column>
<f:facet name="header">Price</f:facet>
#{item.price}
</h:column>
</h:dataTable>
<p>Total price: #{cartBean.totalPrice}</p>
<p><h:outputLink value="index.xhtml">
Click here to continue shopping
</h:outputLink></p>
</h:body>
</html>

Whenever I got to compile I this error:

/index.xhtml: The class 'sessiontracking.SelectionsBean' does not have the property 'Size'.

I'm kind of stuck and don't see in the code how to resolve.

1

There are 1 best solutions below

3
TP95 On BEST ANSWER

In your index.html the offending line is:

<p>You have selected #{selectionsBean.Size} book(s).

selectionsBean.Size will throw an error because there is no property called "Size" defined in your SelectionsBean class.

Looking at the sentence, I assume you are trying to show the number of selections made by the customer so instead use:

<p>You have selected #{selectionsBean.selections.size()} book(s).

Why did that work? Well, in your SelectionsBean class you have defined this property called "selections":

private List<String> selections;

"selections" is a List type that has inbuilt methods like Size() that returns the number of elements in that list, so you can use that. Be sure to check for nulls because your selections variable is only initialized in your constructor SelectionsBean().