6

I'm trying to learn Java and OOP by creating a monopoly banker program. However, I would like some object variables to be saved after I exit the program, so that after playing half of a monopoly game I can exit my program and then restart it with all the player balances saved.

I'm guessing this requires some sort of database?

Here is a section of my code; I am trying to save the "balance" variable for all of my objects (players) after I exit my program.

   public class Monopoly {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    //creates the users (name classes)
players player1 = new players();
players player2 = new players();
players player3 = new players();
players player4 = new players();



while (true)  {

player1.balance=player1.finalbalance;
player2.balance=player2.finalbalance;
player3.balance=player3.finalbalance;
player4.balance=player4.finalbalance;
tshepang
  • 12,111
  • 21
  • 91
  • 136
user3133300
  • 607
  • 4
  • 13
  • 23
  • 1
    A database is a common tool for this. But any persistence medium will work. You can save state to an XML file, a text file, a database, anything that exists after the application has been closed. As for *how*, that's a pretty broad topic. I recommend finding a tutorial on using a database with Java and learning from that. – David Jan 04 '14 at 18:32
  • 1
    http://docs.oracle.com/javase/tutorial/essential/io/objectstreams.html – Submersed Jan 04 '14 at 18:33

4 Answers4

5

I would just serialize this object and deserialize it after you resume your game. I think it is the best way. Here you can find the way how to do that. http://www.tutorialspoint.com/java/java_serialization.htm

1

Maybe I didn't really understant your question but if you need a method to catch the exit before the program exit (CTRL-C for instance) you van use an "exit hook", usefull also to free connection

see

Useful example of a shutdown hook in Java?

Community
  • 1
  • 1
venergiac
  • 7,469
  • 2
  • 48
  • 70
1

While you could of course use a database, this would be quite overkill for a simple offline game. In that case it would be more common to save the game-state to a file (a "savegame").

By using the class ObjectOutputStream, you can write objects to a file. It converts the objects into a format which can then later be read with the class ObjectInputStream .

Philipp
  • 67,764
  • 9
  • 118
  • 153
0

You have to save it to a file somewhere, the only surefire way to carry information between programs. You'll need to decide a format to save your file with. Look up some tutorials for reading/writing files, I'm sure it will help.

Maurdekye
  • 3,597
  • 5
  • 26
  • 43
  • So I guess there is no really simple way to do this? I thought there was a really beginner-friendly way to do this. – user3133300 Jan 04 '14 at 18:43
  • Don't worry. File management isn't THAT hard. But unless you want to get really, really deep and fool with the memory registry there's really no easier way. Programs are self contained, and the only thing that lasts outside of them is what they change on the computer, and files are the only real way to change information on the computer – Maurdekye Jan 04 '14 at 18:57