/** * file : BankApp.java * desc : A bank application. Allows a user to * create and manipulate banking accounts. */ import java.io.*; public class BankApp { static private VirtualTeller ATM = new VirtualTeller(); public static void main( String[] args ) { greeting(); usage(); String command = readLine( "--> "); while( !command.equals( "quit" ) ) { try { if( command.equals( "open" ) ) { open(); } else if( command.equals( "deposit" ) ) { doDeposit(); } else if( command.equals( "withdraw" ) ) { doWithdraw(); } else if( command.equals( "inquire" ) ) { doInquire(); } else { System.err.println( "Invalid command: " + command ); usage(); } } catch( Exception e ) { System.err.println( e ); } command = readLine( "\n--> " ); } } /** * post : prompts the user to create an account and creates * one in the ATM */ private static void open() throws IOException { // Get name String name = readLine( "Enter name: " ); int newNum = ATM.openAccount( name ); System.out.println( name + ", your new account number is: " + newNum ); System.out.println( "Thanks for opening an account with us!" ); } /** * post : prompts the user for an account number and tries * to perform a deposit transaction on that account */ private static void doDeposit() throws IOException { // Get account number int acctNumber = readInt( "Enter account number: " ); int amount = readInt( "Enter amount to deposit: " ); ATM.deposit( acctNumber, amount ); System.out.println( "New balance for #" + acctNumber + " is " + ATM.balanceInquiry( acctNumber ) ); } /** * post : prompts the user for an account number and tries * to perform a withdrawl transaction from that account */ private static void doWithdraw() throws IOException { // Get account number int acctNumber = readInt( "Enter account number: " ); int amount = readInt( "Enter amount to withdraw: " ); ATM.withdraw( acctNumber, amount ); System.out.println( "New balance for #" + acctNumber + " is " + ATM.balanceInquiry( acctNumber ) ); } /** * post : prompts the user for an account number and tries * to get that account's balance */ private static void doInquire() throws IOException { // Get account number int acctNumber = readInt( "Enter account number: " ); System.out.println( "Balance for #" + acctNumber + " is " + ATM.balanceInquiry( acctNumber ) ); } /** * post : displays a greeting message */ private static void greeting() { System.out.println( "-------------------" ); System.out.println( "Welcome to the bank" ); System.out.println( "-------------------" ); System.out.println( "" ); } /** * post : displays instructions on using the command * line arguments */ private static void usage() { System.out.println( "Valid commands are: " + "open, deposit, withdraw, inquire, quit" ); } // some clumsy I/O stuff private static DataInputStream dis = new DataInputStream( System.in ); /** * post : return a string from the input stream */ private static String readLine( String prompt ) throws IOException { System.out.print( prompt ); return dis.readLine(); } /** * post : return an integer from the input stream */ private static int readInt( String prompt ) throws IOException { String text = readLine( prompt ); return Integer.valueOf(text).intValue(); } } // eof