KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > account > AccountManagerImpl


1 /*
2  * Copyright (c) 2003 by The Jackass Team
3  * Licensed under the Open Software License version 2.0
4  */

5 package account;
6
7 import java.util.Hashtable JavaDoc;
8
9 import ve.luz.ica.jackass.component.ApplicationContext;
10 import ve.luz.ica.jackass.component.StatelessContext;
11 import ve.luz.ica.jackass.component.StatelessHook;
12
13 /**
14  *
15  * @author Carlos Arévalo
16  */

17 public class AccountManagerImpl extends AccountManagerPOA implements StatelessHook
18 {
19     private StatelessContext compContext = null;
20     private ApplicationContext appContext = null;
21     private static final String JavaDoc ACCOUNT_NOT_FOUND = "Account not found";
22
23     private static Hashtable JavaDoc accounts = new Hashtable JavaDoc();
24
25     private AccountData getAccount(int id) throws Exception JavaDoc
26     {
27         AccountData account = (AccountData) accounts.get(new Integer JavaDoc(id));
28         if (account == null)
29         {
30             throw new Exception JavaDoc(ACCOUNT_NOT_FOUND);
31         }
32         return account;
33     }
34
35     /**
36      * @see account.AccountManagerOperations#createAccount(int, java.lang.String, int)
37      */

38     public void createAccount(int id, String JavaDoc name, double initialBalance) throws OperationFailed
39     {
40         AccountData account = new AccountData(id, name, initialBalance);
41         accounts.put(new Integer JavaDoc(id), account);
42     }
43
44     /**
45      * @see account.AccountManagerOperations#deleteAccount(int)
46      */

47     public void deleteAccount(int id) throws OperationFailed
48     {
49         accounts.remove(new Integer JavaDoc(id));
50     }
51
52     /**
53      * @see account.AccountManagerOperations#deposit(int, double)
54      */

55     public void deposit(int id, double amount) throws OperationFailed
56     {
57         try
58         {
59             AccountData account = this.getAccount(id);
60             account.balance += amount;
61         }
62         catch (Exception JavaDoc e)
63         {
64             throw new OperationFailed(e.getMessage());
65         }
66     }
67
68     /**
69      * @see account.AccountManagerOperations#withdraw(int, double)
70      */

71     public void withdraw(int id, double amount) throws OperationFailed
72     {
73         try
74         {
75             AccountData account = this.getAccount(id);
76             account.balance -= amount;
77         }
78         catch (Exception JavaDoc e)
79         {
80             throw new OperationFailed(e.getMessage());
81         }
82     }
83
84     /**
85      * @see account.AccountManagerOperations#getBalance(int)
86      */

87     public double getBalance(int id) throws OperationFailed
88     {
89         try
90         {
91             AccountData account = this.getAccount(id);
92             return account.balance;
93         }
94         catch (Exception JavaDoc e)
95         {
96             throw new OperationFailed(e.getMessage());
97         }
98     }
99
100     /**
101      * @see account.AccountManagerOperations#getAccountData(int)
102      */

103     public AccountData getAccountData(int id) throws OperationFailed
104     {
105         try
106         {
107             AccountData account = this.getAccount(id);
108             return account;
109         }
110         catch (Exception JavaDoc e)
111         {
112             throw new OperationFailed(e.getMessage());
113         }
114     }
115
116     /**
117      * @see ve.luz.ica.jackass.component.StatelessHook#jackassSetContext(ve.luz.ica.jackass.component.JackassContext)
118      */

119     public void jackassSetContexts(ApplicationContext appCtx, StatelessContext compCtx)
120     {
121         this.appContext = appCtx;
122         this.compContext = compCtx;
123     }
124
125     /**
126      * @see ve.luz.ica.jackass.component.StatelessHook#jackassCreate()
127      */

128     public void jackassCreate()
129     {
130         // Nothing to do in this sample component
131
}
132
133     /**
134      * @see ve.luz.ica.jackass.component.StatelessHook#jackassRemove()
135      */

136     public void jackassRemove()
137     {
138         // Nothing to do in this sample component
139
}
140 }
141
Popular Tags