import java.util.ArrayList; import java.util.Iterator; import java.util.ListIterator; import java.text.DecimalFormat; /** * John Sloan * Febuary 1, 2009 */ public class Bank { // instance variables - replace the example below with your own private ArrayList differentAccounts; private account currentAccount; private int NextAccountNumber; private DecimalFormat fB; private double interestRate; /** * constructor */ public Bank() { initFormatting(); differentAccounts = new ArrayList(); interestRate = 0.05; NextAccountNumber = 1001; } /** * Method creates a new checking account */ public void newCheckingAccount(String accountHoldersName, double openingBalance) { checking temp = null; if(openingBalance>=0) { temp = new checking(accountHoldersName, NextAccountNumber, openingBalance); differentAccounts.add(temp); NextAccountNumber++; } else{ System.out.println("Error: The balance must be greater than or equal to zerro to open an account"); } } /** * Method creates a new savings account */ public void newSavingsAccount(String accountHoldersName, double openingBalance) { savings temp = null; if( openingBalance >= 0 ) { temp = new savings(accountHoldersName, NextAccountNumber, openingBalance, interestRate); differentAccounts.add(temp); NextAccountNumber++; } else{ System.out.println("Error: The balance must be greater than or equal to zerro to open an account"); } } /** * Method lists accounts */ public void listAccounts() { account temp; for(int i = 0; i < differentAccounts.size(); i++) { temp = differentAccounts.get(i); temp.print(); } } /** * Tells the formatter to always use 3 digits */ public void initFormatting() { fB = new DecimalFormat(); fB.applyPattern( "##0.00%;-##0.00%"); } /** * */ public void getInterest() { initFormatting(); //System.out.println("Your current interest rate is " + currentInterestRate); } /** * Need instruction for type casting. See the top of page 5 of the lab. */ public void setInterestRate(double currentInterestRate) { if(0 <= currentInterestRate && currentInterestRate <= 1) { System.out.print(currentInterestRate); } else { System.out.println("Error: Interest rate is out of bounds"); } } /** * Method Search By Name */ public void searchByName(String accountHoldersNameSearch) { boolean found = false; Iterator it = differentAccounts.iterator(); while(!found && it.hasNext()) { currentAccount = (account) it.next(); if(currentAccount.getName() == accountHoldersNameSearch) { found = true; } } if(found) { printCurrentAccount(); } else { } } /** * Method Search By Number */ public void searchByNumber(int accountNumberSearch) { boolean found = false; Iterator it = differentAccounts.iterator(); while(!found && it.hasNext()) { currentAccount = (account) it.next(); if(currentAccount.getNumber() == accountNumberSearch && currentAccount != null) { found = true; } } if(found) { printCurrentAccount(); } else { System.out.println("Account with number " + accountNumberSearch + " not found"); } } /** * Method print current account */ public void printCurrentAccount() { if(currentAccount == null){ System.out.println("Error: No account is currently selected to be printed"); } if(currentAccount != null) { System.out.println("**Current Account**"); currentAccount.print(); } } /** * Method Print Current Name */ public void printCurrentName() { if(currentAccount != null) { //prints name from the account class?? System.out.println(currentAccount.getName()); } else{ System.out.println("Error: Name not found"); } } /** * Method Print Current Number */ public void printCurrentNumber() { if(currentAccount != null) { //prints name from the account class?? System.out.println( currentAccount.getNumber() ); } else { System.out.println("Error: Number not found"); } } /** * Method makeDeposit */ public void makeDeposit(double depositAmount) { if(currentAccount != null) { currentAccount.deposit(depositAmount); printCurrentAccount(); } else { System.out.println("No account is currently selected"); } } /** * Method Make WithDrawl */ public void makeWithdrawl(double withDrawlAmount) { if(currentAccount != null) { currentAccount.withdrawl(withDrawlAmount); printCurrentAccount(); } if(currentAccount == null) // else { System.out.println("No account is currently selected"); } } /** * Method updateInterest */ public void updateInterest() { savings temp = null; for( account act : differentAccounts ) { if( act.type == 1) { temp = (savings) act; temp.addInterest(); } } } /** * Method deleteCurrentAccount */ public void deleteCurrentAccount() { if(currentAccount == null) { System.out.println("Error: there is no current account"); } if(currentAccount != null) { differentAccounts.remove(currentAccount);//where is the remove method?? currentAccount = null; } } /** * main method */ public static void main(String [] arg) { Bank bank = new Bank (); System.out.println( "Creating new accounts" ); bank.newCheckingAccount("Account One", 1000); bank.newSavingsAccount("Account Two", 2000); bank.newCheckingAccount("Account Three", 3000); bank.newSavingsAccount("Account Four", 4000); System.out.println( "\nListing the accounts" ); bank.listAccounts(); System.out.println( "\nSearching for account two by name" ); bank.searchByName("Account Two"); System.out.println( "\nDepositing $500 to current account" ); bank.makeDeposit(500); System.out.println( "\nSearch for account #1003" ); bank.searchByNumber(1003); System.out.println( "\nWithdrawing $2000 from current account" ); bank.makeWithdrawl(2000); System.out.println( "\nListing the accounts" ); bank.listAccounts(); System.out.println( "\nUpdating the interest" ); bank.updateInterest(); System.out.println( "\nListing the accounts" ); bank.listAccounts(); System.out.println( "\nSearching for account one by name" ); bank.searchByName("Account One"); System.out.println( "\nDeleting the current account" ); bank.deleteCurrentAccount(); System.out.println( "\nListing the accounts" ); bank.listAccounts(); } }