View Javadoc

1   /*
2    * Created on 6 Nov 2007
3    * 
4    */
5   package org.sourceforge.jemm.sample.demo1;
6   
7   import org.sourceforge.jemm.sample.AbstractDemo;
8   import org.sourceforge.jemm.sample.demo1.model.Account;
9   import org.sourceforge.jemm.sample.demo1.model.Person;
10  import org.sourceforge.jemm.sample.demo1.model.SavingsAccount;
11  
12  /**
13   * A simple demonstration program. The demo uses a persistent memory store (the
14   * data is stored in memory and persisted to disk on shutdown. It is a simple
15   * example that looks to see if a Foo is already stored, if so it finds the
16   * associated Bar and increments the counter. If no Foo is found it creates a
17   * new one and places it into the store.
18   * 
19   * @author Rory Graves
20   * 
21   */
22  public final class Demo1 extends AbstractDemo {
23  	static final char POUND ='\u00A3';
24  
25  	/**
26  	 * Private no-args constructor to stop instantiation.
27  	 */
28  	private Demo1(String cmdArgs[]) {
29  		super("Demo1",false,cmdArgs);
30  	}
31  
32  	/**
33  	 * Main method, runs the example using the command line store configuration
34  	 * 
35  	 * @param args Program arguments - see AbstractDemo.
36  	 */
37  	public static void main(String[] args) {
38  		Demo1 demo = new Demo1(args);
39  		demo.runDemo();
40  	}
41  
42  	/**
43  	 * Runs the actual demo with the given store implementation.
44  	 */
45  	public void runDemo() {
46  		try {			
47  			Account account;
48  			// sets foo as a root object, foo and any objects
49  			Person storedPerson = (Person) store.getRoot("client");
50  
51  			if (storedPerson == null) {
52  				System.out.println("person was null - storing new");
53  				// creates a model class - live until it goes out of scope.
54  				Person person = new Person("Fred");
55  
56  				account = new SavingsAccount();
57  				person.setAccount(account);
58  				store.setRoot("client", person);
59  			} else {
60  				System.out.println("Person was valid");
61  				System.out.println("Adding " + POUND + "100");
62  				account = storedPerson.getAccount();
63  				account.deposit(100.0);
64  			}
65  			
66  			System.out.println("CurrentBalance=" + POUND + account.getBalance());
67  		} finally {
68  			store.shutdown();
69  		}
70  	}
71  }