1 /*
2  * @(#) $Id: Bank.java,v 1.2 2003/07/08 08:13:53 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk.example.bank.server;
11
12import java.math.BigInteger;
13import java.math.BigDecimal;
14import java.util.HashMap;
15import java.util.Iterator;
16import org.jstk.example.bank.*;
17import java.security.AccessController;
18
19public class Bank implements BankIntf, java.io.Serializable {
20    private BigInteger nextAcctNo;
21    private HashMap acctsTable = null;
22    private transient BankPersistenceManagerIntf pmi = null;
23
24    public Bank(){
25        nextAcctNo = new BigInteger("1000");
26        acctsTable = new HashMap();
27    }
28    public void setPersistenceManager(BankPersistenceManagerIntf pmi){
29        this.pmi = pmi;
30    }
31    public AccountIntf openAccount(BigDecimal initialDeposit){
32        checkPermission("*", "open");
33        SecurityManager security = System.getSecurityManager();
34        Account acct = new Account(nextAcctNo.toString());
35        try {
36            acct.deposit(initialDeposit);
37        } catch (Exceptions.AccountClosed ac){
38            // Nothing.
39        }
40        acctsTable.put(acct.getAcctNo(), acct);
41        nextAcctNo = nextAcctNo.add(BigInteger.ONE);
42        if (pmi != null){
43            acct.setPersistenceManager(pmi);
44            pmi.save();
45        }
46        return acct;
47    }
48    public void closeAccount(String acctNo)
49            throws Exceptions.AccountNotFound, Exceptions.AccountClosed{
50        checkPermission(acctNo, "close");
51        Account acct = (Account)acctsTable.get(acctNo);
52        if (acct != null)
53            acct.close();
54        else
55            throw new Exceptions.AccountNotFound();
56        if (pmi != null)
57            pmi.save();
58    }
59    public AccountIntf getAccount(String acctNo) throws Exceptions.AccountNotFound{
60        checkPermission(acctNo, "get");
61        Account acct = (Account)acctsTable.get(acctNo);
62        if (acct == null)
63            throw new Exceptions.AccountNotFound();
64        return acct;
65    }
66    public Iterator accounts(){
67        checkPermission("*", "list");
68        return acctsTable.values().iterator();
69    }
70    Iterator _accounts(){
71        return acctsTable.values().iterator();
72    }
73    private void checkPermission(String name, String action){
74        SecurityManager security = System.getSecurityManager();
75        if (security != null) {
76            BankPermission bp = new BankPermission(name, action);
77            AccessController.checkPermission(bp);
78        }
79    }
80
81    public static void main(String[] args) throws Exception{
82        Bank bank = new Bank();
83        Account acct1 = (Account)bank.openAccount(new BigDecimal("100.0"));
84        Account acct2 = (Account)bank.openAccount(new BigDecimal("200.0"));
85        acct2.deposit(new BigDecimal("4000.0"));
86        acct2.withdraw(new BigDecimal("3000.0"));
87
88        Iterator itr = bank.accounts();
89        int idx = 0;
90        while (itr.hasNext()){
91            Account acct = (Account)itr.next();
92            System.out.println("Account[" + idx + "]: " + acct);
93            System.out.print(acct.getStatement());
94            System.out.flush();
95            ++idx;
96        }
97    }
98}