import java.io.*; import java.util.Vector; /* Program maintains a collection of bank account objects in the memory. The program accepts some commands to manipulate these accounts The vaid commands are: HELP display the list of commands the program accepts LIST displays a list of accounts with acct_no, owner and balance SHOW nnnn displays the owner and balance of the account "nnnn" OPEN nnnn,name surname opens a new account with owner "name surname" and balance=0 DEPOSIT nnnn,aaaaaaaa deposits amount aaaaaaa to account nnnn DRAW nnnn,aaaaaaa draws amount aaaaaa from account "nnnn". Checks is balance allows the drawal CLOSE nnnn closes the account "nnnn" if balance is zero QUIT quits the program, all account info will be lost */ public class bank2 { public static class account { // PROPERTIES public String number; public String owner; public int balance; // METHODS public int draw(int amount) { balance = balance - amount; return balance; } public int deposit(int amount) { balance = balance + amount; return balance; } // CONSTRUCTOR public account () { balance = 0; owner = ""; number = ""; } } public static void main( String[] args ) throws IOException { BufferedReader input = new BufferedReader (new InputStreamReader (System.in)); Vector accounts_vector = new Vector(); System.out.println("Banking system ready. \nType a command or HELP to see commands"); String command = ""; account temp_acct; while ( ! command.startsWith("QUIT") ) { System.out.print("? "); command = input.readLine(); if (command.startsWith("QUIT")) { System.out.println("\nSuccessful termination"); System.exit(0); } if (command.startsWith("HELP")) { System.out.println("LIST lists all accounts and balances"); System.out.println("QUIT quits the program"); System.out.println("OPEN nnnnn,Owners Name"); System.out.println("CLOSE nnnnn"); System.out.println("DEPOSIT nnnnn,amount"); System.out.println("DRAW nnnnn,amount"); System.out.println("SHOW nnnnn"); } if (command.startsWith("OPEN")) { String [] temp = null; temp = split(command); account a_new_account = new account(); // temp[0] is the "OPEN" command a_new_account.number = temp[1]; a_new_account.owner = temp[2]; accounts_vector.addElement(a_new_account); } if (command.startsWith("DEPOSIT")) { String [] temp = null; temp = split(command); // temp[0] is the "DEPOSIT" command String number = temp[1]; int amount = Integer.parseInt(temp[2]); int index_of_account = findAccount(number, accounts_vector); if (index_of_account == -1 ) { System.out.println ("Account "+number+" not found!"); } else { temp_acct = (account)accounts_vector.elementAt(index_of_account); int new_balance = temp_acct.deposit( amount ); System.out.println ("New balance for Account "+number+" is "+new_balance); accounts_vector.setElementAt(temp_acct, index_of_account); } } if (command.startsWith("LIST")) { int number_of_accounts = accounts_vector.size(); System.out.println("Number of accounts="+number_of_accounts); System.out.println(""); for (int i=0 ; i < number_of_accounts ; i++) { temp_acct = (account)accounts_vector.elementAt( i ); System.out.print("Acct="+temp_acct.number); System.out.print("\t"+temp_acct.owner); System.out.println("\t"+temp_acct.balance); } } } } public static String [] split ( String str) { // Splits a BANK command into parts delimited by comma or spaces // and returns in an array of strings // e.g. : If argument is "OPEN 123,ABC DEF", then // the returned array will be // arr[0] = "OPEN", arr[1] = "123", arr[2] = "ABC DEF" String [] results_array = new String[3]; String temp_str; int array_index = 0; temp_str = str; // find the position of the first space int posit_space = temp_str.indexOf(' '); if (posit_space == -1) { // if no spaces found, return whatever you have results_array[array_index] = temp_str; return (results_array); } results_array[array_index] = temp_str.substring(0, posit_space-1); array_index++; temp_str = temp_str.substring(posit_space+1); // find the position of the comma int posit_comma = temp_str.indexOf(','); if (posit_comma == -1) { results_array[array_index] = temp_str; return (results_array); } results_array[array_index] = temp_str.substring(0, posit_comma); array_index++; // get whatever is left over temp_str = temp_str.substring(posit_comma+1); results_array[array_index] = temp_str; return (results_array); } public static int findAccount ( String acct_no, Vector accounts_vector) { account temp_acct; int n = accounts_vector.size(); int i; String acct_id; for ( i=0 ; i < n ; i++) { temp_acct = (account)accounts_vector.elementAt( i ); acct_id = temp_acct.number; if ( acct_id.equals(acct_no)) { // Account found, return its index in the vector return i; } } // Account not found return(-1); } }