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

Unable to display data from an ArrayList

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
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...
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take one piece at a time.

What exactly did you try for the display part, and what exactly was the problem with it?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Dan D'Apice
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeff,
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:

inventory.Book@1c8825a5
inventory.Book@2e5f8245
inventory.Book@6197cc

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...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your Book class does not override the toString() method, so it uses the one it inherits from Object, which just prints the class name and a number that may correspond roughly to the hashCode or address or somesuch.

Java doesn't know to turn objects into Strings. It has to be explicitly told.

 
Dan D'Apice
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so that's where the override comes in? I'm going to give it a try and see what other errors I get...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Dan D'Apice
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, after some time I've gotten a little further. I love Java...NOT!!!...what a PITA!! OK, vent session is over. Here is what I have so far:




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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aren't you misusing String.format ?
 
Corrie L Sherone
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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..?


 
Ranch Hand
Posts: 32
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What is the problem in overriding this way
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What is the problem in overriding this way


With all members values concatenated like this, it's going to be difficult to read.
 
Marshal
Posts: 78675
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

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.
 
Dan D'Apice
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All (again),
Thanks to everyone again for the great suggestions. I used Saket's suggestion. Here is what I have as of this morning:


OUTPUT:


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...
 
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

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.

HIH

Winston
 
Corrie L Sherone
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Campbell Ritchie
Marshal
Posts: 78675
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think we are in agreement, if you take the different requirements into consideration
 
Dan D'Apice
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
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...
 
Campbell Ritchie
Marshal
Posts: 78675
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should only have a compareTo() method if there is a “natural order” to books. Look at this interface. Do you sort books by author, by title, by ISBN, or what?
If you have been told to use that method, then I suggest you do three things:-
  • 1: Add “implements Comparable<Book>” after the class name and before the first {
  • 2: Change the method to take Book b as its parameter rather than Object o
  • 3: Get rid of the (Book) cast.
  • If books don’t have an order, you can create Comparator<Book>s for sorting by different criteria. You can find more about sorting here.
     
    Campbell Ritchie
    Marshal
    Posts: 78675
    374
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you go through those links, you may find that the ready‑made sorting methods require a Comparable or a Comparator.
     
    Campbell Ritchie
    Marshal
    Posts: 78675
    374
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    … and, to get me a bit closer to 30000 posts, have a look at the java.lang.String (←link) class and look for Comparable and Comparator and a compareTo method. See if that helps you work out what your compareTo method is doing.
     
    Corrie L Sherone
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
     
    Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic