Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 52

Thread: Method call

  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    If it returns 0 then is that the correct quantity for the product it is looking at?
    Can you write a loop to print out all the product ids with their quantities?

  2. #22
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Any idea what I'm doing wrong?

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    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.

  4. #24
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    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());
                    }

  5. #25
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    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?

  6. #26
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    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);
        }

  7. #27
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    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;
        }

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    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.

  9. #29
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    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());
                    }

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    Here is what I was looking for:
    PHP Code:
       Product ID    Quantity
           101             4
           122             1
           133             6
           201             3
    You only print the quantity for product id = 37. Is that the only product in the stock list?
    For testing you should add at least 6 different product ids to the list with different quantities for each one.

  11. #31
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    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);
            }
            
            
        }

  12. #32
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    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.

  13. #33
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    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; 
                }

  14. #34
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    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

  15. #35
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    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)

  16. #36
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    What does the findProduct() method return when there are no items with that number?

  17. #37
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    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?

  18. #38
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    Yes, you could use an if test to detect if a null was returned.

  19. #39
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    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?

  20. #40
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    do the test for 0
    You could use an if test:
    if (variable == 0) ...

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. call a JSP method
    By sauravsinha in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 09-26-2011, 05:19 PM
  2. How do I call this method
    By africanhacker in forum New To Java
    Replies: 1
    Last Post: 06-29-2011, 05:03 PM
  3. How to call this method?
    By Rockefella in forum New To Java
    Replies: 12
    Last Post: 01-20-2011, 05:32 PM
  4. how to call method?
    By leapinlizard in forum New To Java
    Replies: 9
    Last Post: 04-30-2009, 12:55 AM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 09:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •