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")