aspose file tools*
The moose likes Beginning Java and the fly likes OVERWRITING OBJECTS IN AN ARRAY LIST HELP PLEASE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "OVERWRITING OBJECTS IN AN ARRAY LIST HELP PLEASE" Watch "OVERWRITING OBJECTS IN AN ARRAY LIST HELP PLEASE" New topic
Author

OVERWRITING OBJECTS IN AN ARRAY LIST HELP PLEASE

jonathan cassar
Greenhorn

Joined: Jan 17, 2010
Posts: 4
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

Joined: Mar 24, 2008
Posts: 3851

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
jonathan cassar
Greenhorn

Joined: Jan 17, 2010
Posts: 4
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

Joined: Oct 02, 2003
Posts: 11475
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OVERWRITING OBJECTS IN AN ARRAY LIST HELP PLEASE