01: import java.awt.*;
02: import java.awt.event.*;
03: import javax.swing.*;
04: import javax.swing.event.*;
05:
06: /**
07: A program that tests the invoice classes.
08: */
09: public class InvoiceTest
10: {
11: public static void main(String[] args)
12: {
13: final Invoice invoice = new Invoice();
14: final InvoiceFormatter formatter = new SimpleFormatter();
15:
16: // this text area will contain the formatted invoice
17: final JTextArea textArea = new JTextArea(20, 40);
18:
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);
29:
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));
39:
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: });
52:
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);
57:
58: // add the text area and panel to the content pane
59: JFrame frame = new JFrame();
60: Container contentPane = frame.getContentPane();
61: contentPane.add(new JScrollPane(textArea),
62: BorderLayout.CENTER);
63: contentPane.add(panel, BorderLayout.SOUTH);
64: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
65: frame.pack();
66: frame.show();
67: }
68: }