wood burning stoves 2.0*
The moose likes JSP and the fly likes Referencing classes and variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Referencing classes and variables" Watch "Referencing classes and variables" New topic
Author

Referencing classes and variables

shuzo monsoon
Ranch Hand

Joined: Feb 11, 2004
Posts: 54
Hello all, I have written a class called BasketItems, which basically stores the title, artist, price and quantity of an item. Then another class - ShoppingBasket is used to store an instance of the basketItems class in a vector. However, the vector itself is not instantiated until a user adds an item (through a JSP page) to the basket, in which case a process request method should add the product to the vector. I am having some trouble in specifying that if the product is already present, then the quantity should be incremented. Here are the 2 classes -

BASKET ITEMS CLASS
public class BasketItem {
String product = null;
String artist = null;
float price = 0;
int quantity = 0;

public BasketItem(String product_, String artist_, float price_) {
product = product_;
artist = artist_;
price = price_;
quantity = 1;
}
public String getTitle() {
return product;
}
public String getArtist() {
return artist;
}
public float getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity_) {
quantity = quantity_;
}
public void addToItemQuantity(int q) {
quantity += q;
}
}
SHOPPING BASKET CLASS
package catalogue;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
public class ShoppingBasket extends Object {
private Vector basket = null;
String product = null;
String submit = null;

public ShoppingBasket() {
basket = new Vector();
}
public void setProduct(String product) {
this.product = product;
}
public void setSubmit(String submit) {
this.submit = submit;
}
public Vector getProducts() {
return basket;
}
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")) {

BasketItem item = null;
boolean added = false;
for(int i=0;i<products.size();i++) {
item = (BasketItem)products.get(i);
if(item.getTitle().equals(product)) {
((BasketItem)products.get(i)).addToItemQuantity(1);

added = true;
break;
}
if(!added) {
addProduct(product);
}
}
}
}
else if(submit.equals("remove")) {
removeProduct(product);
}
else {
reset();
}
}
public void reset() {
submit = null;
product = null;
}
}
Teh problem I have is that I have used "products.get(i)" for example which is actually a reference to the vector created on the JSP page.
I hope this isn't too confusing and that someone can try and help!
Thanks.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
What's going wrong here?
My guess is you're getting a NullPointerException because the Vector is not instantiated?
If so, instantiate the Vector instead of iterating over it (as you know it'll be empty...


42
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Referencing classes and variables
 
Similar Threads
Instantiation Exception
Displaying prices
Syntax error
JSP class file
getting-> 500 Internal Server Error