import java.util.Date; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.FlowLayout; import javax.swing.*; import java.awt.event.*; /** Main CheckBook program containing a graphical interface to create and delete transaction objects. This class handles all interaction with the user, calling on other classes to perform the necessary computations. @author Bert G. Wachsmuth @version Feb. 2002 */ public class Money extends JFrame implements ActionListener, ItemListener { // fields private JCheckBox checkOption = new JCheckBox("Check", true); private JCheckBox depositOption = new JCheckBox("Deposit", false); private JCheckBox adminOption = new JCheckBox("Administrative", false); private JButton add = new JButton("Add", new ImageIcon("star.gif")); private JButton del = new JButton("Remove"); private JButton quit = new JButton("Quit"); private JTextField name = new JTextField(40); private JTextField amount = new JTextField(10); private JTextField date = new JTextField(25); private JLabel checkAmount = new JLabel("Checks: $0.0"); private JLabel depositAmount = new JLabel("Deposits: $0.0"); private JLabel adminAmount = new JLabel("Admins: $0.0"); private JLabel totalAmount = new JLabel("Total: $0.0"); private DefaultListModel listData = new DefaultListModel(); private JList transactionList = new JList(listData); private Checkbook checkbook = new Checkbook("1st Java Bank of New Jersey"); /** The constructor creates the JFrame, adds all GUI components using suitable layout managers, and makes the frame visible. */ public Money() { super("SHU Money ver. 1.0"); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add("North", getStatusPanel()); panel.add("Center", getInputPanel()); panel.add("East", getButtonPanel()); panel.add("South", getOptionsPanel()); getContentPane().setLayout(new BorderLayout()); getContentPane().add("South", panel); getContentPane().add("Center", getListPanel()); // activating the buttons so they can generate action events, which are intercepted by // the 'actionPerformed' method. del.addActionListener(this); add.addActionListener(this); quit.addActionListener(this); // activating the checkboxes so that they can generate 'item events', which are intercepted // by the 'itemStateChanged' method. This allows the program to disallow editing of the // 'name' field when an 'administrative' transaction is chosen. checkOption.addItemListener(this); depositOption.addItemListener(this); adminOption.addItemListener(this); validate(); pack(); show(); // adding today's date as default entry to the 'date' text field date.setText(Transaction.toDate(new Date())); updateStatus(); name.requestFocus(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /** Method to layout the list together with its title. */ private JPanel getListPanel() { JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add("North", new JLabel("

Transaction List

")); panel.add("Center", new JScrollPane(transactionList)); return panel; } /** Method to layout the options and add them to a ButtonGroup for mutually exclusive selections. */ private JPanel getOptionsPanel() { JPanel panel = new JPanel(); panel.setLayout(new FlowLayout()); panel.add(checkOption); panel.add(depositOption); panel.add(adminOption); ButtonGroup radioGroup = new ButtonGroup(); radioGroup.add(checkOption); radioGroup.add(depositOption); radioGroup.add(adminOption); return panel; } /** Method to layout the status line (fields for check, deposit, etc. amounts). */ private JPanel getStatusPanel() { JPanel status = new JPanel(); status.setLayout(new GridLayout(1, 4)); status.add(checkAmount); status.add(depositAmount); status.add(adminAmount); status.add(totalAmount); return status; } /** Method to layout the buttons in a grid and to add tool tips to the buttons. */ private JPanel getButtonPanel() { JPanel buttons = new JPanel(); buttons.setLayout(new GridLayout(3, 1)); buttons.add(add); add.setToolTipText("Add a new transaction"); buttons.add(del); del.setToolTipText("Deletes the selected transaction"); buttons.add(quit); quit.setToolTipText("Quits the program"); return buttons; } /** Method to layout the input text fields and their labels. */ private JPanel getInputPanel() { JPanel labels = new JPanel(); labels.setLayout(new GridLayout(3, 1)); labels.add(new JLabel("Name")); labels.add(new JLabel("Amount")); labels.add(new JLabel("Date")); JPanel fields = new JPanel(); fields.setLayout(new GridLayout(3, 1)); fields.add(name); fields.add(amount); fields.add(date); name.setToolTipText("Enter recipient of transaction here"); amount.setToolTipText("Enter dollar amount of your transaction here"); date.setToolTipText("Enter date in MM/DD/YYYY format"); JPanel input = new JPanel(); input.setLayout(new BorderLayout()); input.add("West", labels); input.add("Center", fields); return input; } /** Updating the status fields for new deposit, check, etc. amounts. */ private void updateStatus() { checkAmount.setText("Checks: " + Transaction.toMoney(checkbook.getCheckAmount())); depositAmount.setText("Deposits: " + Transaction.toMoney(checkbook.getDepositAmount())); adminAmount.setText(" Admins: " + Transaction.toMoney(checkbook.getAdminAmount())); totalAmount.setText(checkbook.getSize() + " Total: " + Transaction.toMoney(checkbook.getAmount())); } /** Adding a new transaction depending on which of the checkboxes is currently selected. Note that this method converts a String to a double, a process that may or may not succeed. Also, no test is made to check for sufficient funds when adding a new transaction. */ private void addTransaction() { String name_s = name.getText().trim(); String amount_s = amount.getText().trim(); String date_s = date.getText().trim(); double amount_d = Double.parseDouble(amount_s); Transaction t = null; if (checkOption.isSelected()) { t = new Check(name_s, amount_d, date_s); listData.addElement(t.toString()); checkbook.addTransaction((Check)t); } else if (depositOption.isSelected()) { t = new Deposit(name_s, amount_d, date_s); checkbook.addTransaction((Deposit)t); listData.addElement(t.toString()); } else { t = new Administrative(name_s, amount_d, date_s); checkbook.addTransaction((Administrative)t); listData.addElement(t.toString()); } updateStatus(); name.setText(""); amount.setText("0.0"); name.requestFocus(); } /** Removing a selected transaction. */ private void delTransaction() { int pos = transactionList.getSelectedIndex(); if (pos >= 0) { transactionList.remove(pos); checkbook.deleteTransaction(pos); System.gc(); updateStatus(); } } /** Standard method to intercept action events and react accordingly. */ public void actionPerformed(ActionEvent ae) { if (ae.getSource() == quit) System.exit(0); else if (ae.getSource() == add) addTransaction(); else if (ae.getSource() == del) delTransaction(); } /** Standard method to intercept item events and react accordingly. */ public void itemStateChanged(ItemEvent ie) { if (adminOption.isSelected()) { name.setText(checkbook.getBankName()); name.setEditable(false); } else name.setEditable(true); } /** Standard method to make this class executable. */ public static void main(String args[]) { Money m = new Money(); } }