Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OVERWRITING OBJECTS IN AN ARRAY LIST HELP PLEASE

 
jonathan cassar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to code using java the following requirements.

The user must first enter a some requirements of an item (item code, item description, item weight and item price). Then upon entering the code once again the application should print the item description, the item weight and the item price.

I've managed to make the user input all the items and make the system print the requirements upon entering the code for the second time. However only the last product entered can be retrived from the system. If the user enters the 3 items and he needs to retrieve the first, the system prints and error message.

The problem lies in the fact that my array Lists are being overwritten each time the user is entering new data. Its as if the array list contains only one element all through the execution of the program. How can I get pas this problem. Any help please

I'm attaching my code which uses 3 classes









 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jonathan,
Welcome to JavaRanch

Please use code tags when posting code in the forum (you can edit your post and add [code][/code] tags to the code), It's very hard to read without them.
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you hacked an ItemDetails class, which was designed to hold the details for one data item, to "hold" multiple items, but changing it to keep a history of the values called by the setters.

Instead of doing that (having a single ItemDetails object with many arraylists of the fields), would it not be better to keep the ItemDetails class as is, and have multiple items by having an ArrayList of ItemDetails objects?

Henry
 
jonathan cassar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Henry

Sorry for my ignorance but I didn't understand your reply. I'm new to programming. Can you explain further please.

Thanks for your reply.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry is saying this...

it appears that you may have 'tweaked' an old, existing class. The original design would hold all the info for a single item. the tweak attempts to make the item hold lots of info for lots of items.

Henry's suggested says you should go back to the original design for the class. Then write code to manipulate a collection of these objects.

think of it this way. Originally, you had code for an egg. You took that code and tried to figure out a way to put two egg yolks and two egg whites into a single egg shell.

instead, you should create an egg carton, which will hold multiple eggs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic