001    package topdown.client;
002    
003    import topdown.client.proxy.BankServicePortClient;
004    
005    /**
006     * A client that interacts with the bank service to demonstrate various interactions.  The client creates a service
007     * endpoint and subsequently uses that instance to invoke service methods.
008     */
009    public class BankingApplication {
010    
011        /** A demo user. */
012        private static String DEMO_USER1 = "DemoUser1";
013    
014        /** A demo user. */
015        private static String DEMO_USER2 = "DemoUser2";
016    
017        /** A demo user. */
018        private static String DEMO_USER3 = "DemoUser3";
019    
020        /** The service endpoint. */
021        private BankServicePortClient m_endpoint;
022    
023        /**
024         * Creates an instance of the bank client.
025         * @param serviceURL    the endpoint URL.
026         * @throws Exception    if any runtime exception is generated during the interactions with the service.
027         */
028        public BankingApplication(String serviceURL) throws Exception{
029            // create the service endpoint and set its URL
030            m_endpoint = new BankServicePortClient();
031            if ( serviceURL != null ) {
032                m_endpoint.setEndpoint( serviceURL );
033            }
034        }
035    
036        /**
037         * Executes a number of demo interactions with the deployed service.
038         * @throws Exception    if any runtime exception is generated during the interactions with the service.
039         */
040        private void runDemo() throws Exception{
041            demoGoodAccount();
042            demoInsufficientFunds();
043            demoWithdrawalCap();
044        }
045    
046        /**
047         * Demonstrates an attempt to withdraw money from a generated account beyond the allowed withdrawl cap.
048         * @throws Exception    if any runtime exception is generated during the interactions with the service.
049         */
050        private void demoWithdrawalCap() throws Exception{
051            String accountID = m_endpoint.createAccount(DEMO_USER3,3000.00f);
052            System.out.println("Created an account for " + DEMO_USER3 + " with $3000.00");
053            System.out.println("Attempting to withdraw 2500.00 from the account.  The account cap is 2000.00");
054            try{
055                m_endpoint.withdraw(accountID,2500.00f);
056            }
057            catch(Exception ex){
058                System.out.println("Unable to withdraw funds.");
059                System.out.println(ex.getMessage());
060            }
061        }
062    
063        /**
064         * Demonstrates an attempt to create an account with no initial funds.
065         * @throws Exception     if any runtime exception is generated during the interactions with the service.
066         */
067        private void demoInsufficientFunds() throws Exception{
068            System.out.println("Attempting to create an account for " + DEMO_USER2 + " with no initial funds");
069            try{
070                m_endpoint.createAccount(DEMO_USER2,0f);
071            }
072            catch(Exception ex){
073                System.out.println("Could not createAccount for " + DEMO_USER2);
074                System.out.println(ex.getMessage());
075            }
076        }
077    
078        /**
079         * Demonstrates successful interactions with the bank service.
080         * @throws Exception    if any runtime exception is generated during the interactions with the service.
081         */
082        private void demoGoodAccount() throws Exception{
083            String accountID = m_endpoint.createAccount(DEMO_USER1,2000.50f);
084            System.out.println("Created an account for " + DEMO_USER1 + " with $2000.50");
085            System.out.println("AccountID for DemoUser is " + accountID);
086            System.out.println("Depositing $500.50 into account.");
087            m_endpoint.deposit(accountID,500.50f);
088            float balance = m_endpoint.getBalance(accountID,DEMO_USER1);
089            System.out.println("Current balance is now " + balance);
090            System.out.println("Withdrawing $250.00 from account");
091            m_endpoint.withdraw(accountID,250.00f);
092            balance = m_endpoint.getBalance(accountID,DEMO_USER1);
093            System.out.println("Current balance is now " + balance);
094        }
095    
096        /**
097         * Runs the demo client.
098         * @param args  the URL for the service endpoint.
099         */
100        public static void main(String args[]){
101            String serviceURL = null;
102            if(args.length > 0){
103                serviceURL = args[0];
104            }
105            try{
106                BankingApplication demo = new BankingApplication(serviceURL);
107                demo.runDemo();
108            }
109            catch(Exception ex){
110                ex.printStackTrace();
111            }
112        }
113    }