001    package topdown.service;
002    
003    import java.rmi.RemoteException;
004    import java.util.Iterator;
005    import java.util.List;
006    
007    /**
008     * The is the bank service implementation class.  It implements the remote service interface and creates the underlying
009     * bank class this service makes available to remote users.
010     */
011    public class BankServiceImpl implements BankService{
012    
013        /** The underlying bank instance.  */
014        private Bank m_bank;
015    
016        /**
017         * Creates a bank service instance that wraps a bank object.
018         */
019        public BankServiceImpl(){
020            m_bank = BankFactory.createBank();
021        }
022    
023        /**
024         * Creates a bank account.  This class simply delegates the call to the underlying bank instance.
025         * @param acctName  the name of the account owner.
026         * @param initBalance   the initial balance for the account.
027         * @return  the account ID.
028         * @throws RemoteException  if an unexpected system error occurs.
029         * @throws AccountException if the initial balance is insufficient (<= 0).
030         */
031        public String createAccount(String acctName,float initBalance) throws
032                RemoteException,AccountException {
033            return m_bank.addNewAccount(acctName,initBalance);
034        }
035    
036        /**
037         * Deposits money into the given account.
038         * @param acctID    the ID of the account into which the deposit is made.
039         * @param amount    the amount of money to deposit.
040         * @throws RemoteException  if an unexpected system error occurs.
041         * @throws AccountException if no account is found for the given account ID.
042         */
043        public void deposit(String acctID, float amount) throws
044                RemoteException, AccountException {
045            Account theAccount = m_bank.getAccount(acctID);
046            if(theAccount == null){
047                throw new AccountException("No account found for " + acctID);
048            }
049            theAccount.deposit(amount);
050        }
051    
052        /**
053         * Withdraws money from the given account.
054         * @param acctID    the account ID for the account from which the money will be withdrawn.
055         * @param amount    the amount of money to withdraw.
056         * @throws RemoteException  if an unexpected system error occurs.
057         * @throws AccountException if the amount of money withdrawn exceeds the maximum amount allowed ($2000.00).
058         */
059        public void withdraw(String acctID, float amount) throws
060                RemoteException, AccountException {
061            Account theAccount = m_bank.getAccount(acctID);
062            if(theAccount == null){
063                throw new AccountException("No account found for " + acctID);
064            }
065            theAccount.withdraw(amount);
066        }
067    
068        /**
069         * Returns the current balance of the account.
070         * @param acctID    the account ID of the account.
071         * @param acctName  the name of the account owner.
072         * @return  the balance of the account.
073         * @throws RemoteException  if an unexpected system error occurs.
074         * @throws AccountException if no account is found for the given account ID.
075         */
076        public float getBalance(String acctID, String acctName) throws
077                RemoteException, AccountException {
078            Account theAccount = m_bank.getAccount(acctID);
079            if(theAccount == null){
080                throw new AccountException("No account found for " + acctID);
081            }
082            return theAccount.getBalance();
083        }
084    
085        /**
086         * Returns the account ID associated with the provided account owner's name.
087         * @param acctName  the name of the account owner.
088         * @return  the account ID.
089         * @throws RemoteException  if an unexpected system error occurs.
090         * @throws AccountException if no account is found for the provided owner name.
091         */
092        public String getAccountID(String acctName) throws
093                RemoteException, AccountException {
094            List acctList = m_bank.getAccounts();
095            Iterator it = acctList.iterator();
096            Account theAccount = null;
097            while(it.hasNext()){
098                Account curAccount = (Account)it.next();
099                if(curAccount.getAccountName().equals(acctName)){
100                    theAccount = curAccount;
101                    break;
102                }
103            }
104            if(theAccount == null){
105                throw new AccountException("No acct id found for " + acctName);
106             }
107            return theAccount.getAccountID();
108        }
109    
110    
111    }