1  import java.awt.*;
  2  import java.awt.event.*;
  3  import javax.swing.*;
  4  import javax.swing.event.*;
  5  
  6  /**
  7     A program that tests the invoice classes.
  8  */
  9  public class InvoiceTester
 10  {
 11     public static void main(String[] args)
 12     {
 13        Invoice invoice = new Invoice();
 14        InvoiceFormatter formatter = new SimpleFormatter();
 15  
 16        // This text area will contain the formatted invoice
 17        JTextArea textArea = new JTextArea(20, 40);
 18  
 19        // When the invoice changes, update the text area
 20        invoice.addChangeListener(event ->
 21           textArea.setText(invoice.format(formatter)));
 22  
 23        // Add line items to a combo box
 24        JComboBox<LineItem> combo = new JComboBox<>();
 25        Product hammer = new Product("Hammer", 19.95);
 26        Product nails = new Product("Assorted nails", 9.95);
 27        combo.addItem(hammer);
 28        Bundle bundle = new Bundle();
 29        bundle.add(hammer);
 30        bundle.add(nails);
 31        combo.addItem(new DiscountedItem(bundle, 10));
 32  
 33        // Make a button for adding the currently selected
 34        // item to the invoice
 35        JButton addButton = new JButton("Add");
 36        addButton.addActionListener(event ->
 37           {
 38              LineItem item = combo.getItemAt(combo.getSelectedIndex());
 39              invoice.addItem(item);
 40           });
 41  
 42        // Put the combo box and the add button into a panel
 43        JPanel panel = new JPanel();
 44        panel.add(combo);
 45        panel.add(addButton);
 46  
 47        // Add the text area and panel to the frame
 48        JFrame frame = new JFrame();
 49        frame.add(new JScrollPane(textArea),
 50           BorderLayout.CENTER);
 51        frame.add(panel, BorderLayout.SOUTH);
 52        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 53        frame.pack();
 54        frame.setVisible(true);
 55     }
 56  }