// COBOL Programmers Swing With Java - copyright 2005 Doke, Hardgrave, & Johnson // Chapter 11 - OO Development Issues // Demonstrate a GUI collaborating with a PD class // PostChecksGUI.java 1 JAN 05 import java.awt.*; import java.awt.event.*; public class PostChecksGUI extends MyWindow implements ActionListener { // reference variables for GUI components Button btAccept, btClear, btClose; // buttons TextField tfAccountNumber, tfCheckAmount; // text fields Label lblMessage; // label Panel pTop, pMiddle, pBottom; // panels CheckingAccountPD anAccount; // other variable definitions int accountNumber; float balance, checkAmount; boolean goodData; public static void main(String args[]) // main method called first { PostChecksGUI aWindow = new PostChecksGUI(); aWindow.setSize(300,170); // pixels wide x pixels high aWindow.show(); // display the window CheckingAccountPD.initialize(); // initialize data manager } // end of main method public PostChecksGUI() // constructor method { setTitle("CNB - Post Checks"); // place title pTop = new Panel(); // create panels pMiddle = new Panel(); pBottom = new Panel(); // place labels & textfields in top panel pTop.setLayout(new GridLayout(2, 2)); // rows & 2 cols pTop.add(new Label("AccountNumber: ", Label.RIGHT)); // Right justify the text pTop.add(tfAccountNumber = new TextField()); pTop.add(new Label("Check Amount: ", Label.RIGHT)); pTop.add(tfCheckAmount = new TextField()); // message label in middle panel pMiddle.setLayout(new FlowLayout()); pMiddle.add (lblMessage = new Label(" ")); // create & place the buttons in bottom panel pBottom.setLayout(new FlowLayout()); pBottom.add(btAccept = new Button("Accept")); pBottom.add(btClear = new Button("Clear")); pBottom.add(btClose = new Button("Close")); // place panels in the window setLayout(new BorderLayout()); add("North", pTop); add("Center", pMiddle); add("South", pBottom); btAccept.addActionListener(this); // listen for Button Events btClear.addActionListener(this); btClose.addActionListener(this); } // end of constructor method // event handler method for button clicks public void actionPerformed(ActionEvent event) { if (event.getSource() == btAccept) // if source is accept button accept(); if (event.getSource() == btClear) // if source is clear button clear(); if (event.getSource() == btClose) // if source is close button {CheckingAccountPD.terminate(); // shut down the data manager System.exit(0);} } // end of actionPerformed private void accept() { getInput(); if (goodData) postCheck(); } // end accept private void getInput() { goodData = true; try {accountNumber = Integer.parseInt(tfAccountNumber.getText().trim());} catch (NumberFormatException e) { lblMessage.setText("Bad Account Number Entered!"); goodData = false; } try {Float f = new Float(tfCheckAmount.getText().trim()); checkAmount = f.floatValue();} catch (NumberFormatException e) { lblMessage.setText("Bad Amount Entered!"); goodData = false; } } // end getInput public void postCheck() { try { anAccount = CheckingAccountPD.recordACheck(accountNumber, checkAmount); lblMessage.setText("Balance is: " + Float.toString(anAccount.getCurrentBalance())); } catch (NoAccountFoundException e) {lblMessage.setText("No Account Found");} catch (NSFException e) {lblMessage.setText("Insufficient Funds");} } // end postCheck private void clear() { tfAccountNumber.setText(""); tfCheckAmount.setText(""); lblMessage.setText(""); } // end of clear method } // end of PostChecksGUI.java