KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > samples > distransbank > Account


1 /*
2   Copyright (C) 2001-2003 Lionel Seinturier <Lionel.Seinturier@lip6.fr>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.samples.distransbank;
19
20 /**
21  * Sample program using the distrans AC.
22  *
23  * @author Lionel Seinturier
24  * @version 1.0
25  */

26 public class Account {
27     
28     private String JavaDoc name;
29     private double balance;
30
31     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
32         
33         Account bob = new Account();
34         Account robert = new Account();
35         
36         bob.setName("Bob");
37         robert.setName("Robert");
38         
39         double initial = (int) (Math.random()*100) * 2;
40         
41         System.out.println("Set balance Bob: "+initial);
42         bob.setBalance(initial);
43         System.out.println(bob);
44         System.out.println(robert);
45         System.out.println();
46         
47         System.out.println("Test for commit");
48         System.out.println("Transfert from Bob to Robert: "+initial/2);
49         transfert(bob,robert,initial/2);
50         System.out.println();
51         
52         System.out.println("After transaction");
53         System.out.println(bob);
54         System.out.println(robert);
55         System.out.println();
56         
57         System.out.println("Test for rollback");
58         System.out.println("Transfert from Bob to Robert: "+initial*2);
59         transfert(bob,robert,initial*2);
60         System.out.println();
61         
62         System.out.println("After transaction");
63         System.out.println(bob);
64         System.out.println(robert);
65         System.out.println();
66         
67         System.exit(1);
68     }
69     
70     public void setName(String JavaDoc name){
71         this.name = name;
72     }
73     
74     public void setBalance(double balance) { this.balance = balance; }
75     public double getBalance() { return balance; }
76     
77     public void credit( double amount ) {
78         System.out.print(name+": "+balance+" -> ");
79         balance += amount;
80         System.out.println(balance);
81     }
82     
83     public void withdraw( double amount ) {
84         System.out.print(name+": "+balance+" -> ");
85         balance -= amount;
86         System.out.println(balance);
87     }
88     
89     public static void transfert( Account from, Account to, double amount ) {
90         to.credit(amount);
91         from.withdraw(amount);
92     }
93     
94     public String JavaDoc toString() {
95         /**
96          * Accessing directly the fields (eg balance) may lead
97          * to inconsistencies after a rollback
98          * (the initial state is restored in
99          * the persistant storage but not in the objects).
100          * Calling a getter forces the loading of the state
101          * from the persistant storage
102          * (getters are wrapped by ReadWrapper instances).
103          */

104         return "Account: "+name+", balance: "+getBalance();
105     }
106 }
107
Popular Tags