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

Persistence problem (was "array problem")

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is probably very simple for someone with a little more knowledge than me but I am trying to get the program to save the new information that is entered into the input box. For example, if you enter a new charm, supplier, etc--having it actually add the charm to stay. when I close the program, the info entered is erased. Your help is really appreciated. I have been struggling with Java.


Edit by mw: Changed subject line to be more accurate.
[ March 06, 2007: Message edited by: marc weber ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kendra Payne:
...when I close the program, the info entered is erased...


It sounds like you're looking for a way to get object "persistence." Basically, you need to store the object's state information somewhere (like a hard drive), and be able to use that information to re-create the object later. You might look at Object Streams in the Java Tutorial or the Object Serialization section from Thinking in Java.
 
Kendra Payne
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am so confused as to what to do. Do you know of any examples anywhere that are similar to this?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I would reconsider this design a little bit.

It seems that each Charm should be an object that HAS-A title, supplier, id, sales, inventory, totalSold, etc. The class you are currently calling "CHARM" is really a "Charm Manager" that should HAVE instances of Charm. More specifically, it seems like CharmManager should HAVE-A List of Charms, and you should be able to save that list so that you can open it back up later.

So first, I would write a separate Charm class, and make sure it implements the Serializable interface. (This interface is just a "tag" -- there are no methods you need to implement.)

Next, I would modify your CharmManager (currently called "CHARM") class so that it has a List of Charms. Then I would adapt the example in Thinking in Java to write 2 new methods for the CharmManager class: One method for saving the List (writing the object out as its own file), and a second method for loading the List (reading the file and re-creating the original list).

Give this a try, and see what you come up with.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might help to think of your program like this:

Charm Manager is an application, similar to a spreadsheet application like Excel.

When you launch Excel, it has no data -- you either create a new spreadsheet or open an existing spreadsheet. The same might be true with your application. When you launch Charm Manager (start your Java program), it will have no data -- you either create a new list of charms or load a previously saved list. Instead of .xls files (for Excel), your application might have .chm files (an extension I made up for Charms).

These files are saved and loaded using serialization. For example, suppose Charm is defined as a separate class that implements Serializible, and your CharmManager has a List of Charms. Then (following the Thinking in Java example) you might have something like...
 
Kendra Payne
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much for your help. I have to turn in a final working program on Thurs(tomm) and have nowhere near the comprehension needed to develop one. (as the rest of the class doing this). So do I just put that code in my project? Does that save it somewhere? This has me frustrated to tears.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kendra Payne:
...So do I just put that code in my project? Does that save it somewhere? ...


That code is just some fragments to suggest an approach. It assumes that some other pieces are in place, and uses a fundamentally different approach than what you've done with arrays. So no, it wouldn't make sense to just put that code in unless you can put the other pieces together.

At this point, I think the best thing to do is turn in what you've done. It might not be complete, but it does show a lot of work that you should get some credit for. In your next project, I suggest getting some early feedback on your design ideas before doing too much coding.
 
Kendra Payne
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,
this is my last Java class. I don't even know why we have to take 2 quarters of it, it is not enough to do anything with. Nobody in the class wants to be a programmer, we are business majors.
 
Could you hold this kitten for a sec? I need to adjust this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic