• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

My loop isn't working

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[size=12]
I'm making an inventory program for class. It has a loop that should cycle through the array and print the information. Part of the loop is supposed to add the value from each inventory item together to get the total value of the inventory. However, instead the program simply adds the last value entered to itself repeatedly. There is no compiler error, but the program isn't doing what it should.

Here is my code for the Product class.


And my loop


[/size]
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
assuming the loop you are talking about is the one on lines 66-72 of the InventoryDisplay class...it makes no reference to any array. I am GUESSING you want to loop over your "inventory" array, but nowhere in that block do you make reference to it. At least, not on line 68, where you are making your sum.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tiffany Walker wrote:I'm making an inventory program for class. It has a loop that should cycle through the array and print the information. Part of the loop is supposed to add the value from each inventory item together to get the total value of the inventory. However, instead the program simply adds the last value entered to itself repeatedly. There is no compiler error, but the program isn't doing what it should.


My reply isn't directed at your code, but more to inform you of a design pattern called "cooperative classes".

Since your Inventory system is likely to need to keep track of loads of "products", what you do is to split your class into Product (stuff that has to do with a specific product) and Products (stuff that has to do with many Products).

The only rule you need to generally follow, Java-wise, is that both classes should be in the same package; and you may need to make some private methods package-private.

One of the uses for a class like Products is for holding summary information (eg, total cost), and it works like this:
Instead of running through your entire list, just have your Product object talk to the Products object every time a change is made. That way, you only need to add/subtract the value from whatever running total you have.

Inventory maths is pretty simple; it's the model that gets complicated (often unnecessarily, even by experts; and believe me, I know (30 years, mostly in inventory systems of some description)). The trick is to keep it simple.

Winston

Oh, and while I'm on my hobby-horse: you should be very careful about using words like "value" with a Warehouse manager:
  • Value - Hunh? Whassat??
  • Cost (the amount you paid) - Might mean something, but usually filled in on a form by Head Office.
  • RetailPrice - Again, may have some meaning, though probably only grudgingly, because it usually means changing price tags.
  • RecoverableValue - Absolutely. Read: "if this warehouse burns down, what's it worth?"

  • (the above from "the Yorkshire guide to Inventory Management")
     
    Tiffany Walker
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for replying. So really I should redesign the classes, not tweak the code?
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Tiffany Walker wrote:Thank you for replying. So really I should redesign the classes, not tweak the code?


    A toughie. My reply was intended to address the problem in general; not yours.
    It's quite likely that you'll get a solution to your code problem (ie, getting it to compile and run); the question for you then is: is that enough?

    For now, my advice would be: get your current code running with the advice you get here; then read my post and see if it makes sense. If so, bang out a new question.

    Winston
     
    Tiffany Walker
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the help Winston. I got the code working.
    total += inventory[counter].value(CD.getPrice(), CD.getUnits());
    fixes my problem. I do have another class I didn't post here called
    InventoryOfCD's that shows how the products occupy the inventory.
    I made it honestly mimicking the textbook on a similar issue. Is that what
    you referred to?
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Tiffany Walker wrote:I do have another class I didn't post here called
    InventoryOfCD's that shows how the products occupy the inventory.
    I made it honestly mimicking the textbook on a similar issue. Is that what
    you referred to?


    Sounds like it. If you put your 'total' field in there, you could just add or subtract from it whenever you put CDs in or take them out of inventory rather than having to recalculate it every time.

    Not that there's anything wrong with what you're doing now; it's just a different approach. Your way has the advantage of being dead simple, so it's unlikely to go wrong. Keeping a running total is a bit trickier. For a home CD collection or a small shop, your way is probably best; but for a Virgin distribution centre, the time you save from running through millions of CDs every time you want a total may make it worthwhile.

    Either way, you might want to think about adding a 'getTotalValue()' method to your inventory class and putting the calculation code in there. That way you can change the way you do it later on without affecting any other code.

    As with most things in programming, there's usually more than one way to do something and often no "right" answer. The trick is to know what the alternatives are.

    Winston
     
    Space pants. Tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic