Main { (@ static public void main(String args[]) throws Exception { BrokerSim b = new BrokerSim(); // print welcome message b.set_marketList(MarketList.parse(new FileInputStream("../../Broker.mar"))); b.set_accountList(AccountList.parse(new FileInputStream("../../Broker.acc"))); b.set_operationList(OperationList.parse(new FileInputStream("../../Broker.op"))); b.run(); } @) } BrokerSim { (@ void run() throws Exception { Account a = new Account(); Username u = new Username(); Password p = new Password(); Password pv = new Password(); Cash c = new Cash(); BufferedReader inputLine; String input = new String(); boolean operational = true; boolean flag = true; a = null; while(operational) { while (a == null) { System.out.println("\n\n------------------------------"); System.out.println("Log On to the Brokerage System"); System.out.println("(type exit to end simulation)"); System.out.println("------------------------------\n"); System.out.print("Username : "); inputLine = new BufferedReader (new InputStreamReader(System.in)); input = inputLine.readLine(); u = new Username(input); if (input.equals("exit")) { System.out.println("see ya"); exit_Broker(); operational = false; break; } a = check_Username(u); if (a != null) { System.out.print("Password : "); inputLine = new BufferedReader (new InputStreamReader(System.in)); input = inputLine.readLine(); if (input.equals(a.get_password().get_pword())) { loggedOn(a); a = null; } else { System.out.println("\nInvalid Password"); a = null; } } else { System.out.println("\nThat Username is not in our database"); System.out.print("\nWould you like to create a new Account (y/n)? "); input = inputLine.readLine(); if (input.equals("y")) { System.out.print("\nEnter a password for this account : "); input = inputLine.readLine(); p.set_pword(input); c.set_amount(10000); a = new Account(); a.set_username(u); a.set_password(p); a.set_cash(c); accountList.get_aclist().addElement(a); loggedOn(a); a = null; } } } } } @) } BrokerSim { void loggedOn(Account a) throws Exception (@ Operation current_operation; // OpCode inv_opcode = new OpCode("invalid"); // current_operation.set_opCode(inv_opcode); OpCode quit_opcode = new OpCode(); quit_opcode = get_quit_opcode(); OpCode input_opcode = new OpCode(); BufferedReader inputLine; String input = new String(); System.out.println("\nWelcome! Time to start making some $$$ :-) \n"); do { show_Operations(); System.out.print("\nSo what's your pleasure : "); inputLine = new BufferedReader (new InputStreamReader(System.in)); input = inputLine.readLine(); input_opcode.set_o(input); current_operation = find_Operation(input_opcode); current_operation.execute(get_marketList(), a); } while (current_operation.get_opCode().get_o() != quit_opcode.get_o()); @) } BrokerSim { traversal findExitOpCode (FindExitOpVisitor fv) { through OperationList to Quit; } OpCode get_quit_opcode() (@ FindExitOpVisitor fv = new FindExitOpVisitor(); this.findExitOpCode(fv); return fv.get_return_val(); @) } BrokerSim { traversal checkAccount (CheckUsernameVisitor cv) { through AccountList to Username; } Account check_Username(Username username) (@ Account a = new Account(); CheckUsernameVisitor cv = new CheckUsernameVisitor(); cv.set_username(username); this.checkAccount(cv); a = cv.get_return_val(); return a; @) } BrokerSim { traversal allOperations (DisplayOpVisitor dv) { to {Buy, Sell, MarketReport, AccountReport, Quit}; } void show_Operations() (@ DisplayOpVisitor dv = new DisplayOpVisitor(); this.allOperations(dv); @) } BrokerSim { traversal findOperations (FindOpVisitor fv) { through OperationList to {Buy, Sell, MarketReport, AccountReport, Quit, Invalid}; } Operation find_Operation(OpCode o) (@ Operation op; FindOpVisitor fv = new FindOpVisitor(); fv.set_opcode(o); this.findOperations(fv); op = fv.get_return_val(); return op; @) } BrokerSim { void exit_Broker() throws Exception (@ get_accountList().write_file(); get_marketList().write_file(); @) } AccountList { traversal allAccounts (FileVisitor fv) { through {AccountList} to *; } void write_file() throws Exception (@ PrintWriter acc = new PrintWriter(new FileOutputStream("../../Broker.acc")); FileVisitor fv = new FileVisitor(acc); this.allAccounts(fv); acc.close(); @) } MarketList { traversal allInvestments (FileVisitor fv) { through {MarketList} to *; } void write_file() throws Exception (@ PrintWriter mar = new PrintWriter(new FileOutputStream("../../Broker.mar")); FileVisitor fv = new FileVisitor(mar); this.allInvestments(fv); mar.close(); @) } FileVisitor { before AccountList (@ file.println("("); @) before Username (@ file.print("Username: \"" + host.get_uname() + "\" "); @) before Password (@ file.print("Password: \"" + host.get_pword() + "\" "); @) before Cash (@ file.print("$ " + host.get_amount()); @) before MarketList (@ file.println("("); @) before TickerSymbol (@ file.print("* \"" + host.get_ttsymbol() + "\" "); @) before UnitValue (@ file.print("$ " + host.get_value() + " "); @) before Shares (@ file.print("# " + host.get_shares() + " "); @) before Description (@ file.print("\"" + host.get_desc() + "\""); @) before Portfolio (@ file.println("[ ("); @) after Account (@ file.println(); @) after AccountList (@ file.println(")"); @) after Investment (@ file.println(); @) after MarketList (@ file.println(")"); @) after Portfolio (@ file.println(") ]"); @) } FindExitOpVisitor { init (@ opcode = new OpCode(); @) before Quit (@ opcode = host.get_opCode(); @) return OpCode (@ opcode @) } FindOpVisitor { init (@ operation = new Invalid(); flag = false; @) before Operation (@ if (opcode.get_o().equals(host.get_opCode().get_o())) { operation = host; flag = true; } @) before Invalid (@ if (flag != true) { operation = host; } @) return Operation (@ operation @) } DisplayOpVisitor { before BrokerSim (@ System.out.println("\n---------------------------"); System.out.println("Please choose an operation:"); System.out.println("---------------------------\n"); @) before Operation (@ System.out.println(host.get_opCode().get_o() + "\t" + host.get_opDesc().get_desc()); @) } CheckUsernameVisitor { init (@ account = null; @) before Account (@ caccount = host; @) before Username (@ if (host.get_uname().equals(username.get_uname())) { account = caccount; } @) return Account (@ account @) }