aspose file tools*
The moose likes JSP and the fly likes Displaying prices Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Displaying prices" Watch "Displaying prices" New topic
Author

Displaying prices

shuzo monsoon
Ranch Hand

Joined: Feb 11, 2004
Posts: 54
I have created a java class which basically handles a shopping cart, in terms of that it adds the title fo a product to a vector and a JSP page then displays the contents of this vector. The problem I am experiencing is that I need to display the price for each particular product, but when I try to do this using get and set methods in the class, the price is overwritten each time by the new price. This is also the case when I simple use a database call in the JSP page itself to get the associated price for that product. I have tried using a vector for the price as well as the product, but this doesn't work. Anyone have any ideas?
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Best show some code first so we can better understand what you're doing and what exactly is happening.


42
shuzo monsoon
Ranch Hand

Joined: Feb 11, 2004
Posts: 54
Here is the class for the cart -
import java.util.*;
import javax.servlet.http.HttpServletRequest;
public class ShoppingBasket extends Object {
private Vector basket = null;
String product = null;
String submit = null;
float price = 0;

public ShoppingBasket() {
basket = new Vector();
}
public void setProduct(String product) {
this.product = product;
}
public void setPrice(float price) {
this.price = price;
}
public void setSubmit(String submit) {
this.submit = submit;
}
public Vector getProducts() {
return basket;
}
public float getPrice() {
return price;
}
public void addProduct(String product) {
basket.add(product);
}
public void removeProduct(String product) {
basket.remove(product);
}
public void processRequest(HttpServletRequest req) {
if(submit!=null) {
if(submit.equals("Add")) {
addProduct(product);
}
else if(submit.equals("remove")) {
removeProduct(product);
}
reset();
}
}
public void reset() {
submit = null;
product = null;
}
}
**
The JSP page using it is like this -
//Uses parameters passed from previous page
String product = request.getParameter("product");
String submit = request.getParameter("submit");
if ( (product!=null) && (submit!=null) ) {
%>
<jsp:useBean id="basket" scope="session" class="catalogue.ShoppingBasket"/>
<jsp:setProperty name="basket" property="*"/>
<% basket.processRequest(request);
%>
Vector products = basket.getProducts();
for(int i=0;i<products.size();i++) {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbURL,"","");

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM CD WHERE title LIKE '"+ product + "'");
while (rs.next()) {

float price = rs.getFloat("price");
%>
<tr>
<td align="center" width="130"><font color="black" face="tahoma" size="2"><%=products.get(i)%> </td>
<td align="center" width="80"><font color="black" face="tahoma" size="2">� <%=basket.getPrice()%></td>
<td align="center" width="80"><input type=text name="quantity" size="2"></td>
<td align="center" width="60"><font color="black" face="tahoma" size="2"><a href="Purchase.jsp?product=<%=products.get(i)%>&submit=remove"
style="text-decoration: none">
<font color="darkblue">Remove</font></a></font>
</td>
</tr>
<%
}
}
if (products.size() == 0) {
%>
</table>
<br>
<font color="black" face="Tahoma"><b> Your basket is currently empty</b></font>
<%
}
%>
</table>
<%
}
%>
Any help?
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Make a new class Product like this:

Modify your Cart to store those in your Vector (better use an ArrayList, it can give you a huge performance increase) and the JSP to read them.
If someone adds or removes items from a single product, update the numberOrdered only, make a function in Product to return the total value of ordered products (could later be linked to a Discount maybe?), and Cart a method to calculate the total value of the order by iterating all Products.
shuzo monsoon
Ranch Hand

Joined: Feb 11, 2004
Posts: 54
Ok, I sort or understand what you mean, but let me clarify. I should put the get/set methods currently in the sshopping basket class into the new product class? And when you say store 'those' in a an array, do you mean the items and the prices? or am I mistaken?
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
your new Product class will be fully selfcontained.
When the user adds some product to his shopping cart, the shopping cart will add a Product to the List (if there is no product with that name or product number in there) or incrrease the numberOrdered on an existing item if it does exist.
Each product knows its price PER ITEM and knows how many there are on order for it.
Based on that it can calculate (if you provide a function for it) the total price.
Say you have a books on order which each cost $15, the Product instance with name "book" would then return $30 when asked for totalPrice. Notice there's no totalPrice field, you just add a function getTotalPrice() that does nothing but calculate price*numberOrdered and returns that.
Personally I'd probably do away with a setNumberOrdered(int) method, instead using addNumberOrdered(int) which increases the numberOrdered with a specified amount (which can be negative, so make sure the total number cannot go under zero).
You store the Product objects in the ArrayList, which will store both price, numberOrdered and name all together.
In your JSP showing the content of the cart, you can then iterate over that using something like

Which will give you a nice list of all the items in the cart if you store the cart in your session under the name "cart".
shuzo monsoon
Ranch Hand

Joined: Feb 11, 2004
Posts: 54
Well I've thoroughly read what you wrote and I've tried to implement a class like you suggested, but i'm not too familiar with ArrayLists, so I don't know how to populate them with the name, price and quantity. Here is the code for what I have done so far for the product class ;
import java.util.*;
public class Product {

private ArrayList cart = new ArrayList();
private String product;
private String submit = null;
private int price;
private int numberOrdered;

public void setProduct(String product) {
this.product = product;
}
public void setPrice(int price) {
this.price = price;
}
public void addNumberOrdered(int numberOrdered) {
this.numberOrdered = numberOrdered + 1;
}
public void setSubmit(String submit) {
this.submit = submit;
}
public ArrayList getProducts() {
return cart;
}
public int getTotalPrice(int totalPrice) {
totalPrice = price * numberOrdered;

return totalPrice;
}
}
But also forgot to mention that the details of the items that are actually being added are stored in an Access database. Also the code sample you gave me for iterating through the arraylist (thanks by the way) makes sense but only if i can add data to the array list!
thanks for helping.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
You can add data to an ArrayList in a similar fashion as you would a Vector, by calling the add(Object) method on it.
If your data is stored in a database, so much the better.
Give Product a function to load itself from the database, and a static function to create an ArrayList of itself.
Something like

You can then use that function to load all Products at once to display the store catalogue.
When adding a product to a cart, create a new Product record and do not use the one from this List.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Hi shuzo,
The ArrayList, you can simply regard it as even a Vector. Just use add to add your result into the List is ok.
If you still feel uncomfortable, you can use Vector instead.
Nick.


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying prices