Results 21 to 40 of 52
Thread: Method call
- 07-15-2011, 10:04 PM #21
- 07-16-2011, 12:35 AM #22
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Any idea what I'm doing wrong?
- 07-16-2011, 12:36 AM #23
You need to understand how to use those methods.
As an exercise: write a loop to print out all the product ids with their quantities.
- 07-16-2011, 01:03 AM #24
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
This is what I have tryed so far
The output:
1: Quantity 0// print 0 even there are two item with id number 132
1: ID 132
2: Quantity 0
2: ID 132
Java Code:for(int i = 0; i < stock.size(); i++) { System.out.println(i+1 + ": Quantity " + findProduct(id).getQuantity()); System.out.println(i+1 + ": ID " + findProduct(id).getID()); }
- 07-16-2011, 01:10 AM #25
You don't show where id is defined or where it is given values.
Why is the id value always 132? Should the id value change as you go around the loop?
Where do you use the loop index i?
- 07-16-2011, 01:23 AM #26
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Java Code:This code is where the item is being added: I add it like this: (new Product(132, "Clock Radio")) and if I add the same line two or more times, then I think the quantity should keep a record on how many items there are. So no, the id value should not change. I insert 132 into the parameter in the method numberInStock The index i it used to loop through the list public void addProduct(Product item) { stock.add(item); }
- 07-16-2011, 02:31 AM #27
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
This is the constructor in the class Product, where quantity is set to 0. This is the value I get returned when I use the method numberInStock and not the number of items which are stored in stock arraylist
Java Code:public Product(int id, String name) { this.id = id; this.name = name; quantity = 0; }
- 07-16-2011, 02:41 AM #28
Can you write a loop to print out all the product ids in stock with their quantities?
Print the product ids in column one and the quantities in column two.
- 07-16-2011, 03:09 AM #29
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
I'm not shore if I understand you correct, but is it something like this for loop?
The output from the code below:
Quantity :0
ID :37
Quantity :0
ID :37
Quantity :0
ID :37
Quantity :0
ID :37
Java Code:for(int i = 0; i < stock.size(); i++) { System.out.println("Quantity :" + findProduct(id).getQuantity()); System.out.println(); System.out.println("ID :" + findProduct(id).getID()); }
- 07-16-2011, 03:19 AM #30
Here is what I was looking for:
You only print the quantity for product id = 37. Is that the only product in the stock list?PHP Code:Product ID Quantity 101 4 122 1 133 6 201 3
For testing you should add at least 6 different product ids to the list with different quantities for each one.
- 07-16-2011, 03:31 AM #31
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
I have this method which print out details
With this output:
Product: 23: Microwave Oven stock level: 0
Product: 23: Microwave Oven stock level: 0
Product: 23: Microwave Oven stock level: 0
Product: 37: Mobile Phone stock level: 0
Product: 132: Clock Radio stock level: 0
Java Code:public void printProductDetails() { Iterator it = stock.iterator(); while(it.hasNext()) { Product product = (Product) it.next(); System.out.println("Product: " + product); // System.out.println("Stock " + "Hello" + " " + stock); } }
- 07-16-2011, 03:50 AM #32
But that is not what was requested.
The output was to be only the product id and the quantity.
Nothing else.
Does the class has methods for you get those values? Can you use them?
This is a very simple project. If you don't see how to use the methods in the Product class for this then you won't be able to use the Product class for your other project.
- 07-16-2011, 03:41 PM #33
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Hello, Norm!
I think this short code actually is sufficient for the task to find quantity
It is only the return line that is needed.
The next method that I'm about to do is a method to incresae the quantity. Until that method is done the return will return only 0
Or what do you think?
Java Code:public int numberInStock(int id) {// System.out.println("Quantity: " + findProduct(id).getQuantity()); return findProduct(id).getQuantity(); //System.out.println("Quantity: " + findProduct(id).getQuantity()); // } // return 0; }
- 07-16-2011, 03:55 PM #34
I think you should compile and execute it and see what happens. Test it with many different cases:
product not found, quantity = 0 and quantity > 0
- 07-16-2011, 04:13 PM #35
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
It seems to work at least when I'm insert a number into the parameter, a number I know exist in the list of quantity, but if I insert a number which there are no items with that number, then I get:
java.lang.NullPointerException
at StockManager.numberInStock(StockManager.java:124)
- 07-16-2011, 04:15 PM #36
What does the findProduct() method return when there are no items with that number?
- 07-16-2011, 04:32 PM #37
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
The findProduct() return this when there are no items with that number
java.lang.NullPointerException
at StockManager.numberInStock(StockManager.java:124)
I try to use an if test stop the nullpointer, but it don't work yet, any idea?
- 07-16-2011, 04:34 PM #38
Yes, you could use an if test to detect if a null was returned.
- 07-16-2011, 10:05 PM #39
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
I'm stuck on this method numberInStock
Any idea on how I should do the test for 0?
should I use an if test inn an if test?
- 07-16-2011, 10:09 PM #40
Similar Threads
-
call a JSP method
By sauravsinha in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 09-26-2011, 04:19 PM -
How do I call this method
By africanhacker in forum New To JavaReplies: 1Last Post: 06-29-2011, 04:03 PM -
How to call this method?
By Rockefella in forum New To JavaReplies: 12Last Post: 01-20-2011, 04:32 PM -
how to call method?
By leapinlizard in forum New To JavaReplies: 9Last Post: 04-29-2009, 11:55 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks