3 // Author: Danish Lakhani
5 // BankAppServer - Bank Application Server that services one client at a time
13 static int SUCCESS = 0;
16 static int LOGIN_ACCOUNT = 1;
17 static int LOGIN_PIN = 2;
19 static int ACCOUNT_SAVINGS = 1;
20 static int ACCOUNT_CHECKING = 2;
21 static int ACCOUNT_TELLER = 3;
23 private ServerSocket serverSocket = null;
24 private Socket clientSocket = null;
25 private PrintWriter out = null;
26 private BufferedReader in = null;
27 private static int serverPort = 44444;
28 private AccountDatabase accounts = null;
30 private boolean isLoggedIn = false;
31 private Integer activeAccount = 0;
32 private Integer tellerCode = 0;
34 public BankAppServer()
36 // initializeServer();
39 private void initializeServer()
42 accounts = new AccountDatabase();
44 //Initialize Server Socket
45 System.out.print("Creating Server Socket...");
47 serverSocket = new ServerSocket(serverPort);
48 } catch (IOException e) {
49 System.out.println("Cannot listen on port " + serverPort);
52 System.out.println("Done");
55 private void establishConnection()
58 System.out.print("Waiting for connection...");
60 clientSocket = serverSocket.accept();
61 } catch (IOException e) {
62 System.out.println("Accept failed");
65 out = new PrintWriter(clientSocket.getOutputStream(), true);
66 in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
67 System.out.println("Connection Established!");
70 private int authenticateUser(Integer accountNumber, Integer pin)
72 if (!accounts.accountExists(accountNumber))
74 if (accounts.getPin(accountNumber) != pin)
83 Integer accountNumber = new Integer(in.readLine());
84 System.out.println("Account number: " + accountNumber);
85 Integer pin = new Integer(in.readLine());
86 System.out.println("PIN: " + pin);
87 System.out.println("Authenticating...");
88 int status = authenticateUser(accountNumber, pin);
89 if (status == SUCCESS)
91 out.println(accountNumber);
95 if (accounts.isTeller(accountNumber))
96 tellerCode = accountNumber;
98 activeAccount = accountNumber;
99 System.out.println("Logged Success");
103 if (status == LOGIN_ACCOUNT)
104 out.println("ERROR: login failed: Account " + accountNumber + " does not exist.");
106 out.println("ERROR: login failed: Incorrect pin.");
108 System.out.println("Login Failed");
112 private void closeConnection()
117 clientSocket.close();
118 // serverSocket.close();
121 private void processDeposit(Integer accountNumber) throws IOException
124 if (!accounts.accountExists(accountNumber))
126 out.println("ERROR: Account " + accountNumber + " not found.");
133 inVal = in.readLine();
134 Double depAmount = new Double(inVal);
137 out.println("ERROR: Negative or zero deposit amount");
141 accounts.deposit(accountNumber, depAmount);
143 out.println("$" + depAmount + " deposited successfully! Account: " + accountNumber + " New Balance: " + accounts.getBalance(accountNumber));
147 private void processWithdrawal(Integer accountNumber) throws IOException
150 if (!accounts.accountExists(accountNumber))
152 out.println("ERROR: Account " + accountNumber + " not found.");
158 //Get withdrawal amount
159 inVal = in.readLine();
160 Double wdAmount = new Double(inVal);
163 out.println("ERROR: Negative or zero withdrawal amount");
166 // else if (wdAmount > accounts.getBalance(accountNumber))
168 // out.println("ERROR: Insufficient funds. Balance = " + accounts.getBalance(accountNumber));
172 accounts.withdraw(accountNumber, wdAmount);
174 out.println("$" + wdAmount + " withdrawn successfully! Account: " + accountNumber + " New Balance: " + accounts.getBalance(accountNumber));
178 private void processBalanceCheck(Integer accountNumber)
181 out.println("Account: " + accountNumber + " Balance: " + accounts.getBalance(accountNumber));
185 private void startServer()
189 int status = SUCCESS;
192 while (serverQuit == 0)
194 establishConnection();
196 accounts.loadDatabase();
197 while (isConnected == 1)
199 System.out.println("Waiting for request...");
201 String request = in.readLine();
205 System.out.println("Request: " + request);
208 if (request.equals("EXIT"))
210 accounts.storeDatabase();
219 if (request.equals("LOGIN"))
223 out.println("ERROR: Already logged in. Please logout.");
236 out.println("ERROR: Not logged in");
240 if (request.equals("LOGOUT"))
244 out.println(activeAccount);
246 out.println(tellerCode);
247 accounts.storeDatabase();
251 // closeConnection();
255 if (request.equals("DEPOSIT"))
257 processDeposit(activeAccount);
260 if (request.equals("WITHDRAW"))
262 processWithdrawal(activeAccount);
265 if (request.equals("CHECK"))
267 processBalanceCheck(activeAccount);
270 if (request.equals("TELLERDEPOSIT"))
274 out.println("ERROR: Teller not logged in");
278 Integer acc = new Integer(in.readLine());
282 if (request.equals("TELLERWITHDRAW"))
286 out.println("ERROR: Teller not logged in");
290 Integer acc = new Integer(in.readLine());
291 processWithdrawal(acc);
294 if (request.equals("TELLERCHECK"))
298 out.println("ERROR: Teller not logged in");
302 Integer acc = new Integer(in.readLine());
303 processBalanceCheck(acc);
306 if (request.equals("TELLEROPEN"))
310 out.println("ERROR: Teller not logged in");
314 Integer accNum = new Integer(in.readLine());
315 String fName = in.readLine();
316 String mName = in.readLine();
317 String lName = in.readLine();
318 Integer accType = new Integer(in.readLine());
319 Double bal = new Double(in.readLine());
320 Integer pNum = new Integer(in.readLine());
321 status = accounts.openAccount(accNum, fName, mName, lName, accType, bal, pNum);
324 out.println("ERROR: Account " + accNum + " already exists.");
328 out.println("Account Number: " + accNum + " " +
329 "Customer Name: " + fName + " " + mName + " " + lName + " " +
330 "Account Type: " + ((accType == ACCOUNT_SAVINGS)?"SAVINGS":(accType == ACCOUNT_CHECKING)?"CHECKING":"TELLER") + " " +
331 "Balance: $" + bal + " " +
332 "PIN: " + pNum + " ");
335 if (request.equals("TELLERCLOSE"))
339 out.println("ERROR: Teller not logged in");
343 Integer accNum = new Integer(in.readLine());
344 status = accounts.closeAccount(accNum);
347 out.println("ERROR: Account " + accNum + " does not exist.");
351 out.println("Account " + accNum + " closed successfully");
354 if (request.equals("TELLERMODIFY"))
358 out.println("ERROR: Teller not logged in");
362 Integer accNum = new Integer(in.readLine());
363 if (!accounts.accountExists(accNum))
365 out.println("ERROR: Account " + accNum + " does not exist.");
370 while (!(inVal = in.readLine()).equals("DONE"))
372 if (inVal.equals("CHANGENAME"))
374 String fName = in.readLine();
375 String mName = in.readLine();
376 String lName = in.readLine();
377 accounts.modifyName(accNum, fName, mName, lName);
380 else if (inVal.equals("CHANGETYPE"))
382 Integer newType = new Integer(in.readLine());
383 if (newType.intValue() < 1 || newType.intValue() > 3)
385 out.println("ERROR: Invalid account type: " + newType + ". Must be 1-3.");
388 accounts.modifyType(accNum, newType);
391 else if (inVal.equals("CHANGEPIN"))
393 Integer newPin = new Integer(in.readLine());
394 if ((newPin < 0) || (newPin > 9999))
396 out.println("ERROR: Invalid pin " + newPin + ". Must be 0000-9999.");
399 accounts.modifyPin(accNum, newPin);
402 else if (inVal.equals("CHANGEBALANCE"))
404 Double newBal = new Double(in.readLine());
405 accounts.modifyBalance(accNum, newBal);
409 out.println("Account Number: " + accNum + " " +
410 "Customer Name: " + accounts.nameString(accNum) + " " +
411 "Account Type: " + accounts.typeString(accNum) + " " +
412 "Balance: $" + accounts.getBalance(accNum) + " " +
413 "PIN: " + accounts.getPin(accNum) + " ");
416 if (request.equals("TELLERVIEW"))
420 out.println("ERROR: Teller not logged in");
424 Integer accNum = new Integer(in.readLine());
425 if (!accounts.accountExists(accNum))
427 out.println("ERROR: Account " + accNum + " does not exist.");
431 out.println("Account Number: " + accNum + " " +
432 "Customer Name: " + accounts.nameString(accNum) + " " +
433 "Account Type: " + accounts.typeString(accNum) + " " +
434 "Balance: $" + accounts.getBalance(accNum) + " " +
435 "PIN: " + accounts.getPin(accNum) + " ");
441 public static void main(String [] args)
444 System.out.println("BankAppServer in Java");
445 BankAppServer server = new BankAppServer();
446 server.startServer();
452 Integer accountNumber;
460 public AccountEntry(Integer accNum, String fName, String mName, String lName, Integer accType, Double bal, Integer pNum)
462 accountNumber = accNum;
466 accountType = accType;
473 class AccountDatabase
475 static int ACCOUNT_SAVINGS = 1;
476 static int ACCOUNT_CHECKING = 2;
477 static int ACCOUNT_TELLER = 3;
478 static int SUCCESS = 0;
479 static int ERROR = 1;
481 static String dbfilename = "accts.txt";
483 Vector<AccountEntry> entries = null;
485 public AccountDatabase()
487 entries = new Vector<AccountEntry>();
490 public void loadDatabase()
492 entries.removeAllElements();
494 BufferedReader fin = new BufferedReader(new FileReader(dbfilename));
496 while ((str = fin.readLine()) != null)
498 Integer accNum = new Integer(str);
499 String fName = fin.readLine();
500 String mName = fin.readLine();
501 String lName = fin.readLine();
502 Integer accType = new Integer(fin.readLine());
503 Double bal = new Double(fin.readLine());
504 Integer pNum = new Integer(fin.readLine());
505 AccountEntry newEntry = new AccountEntry(accNum, fName, mName, lName, accType, bal, pNum);
506 entries.add(newEntry);
509 } catch (IOException e) {
510 System.out.println("Cannot open database file");
516 public void storeDatabase()
519 BufferedWriter fout = new BufferedWriter(new FileWriter(dbfilename));
520 for (int i = 0; i < entries.size(); i++)
522 AccountEntry acc = (AccountEntry)entries.elementAt(i);
523 fout.write(acc.accountNumber.toString());
525 fout.write(acc.firstName);
527 fout.write(acc.middleName);
529 fout.write(acc.lastName);
531 fout.write(acc.accountType.toString());
533 fout.write(acc.balance.toString());
535 fout.write(acc.pin.toString());
539 } catch (IOException e) {
540 System.out.println("Cannot write to database file");
545 public AccountEntry getAccount(Integer accNum)
547 for (int i = 0; i < entries.size(); i++)
549 AccountEntry acc = (AccountEntry)entries.elementAt(i);
550 if (acc.accountNumber.equals(accNum))
556 public void deposit(Integer accNum, Double amount)
558 AccountEntry acc = getAccount(accNum);
559 acc.balance += amount;
562 public void withdraw(Integer accNum, Double amount)
564 AccountEntry acc = getAccount(accNum);
565 acc.balance -= amount;
568 public Double getBalance(Integer accNum)
570 AccountEntry acc = getAccount(accNum);
574 public int getPin(Integer accNum)
576 AccountEntry acc = getAccount(accNum);
578 return acc.pin.intValue();
582 public boolean accountExists(Integer accNum)
584 AccountEntry acc = getAccount(accNum);
590 public boolean isTeller(Integer accNum)
592 AccountEntry acc = getAccount(accNum);
593 if (acc.accountType.equals(ACCOUNT_TELLER))
598 public Integer openAccount(Integer accNum, String fName, String mName, String lName, Integer accType, Double bal, Integer pNum)
600 if (accountExists(accNum))
602 AccountEntry acc = new AccountEntry(accNum, fName, mName, lName, accType, bal, pNum);
607 public Integer closeAccount(Integer accNum)
609 if (accountExists(accNum))
611 AccountEntry acc = getAccount(accNum);
619 public String nameString(Integer accNum)
621 AccountEntry acc = getAccount(accNum);
624 return (acc.firstName + " " + acc.middleName + " " + acc.lastName);
629 public String typeString(Integer accNum)
631 AccountEntry acc = getAccount(accNum);
634 return ((acc.accountType == ACCOUNT_SAVINGS)?"SAVINGS":(acc.accountType == ACCOUNT_CHECKING)?"CHECKING":"TELLER");
639 public void modifyName(Integer accNum, String fName, String mName, String lName)
641 AccountEntry acc = getAccount(accNum);
644 acc.firstName = fName;
645 acc.middleName = mName;
646 acc.lastName = lName;
651 public void modifyType(Integer accNum, Integer newType)
653 AccountEntry acc = getAccount(accNum);
656 acc.accountType = newType;
661 public void modifyPin(Integer accNum, Integer newPin)
663 AccountEntry acc = getAccount(accNum);
671 public void modifyBalance(Integer accNum, Double newBal)
673 AccountEntry acc = getAccount(accNum);
676 acc.balance = newBal;
681 public void printAccounts()
683 System.out.println("entries.size = " + entries.size());
684 for (int i = 0; i < entries.size(); i++)
686 System.out.println("Entry " + i);
687 AccountEntry acc = entries.elementAt(i);
688 System.out.println("1 " + acc.accountNumber.toString());
689 System.out.println("2 " + acc.firstName);
690 System.out.println("3 " + acc.middleName);
691 System.out.println("4 " + acc.lastName);
692 System.out.println("5 " + acc.accountType.toString());
693 System.out.println("6 " + acc.balance.toString());
694 System.out.println("7 " + acc.pin.toString());