// file: Bank.java // author: Robert Keller // purpose: A Java program, defining a bank, using BankAccount class import java.io.*; /** * Bank defines one bank. **/ class Bank { int max_accounts; // maximum number of accounts BankAccount account[]; // declare array of bank accounts int num_accounts; // current number of accounts /** * Constructor, open a bank with a maximum number of accounts **/ public Bank(int max_accounts) { this.max_accounts = max_accounts; num_accounts = 0; account = new BankAccount[max_accounts]; // allocate array } // (not accounts themselves) /** * Open a new account at this bank **/ public boolean open(String name, String ss_no, int balance) { if( num_accounts == max_accounts ) return false; // reached maximum number account[num_accounts++] = new BankAccount(name, ss_no, balance); return true; } /** * Issue statements for all accounts. **/ public void issue_statements() { for( int i = 0; i < num_accounts; i++ ) account[i].statement(); } /** * Conduct business of the bank. * Read input specifying accounts to be opened. * Print statements at the end. **/ public void conduct_business(InputStream In) { StreamTokenizer input = new StreamTokenizer(In); // create tokenizer int TT_EOF = StreamTokenizer.TT_EOF; try { while( input.nextToken() != TT_EOF ) // get the name { String name = input.sval; // assign the name if( input.nextToken() == TT_EOF ) break; // get SS number String ss_no = input.sval; if( input.nextToken() == TT_EOF ) break; // get the balance if( input.ttype != StreamTokenizer.TT_NUMBER ) { System.out.println("Invalid balance for " + name + " ignored: " + input.sval); continue; // can't read balance } else { int balance = (int)input.nval; // make the account if( !open(name, ss_no, balance) ) System.out.println("Not accepting new accounts"); // can't open } } } catch(IOException e) { System.out.println("Unexpected IO exception"); } issue_statements(); // issue statements } /** * Sample program defining one bank with 10 accounts max. **/ static public void main(String args[]) { Bank test_bank = new Bank(10); test_bank.conduct_business(System.in); } }