Source of InvoiceTester.java


  1: import java.awt.*;
  2: import java.awt.event.*;
  3: import javax.swing.*;
  4: import javax.swing.event.*;

  6: /**
  7:    A program that tests the invoice classes.
  8: */
  9: public class InvoiceTester
 10: {
 11:    public static void main(String[] args)
 12:    {
 13:       final Invoice invoice = new Invoice();
 14:       final InvoiceFormatter formatter = new SimpleFormatter();

 16:       // This text area will contain the formatted invoice
 17:       final JTextArea textArea = new JTextArea(20, 40);

 19:       // When the invoice changes, update the text area
 20:       ChangeListener listener = new
 21:          ChangeListener()
 22:          {
 23:             public void stateChanged(ChangeEvent event)
 24:             {
 25:                textArea.setText(invoice.format(formatter));
 26:             }
 27:          };
 28:       invoice.addChangeListener(listener);

 30:       // Add line items to a combo box
 31:       final JComboBox combo = new JComboBox();
 32:       Product hammer = new Product("Hammer", 19.95);
 33:       Product nails = new Product("Assorted nails", 9.95);
 34:       combo.addItem(hammer);
 35:       Bundle bundle = new Bundle();
 36:       bundle.add(hammer);
 37:       bundle.add(nails);
 38:       combo.addItem(new DiscountedItem(bundle, 10));

 40:       // Make a button for adding the currently selected
 41:       // item to the invoice
 42:       JButton addButton = new JButton("Add");
 43:       addButton.addActionListener(new
 44:          ActionListener()
 45:          {
 46:             public void actionPerformed(ActionEvent event)
 47:             {
 48:                LineItem item = (LineItem) combo.getSelectedItem();
 49:                invoice.addItem(item);
 50:             }
 51:          });

 53:       // Put the combo box and the add button into a panel
 54:       JPanel panel = new JPanel();
 55:       panel.add(combo);
 56:       panel.add(addButton);

 58:       // Add the text area and panel to the content pane
 59:       JFrame frame = new JFrame();
 60:       frame.add(new JScrollPane(textArea),
 61:          BorderLayout.CENTER);
 62:       frame.add(panel, BorderLayout.SOUTH);
 63:       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 64:       frame.pack();
 65:       frame.setVisible(true);
 66:    }
 67: }