1 17 18 package org.objectweb.jac.samples.distransbank; 19 20 26 public class Account { 27 28 private String name; 29 private double balance; 30 31 public static void main(String [] args) throws Exception { 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 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 toString() { 95 104 return "Account: "+name+", balance: "+getBalance(); 105 } 106 } 107 | Popular Tags |