import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; /** * Author by flo on 9/20/15. * Project Num : Project 2 * Purpose : To demonstrate the use of Polymorphism and java Inheritance * Algorithm : many of my methods are of O(1), there were a few which were O(n) due to * : the worse case of a list can be unlimited. * Compile : javac Project2Driver.java * : java Project2Driver < data.txt */ public class Project2Driver { // data members to hold bank records private ArrayList sArrList; private ArrayList accountList; // initializing the two data members public Project2Driver() { sArrList = new ArrayList(); accountList = new ArrayList(); } // Read banking data records from a file via redirection public void readFile() { String line; BufferedReader br = null; br = new BufferedReader(new InputStreamReader(System.in)); // read each line from file and split it based on a space try { // Big O(n) Analysis notation while ((line = br.readLine()) != null) { this.sArrList.add(line); } } catch (IOException err) { System.out.println("no line ? " + err.getMessage()); } finally { if (br != null) { try { br.close(); } catch (IOException err) { System.out.println("Lets exit this program, this is not suppose to happen"); System.exit(1); } } } } // Create bank accounts based on different types public void createAccount() { String line; String[] tokens; Double obalance = 0.00; Double cbalance = 0.00; // loop to read columns to create tokens based on spacing // Big O(n) Analysis notation for (int count = 0; count < this.sArrList.size(); count++) { line = sArrList.get(count); tokens = line.split("\\s+"); // taking out open balance try { obalance = Double.parseDouble(tokens[4]); } catch (NumberFormatException err) { System.out.println("Error message is: " + err.getMessage()); } // quick error handling O(1) if (tokens.length != 6) { System.out.print("Wrong number of Columns, Please fix data file."); System.exit(1); } // switch based on bank type token // then create child class objects as an account // Big O(1) Analysis notation switch (tokens[5].toUpperCase()) { case "B": BusinessAccounts ba = new BusinessAccounts(tokens[0],tokens[1],tokens[2], tokens[3], obalance, cbalance ); accountList.add(ba); break; case "C": CheckingAccounts ca = new CheckingAccounts(tokens[0],tokens[1],tokens[2], tokens[3], obalance, cbalance); accountList.add(ca); break; case "S": SavingsAccounts sa = new SavingsAccounts(tokens[0],tokens[1],tokens[2], tokens[3], obalance, cbalance); accountList.add(sa); break; default: // hummm.... not sure break; } } } // gathered bank information to process interest amount public void computeBalance() { // Big O(n) Analysis notation for ( int item=0; item < accountList.size(); item++){ accountList.get(item).computeCloseBalance(); } } // print out header information public void prtHeaderInfo() { // Big O(1) Analysis notation System.out.println("Name\tAccountNumber\tPhone\t\tSSN\t\tOpenBalance\t\tCloseBalance\n"); } // print out monthly balance for each account public void prtMonthBalance() { // Big O(n) Analysis notation for (int item=0; item < accountList.size(); item++) { System.out.println( accountList.get(item).getName() + "\t" + accountList.get(item).getAccNumber() + "\t" + accountList.get(item).getPhone() + "\t" + accountList.get(item).getSSN() + "\t" + accountList.get(item).getOBalance() + "\t\t\t" + accountList.get(item).getCBalance() + "\t" ); } } // main Driver public static void main(String[] args) throws IOException { // calling this class to call to create Account class Project2Driver p2 = new Project2Driver(); p2.readFile(); p2.createAccount(); p2.prtHeaderInfo(); p2.computeBalance(); p2.prtMonthBalance(); } }