1 /*
2  * @(#) $Id: Account.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.BigDecimal;
13import java.util.Vector;
14import java.util.Date;
15import java.security.AccessController;
16import org.jstk.example.bank.*;
17
18public class Account implements AccountIntf, java.io.Serializable {
19    private static BigDecimal ZERO = new BigDecimal("0.00");
20    private String acctNo;
21    private boolean closed;
22    private BigDecimal balance = null;
23    private Vector transactions = null;
24    private transient BankPersistenceManagerIntf pmi = null;
25
26    public Account(String acctNo){
27        this.acctNo = acctNo;
28        checkPermission("open");
29        Transaction trn = new Transaction(Transaction.OPEN, ZERO, ZERO, "account open");
30        transactions = new Vector();
31        transactions.add(trn);
32        balance = ZERO;
33        closed = false;
34    }
35
36    public void setPersistenceManager(BankPersistenceManagerIntf pmi){
37        this.pmi = pmi;
38    }
39
40    public void deposit(BigDecimal amt) throws Exceptions.AccountClosed{
41        checkPermission("deposit");
42        if (closed)
43            throw new Exceptions.AccountClosed();
44        balance = balance.add(amt);
45        Transaction trn = new Transaction(Transaction.CREDIT, amt, balance, "cash deposit");
46        transactions.add(trn);
47        if (pmi != null)
48            pmi.save();
49    }
50    public void withdraw(BigDecimal amt) throws Exceptions.AccountClosed, Exceptions.InsufficientAmount {
51        checkPermission("withdraw");
52        if (closed)
53            throw new Exceptions.AccountClosed();
54        if (balance.compareTo(amt) <= 0)
55            throw new Exceptions.InsufficientAmount();
56        balance = balance.subtract(amt);
57        Transaction trn = new Transaction(Transaction.DEBIT, amt, balance, "cash withdrawal");
58        transactions.add(trn);
59        if (pmi != null)
60            pmi.save();
61    }
62
63    public void close() throws Exceptions.AccountClosed {
64        checkPermission("close");
65        if (closed)
66            throw new Exceptions.AccountClosed();
67        BigDecimal amt = new BigDecimal("0.00");
68        Transaction trn = new Transaction(Transaction.CLOSE, amt, balance, "account close");
69        transactions.add(trn);
70        closed = true;
71    }
72    public BigDecimal getBalance() throws Exceptions.AccountClosed{
73        checkPermission("read");
74        if (closed)
75            throw new Exceptions.AccountClosed();
76        return balance;
77    }
78    public String getAcctNo(){
79        checkPermission("read");
80        return acctNo;
81    }
82    public String getStatement(){
83        checkPermission("read");
84        String status = (closed ? "CLOSED" : "OPEN");
85        StringBuffer sb = new StringBuffer();
86        sb.append("----------------- BEGIN BANK STATEMENT -----------------\n");
87        sb.append("Statement Date : " + new Date() + "\n");
88        sb.append("Account#       : " + acctNo + "\n");
89        sb.append("Account Status : " + status + "\n");
90        sb.append("Transactions   :\n");
91        for (int i = 0; i < transactions.size(); i++){
92            sb.append(transactions.elementAt(i) + "\n");
93        }
94        sb.append("------------------ END BANK STATEMENT ------------------\n");
95        return sb.toString();
96    }
97    private void checkPermission(String action){
98        SecurityManager security = System.getSecurityManager();
99        if (security != null) {
00            AccountPermission ap = new AccountPermission(acctNo, action);
01            AccessController.checkPermission(ap);
02        }
03    }
04
05    public String toString(){
06        String status = (closed ? "CLOSED" : "OPEN");
07        return "Account#: " + acctNo + ", Balance: " + balance + ", Status: " + status;
08    }
09}