This is my first post, so sorry if i post something wrong here. Anyway, I am working on a class assignment and I need to meet the following requirements:
• Modify the Inventory Program so the application can handle multiple items. Use an ArrayList to store the items. DO NOT PROMPT USER FOR INPUT; initialize the array with hard coded values.
• The output should display the information the first product in your ArrayList, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product.
• Create a method to calculate the value of the entire inventory. Call this method and display the results. (Hint: Use a loop to make your calculation.)
• Create another method to sort the array items by the name of the product.
I was able to code for part 1 and part 3 of the above requirements, but part 2 (displaying) and part 4 (sorting) I am completely stumped!! I have tried multiple variations of overriding a toString() method but just get errors. What in God's name am I missing? Code follows:
Any help would be greatly appreciated...Thanks in advance...
Thanks for the reply. Well, I am looking to display at least one instance as follows:
Item # Name In Stock Price each
001a Java 101 50 19.99
001b Python 101 50 19.95 - this output can be optional
001c C ++ 101 25 25.99 - this output can be optional
What I get for output is:
The line with the Item #, etc. displays fine. It has been suggested to me to do an override of a toString() method, but I wouldn't know where to insert that...
Java doesn't know to turn objects into Strings. It has to be explicitly told.
Dan D'Apice wrote:OK, so that's where the override comes in?
In the general case, overriding is used when you want a subclass to provide different behavior than its parent class. In this particular case, you're calling System.out.println(X), which ultimately ends up calling X.toString(). Since you didn't override toString(), you got the parent's version--Object's version.
I'm going to give it a try and see what other errors I get...
That's the spirit!
My output is as such:
I still need to populate those other columns with the respected data from the arraylist. I feel like I've been pushed into the deep end of the pool without being taught how to swim....now what am I missing? Thanks for all the help so far, everyone :-D
Just my opinion but String.format may be a little advanced at this stage of the game. While I've used it .. I still have to look up the details of the format specifiers every time But to answer your question, if you look at the documentation the method expects a "format string" followed by a list of arguments ie
In your code you've supplied the arguments: itemNumber, itemName, etc... - but forgot the format string.
As you already have Book objects, why not just use existing the methods in the Book class to get the name, price, etc..?
That means other classes need to “know about” the book class. Not at all object‑oriented programming; the book class should take care of itself. If you change the implementation of the book class, (or even worse, its public interface), such a method will be broken. What if you add an author field to the book class later?
Corrie L Sherone wrote:Not to discourage you from learning about overriding but there is more than one way to skin a cat ("ouch"!). Do not forget your Book class has all those perfectly good methods for obtaining the name, price etc. All you need is a Book object from your list.
Overriding (and polymorphism) is the way to go.
Thanks to everyone again for the great suggestions. I used Saket's suggestion. Here is what I have as of this morning:
Not sure why the output is distorted within the code tags? I realized I "cheated" in spots by tweaking the output with additional "\t", but it looks even. I am not sure why the data from index (1) came out misaligned, but the tweaks worked. Now on to the last part:
• Create another method to sort the array items by the name of the product
I'll start off by reading our God awful text from class and see what I can come up with. Wish me luck, I'm going to need it...
Dan D'Apice wrote:So, after some time I've gotten a little further. I love Java...NOT!!!...what a PITA!!
I doubt whether it's Java specifically. All programming languages have their foibles; and ALL of them require you to be precise.
I feel like I've been pushed into the deep end of the pool without being taught how to swim....now what am I missing?
I think everybody else is giving you good advice on the mechanics, so I'm going to concentrate on the data.
What is a Book? Is it a Title? Is it hardback? Paperback? Braille? Audio? What is it's "price": MSRP?
Don't get too bogged down with it, but the fact is that a Book is a Book, not an item of your Stock; and you may well have several items of Stock, which all equate to the same Book.
Now you've abstracted that by having a stock quantity in your Book class, which is understandable; but don't Lamps and playing cards and TV's also have stock quantities? If you set it up that way, you'll have to repeat the stockQuantity for every type of Stock item you hold, and you'll also have to repeat the logic to increment and decrement it properly.
What about, instead, having a StockItem class that holds all the information you need about any item? - Eg: Quantity in Stock, Quantity on Order, Unit price, etc. - and then have that StockItem reference a particular Book, or TV, or whatever?
Do you see how that models your inventory better? "Inventory" then becomes a List of the StockItems you hold; and a StockItem can be anything from Gone With The Wind, to a Sony PlayStation, to a tin of Beans. Furthermore, it separates "what it is" from how it gets dealt with by your Application.
A simple, but easy, mnemonic for thinking about Stock is "CIC", or "Category-Item-Copy":
Category: Something that categorizes an Item - eg, a Book; but it could be something broader, like "Dried Goods".
Item: A specific Book: eg, Gone With The Wind.
Copy: An individual copy of that Book on your shelves. In larger stores these will often be barcoded, but I wouldn't worry about that in your case; your 'quanitity' fields should be just fine as a generalization of a 'copy'.
In large systems, there may be more than one level to each of those definitions, but you'd be surprised how often that basic pattern holds up.
Don't sweat it too much, but maybe it's given you a few things to think about.
Campbell Ritchie wrote:That means other classes need to “know about” the book class. Not at all object‑oriented programming; the book class should take care of itself. If you change the implementation of the book class, (or even worse, its public interface), such a method will be broken. What if you add an author field to the book class later?
Overriding (and polymorphism) is the way to go.
Hm.. I was reading the requirements a little differently. I didn't think they were specifically asking for a method that dumped *all* properties of an object. If that's the case, then I agree. The implementation should be up to the class itself. My take was the simple program needed to print out specific properties - like for a report. In which case it normally would need to know about specific methods of that object. Granted I haven't had my coffee yet, so maybe I'm still off base here.
Just wanted to give a quick update: I wasn't able to figure out (from the syllabus or our wonderful text) the sorting requirement. I input the following code in for good measure and it didn't return any errors:
There is another requirement that is due Sunday, but right now I need to de-tox from coding for a bit. I am not cut out for this. 3 more weeks left to class and I am humbly bowing out. Thanks again to everyone for your awesome suggestions and support! You all have what it takes...
If you have been told to use that method, then I suggest you do three things:-
Dan D'Apice wrote:There is another requirement that is due Sunday, but right now I need to de-tox from coding for a bit. I am not cut out for this. 3 more weeks left to class and I am humbly bowing out. Thanks again to everyone for your awesome suggestions and support! You all have what it takes...
Well not everyone chooses to program for a living. Even if you enjoy it, it still takes study and practice like any field. But do give yourself some credit. You completed two parts on your own and your instincts about the others were good ie Overriding toString and that you need to use a Comparable or a Comparator for sorting.