import java.awt.*; // Java GUI classews import java.applet.*; import java.awt.event.*; // Presentation Layer public class StockApplet extends Applet implements ActionListener { // buttons private Button newinvestor, newacct, login, deposit, withdraw, portfolio, orders, trans, quote, buy, sell, test, chgacct; // lables for fields private Label acctLabel, firstLabel, lastLabel, idLabel, phoneLabel, passLabel, cashLabel, stockLabel, sharesLabel, priceLabel; // fields private TextField acctField, firstField, lastField, idField, phoneField, passField, cashField, stockField, sharesField, priceField; // print to textArea private TextArea textArea1; // references to Domain Logic private AccountManagement management; private ReportGenerator report; private TradeExecution trade; public void init() { super.init(); setLayout(null); // create a label and field, place on the screen idLabel = new Label("Investor ID:"); idLabel.setBounds(20,10,70,30); add(idLabel); idField = new TextField(130); idField.setBounds(100,10,80,30); add(idField); passLabel = new Label("Password:"); passLabel.setBounds(210,10,70,30); add(passLabel); passField = new TextField(100); passField.setEchoChar('*'); passField.setBounds(280,10,80,30); add(passField); acctLabel = new Label("Account#:"); acctLabel.setBounds(380,10,70,40); add(acctLabel); acctField = new TextField(50); acctField.setBounds(460,10,80,30); add(acctField); firstLabel = new Label("First:"); firstLabel.setBounds(20,50,63,30); add(firstLabel); firstField = new TextField(150); firstField.setBounds(100,50,80,30); add(firstField); lastLabel = new Label("Last:"); lastLabel.setBounds(210,50,63,30); add(lastLabel); lastField = new TextField(150); lastField.setBounds(280,50,80,30); add(lastField); phoneLabel = new Label("Phone:"); phoneLabel.setBounds(380,50,45,30); add(phoneLabel); phoneField = new TextField(130); phoneField.setBounds(460,50,130,30); add(phoneField); cashLabel = new Label("Cash:"); cashLabel.setBounds(20,110,63,30); add(cashLabel); cashField = new TextField(150); cashField.setBounds(100,110,80,30); add(cashField); stockLabel = new Label("Stock:"); stockLabel.setBounds(20,160,50,30); add(stockLabel); stockField = new TextField(80); stockField.setBounds(100,160,80,30); add(stockField); sharesLabel = new Label("Shares:"); sharesLabel.setBounds(210,160,50,30); add(sharesLabel); sharesField = new TextField(80); sharesField.setBounds(280,160,80,30); add(sharesField); priceLabel = new Label("Price:"); priceLabel.setBounds(380,160,50,30); add(priceLabel); priceField = new TextField(80); priceField.setBounds(460,160,80,30); add(priceField); // create a TextArea for printing textArea1 = new TextArea(); textArea1.setBounds(20,270,580,350); Font font = new Font("Courier",Font.PLAIN,20); textArea1.setFont(font); add(textArea1); // create a button, place on the screen, applet "listens" for button push login = new Button("Login"); login.setBounds(20,210,60,25); add(login); login.addActionListener(this); chgacct = new Button("Chg Acct"); chgacct.setBounds(20,240,60,25); add(chgacct); chgacct.addActionListener(this); newinvestor = new Button("New Investor"); newinvestor.setBounds(90,210,100,25); add(newinvestor); newinvestor.addActionListener(this); newacct = new Button("New Account"); newacct.setBounds(90,240,100,25); add(newacct); newacct.addActionListener(this); deposit = new Button("Deposit"); deposit.setBounds(200,210,70,25); add(deposit); deposit.addActionListener(this); withdraw = new Button("Withdraw"); withdraw.setBounds(200,240,70,25); add(withdraw); withdraw.addActionListener(this); buy = new Button("Buy"); buy.setBounds(280,210,50,25); add(buy); buy.addActionListener(this); sell = new Button("Sell"); sell.setBounds(280,240,50,25); add(sell); sell.addActionListener(this); orders = new Button("Pending Orders"); orders.setBounds(340,210,110,25); add(orders); orders.addActionListener(this); trans = new Button("Transactions"); trans.setBounds(340,240,110,25); add(trans); trans.addActionListener(this); quote = new Button("Quote"); quote.setBounds(460,210,70,25); add(quote); quote.addActionListener(this); portfolio = new Button("Portfolio"); portfolio.setBounds(460,240,70,25); add(portfolio); portfolio.addActionListener(this); test = new Button("Test"); test.setBounds(540,240,50,25); add(test); test.addActionListener(this); // pre-populate some Domain Objects new StockInit(); // create the Domain Logic classes // hand over textArea1 so class can print to it (in this case, same TextArea) management = new AccountManagement(textArea1); report = new ReportGenerator(textArea1); trade = new TradeExecution(textArea1); } // called by actionPerformed whenever a button is pushed // also called by StockTest to do Automated Test public void doAction(String arg) { // depending on the button, call the appropriate method in Domain Logic if (arg.equals("Login")) management.doLogin(idField.getText(),passField.getText(),acctField,firstField, lastField,phoneField); if (arg.equals("Chg Acct")) management.doChgAcct(acctField.getText()); if (arg.equals("New Investor")) management.doNewInvestor(idField.getText(), passField.getText(), firstField.getText(), lastField.getText(),phoneField.getText(),acctField); if (arg.equals("New Account")) management.doNewAccount(acctField); if (arg.equals("Deposit")) management.doCash("DEPOSIT",cashField.getText()); if (arg.equals("Withdraw")) management.doCash("WITHDRAW",cashField.getText()); if (arg.equals("Portfolio")) report.doPortfolio(); if (arg.equals("Pending Orders")) report.showOrders(); if (arg.equals("Transactions")) report.showTrans(); if (arg.equals("Quote")) report.showQuote(stockField.getText()); if (arg.equals("Buy")) trade.doOrder("BUY",stockField.getText(),sharesField.getText(),priceField.getText()); if (arg.equals("Sell")) trade.doOrder("SELL",stockField.getText(),sharesField.getText(),priceField.getText()); if (arg.equals("Test")) new StockTest (this,textArea1,acctField,firstField,lastField,idField,phoneField, passField,cashField,stockField, sharesField, priceField); } // called by the window manager whenever a button is pushed public void actionPerformed(ActionEvent event) { String arg = event.getActionCommand(); doAction(arg); } } // class StockApplet