1
10package org.jstk.example.bank.client;
11
12import java.io.InputStreamReader;
13import java.io.BufferedReader;
14import java.math.BigDecimal;
15import java.util.Iterator;
16import org.jstk.*;
17import org.jstk.example.bank.*;
18
19public class BankClient extends JSTKAbstractTool {
20 private static BankIntf bank;
21 private static AccountIntf curAcct = null;
22
23 static class OpenCommand extends JSTKCommandAdapter {
24 public Object execute(JSTKArgs args) throws JSTKException {
25 if (args.getNum() < 1)
26 return new JSTKResult(null, false, "No Initial Deposit. Open failed.");
27 BigDecimal amt = null;
28 try {
29 amt = new BigDecimal(args.get(0));
30 } catch (NumberFormatException nfe){
31 return new JSTKResult(null, false, "Invalid Amount");
32 }
33 try {
34 curAcct = bank.openAccount(amt);
35 } catch (Exception e){
36 throw new JSTKException("openAccount() failed.", e);
37 }
38 return new JSTKResult(null, true, "Account Opened: " + curAcct.getAcctNo());
39 }
40 }
41 static class GetCommand extends JSTKCommandAdapter {
42 public Object execute(JSTKArgs args) throws JSTKException {
43 if (args.getNum() < 1)
44 return new JSTKResult(null, false, "No Account No. Get failed.");
45 try {
46 curAcct = bank.getAccount(args.get(0));
47 } catch (Exceptions.AccountNotFound anf){
48 return new JSTKResult(null, false, "Not Found. Get failed.");
49 }
50 return new JSTKResult(null, true, "Current Account: " + curAcct.getAcctNo());
51 }
52 }
53 static class CurrentCommand extends JSTKCommandAdapter {
54 public Object execute(JSTKArgs args) throws JSTKException {
55 if (curAcct == null)
56 return new JSTKResult(null, false, "No Current Account");
57 return new JSTKResult(null, true, "Current Account: " + curAcct.getAcctNo());
58 }
59 }
60 static class ListCommand extends JSTKCommandAdapter {
61 public Object execute(JSTKArgs args) throws JSTKException {
62 Iterator itr = bank.accounts();
63 StringBuffer sb = new StringBuffer();
64 int idx = 0;
65 while (itr.hasNext()){
66 AccountIntf acct = (AccountIntf)itr.next();
67 sb.append("Account[" + idx + "]: " + acct + "\n");
68 ++idx;
69 }
70 return new JSTKResult(null, true, sb.toString());
71 }
72 }
73 static class DepositCommand extends JSTKCommandAdapter {
74 public Object execute(JSTKArgs args) throws JSTKException {
75 if (curAcct == null)
76 return new JSTKResult(null, false, "No Current Account");
77 if (args.getNum() < 1)
78 return new JSTKResult(null, false, "No Amount Specified");
79 BigDecimal amt = null;
80 try {
81 amt = new BigDecimal(args.get(0));
82 } catch (NumberFormatException nfe){
83 return new JSTKResult(null, false, "Invalid Amount");
84 }
85 try {
86 curAcct.deposit(amt);
87 } catch (Exceptions.AccountClosed ac){
88 return new JSTKResult(null, false, "Account Closed");
89 }
90 return new JSTKResult(null, true, "Deposited: " + amt);
91 }
92 }
93 static class WithdrawCommand extends JSTKCommandAdapter {
94 public Object execute(JSTKArgs args) throws JSTKException {
95 if (curAcct == null)
96 return new JSTKResult(null, false, "No Current Account");
97 if (args.getNum() < 1)
98 return new JSTKResult(null, false, "No Amount Specified");
99 BigDecimal amt = null;
00 try {
01 amt = new BigDecimal(args.get(0));
02 } catch (NumberFormatException nfe){
03 return new JSTKResult(null, false, "Invalid Amount");
04 }
05 try {
06 curAcct.withdraw(amt);
07 } catch (Exceptions.AccountClosed ac){
08 return new JSTKResult(null, false, "Account Closed");
09 } catch (Exceptions.InsufficientAmount ac){
10 return new JSTKResult(null, false, "Not Enough Funds");
11 }
12 return new JSTKResult(null, true, "Withdrawn: " + amt);
13 }
14 }
15 static class BalanceCommand extends JSTKCommandAdapter {
16 public Object execute(JSTKArgs args) throws JSTKException {
17 if (curAcct == null)
18 return new JSTKResult(null, false, "No Current Account");
19 BigDecimal balance = null;
20 try {
21 balance = curAcct.getBalance();
22 } catch (Exceptions.AccountClosed ac){
23 return new JSTKResult(null, false, "Account Closed");
24 }
25 return new JSTKResult(null, true, "Current Balance: " + balance);
26 }
27 }
28 static class StatementCommand extends JSTKCommandAdapter {
29 public Object execute(JSTKArgs args) throws JSTKException {
30 if (curAcct == null)
31 return new JSTKResult(null, false, "No Current Account");
32 return new JSTKResult(null, true, curAcct.getStatement());
33 }
34 }
35 static class QuitCommand extends JSTKCommandAdapter {
36 public Object execute(JSTKArgs args) throws JSTKException {
37 System.exit(0);
38 return null;
39 }
40 }
41
42 static {
43 cmds.put("open", new OpenCommand());
44 cmds.put("get", new GetCommand());
45 cmds.put("current", new CurrentCommand());
46 cmds.put("list", new ListCommand());
47 cmds.put("deposit", new DepositCommand());
48 cmds.put("withdraw", new WithdrawCommand());
49 cmds.put("balance", new BalanceCommand());
50 cmds.put("statement", new StatementCommand());
51 cmds.put("quit", new QuitCommand());
52 cmds.put("exit", new QuitCommand());
53 }
54
55 public BankClient(){
56 super();
57
58 }
59
60 public String progName(){
61 String progName = "java org.jstk.example.bank.BankClient";
62 return progName;
63 }
64 public String briefDescription(){
65 return "Client program for bank example";
66 }
67
68 public void init(BankIntf bi){
69 this.bank = bi;
70 }
71
72 public String execCommand(String[] args) throws Exception {
73 JSTKOptions opts = new JSTKOptions();
74 if (args.length < 1){ return usageString();
76 }
77 String cmdString = args[0];
78 if (cmdString.equals("-h") || cmdString.equals("help") || cmdString.equals("-?")){
79 return usageString();
80 }
81
82 JSTKCommand cmd = (JSTKCommand)cmds.get(cmdString);
83 if (cmd == null){ System.out.println("Unknown Command: " + cmdString);
85 return usageString();
86 }
87
88 if (args.length > 1 && (args[1].equals("-h") || args[1].equals("help") || args[1].equals("-?"))){
89 return cmdUsageString(cmd, cmdString);
90 }
91
92 opts.parse(args, 1);
93
94 try {
95 JSTKResult result = (JSTKResult)cmd.execute(opts);
96 return result.getText();
97 } catch (java.security.AccessControlException ace){
98 return "Access denied";
99 }
00 }
01}