• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Referencing classes and variables

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
We don't have time for this. We've gotta save the moon! Or check this out:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic