//======================================================================//  PROJECT:            programming.java, Chapter 9//  FILE:               OrderPlease.java//  VERSION:            2.0//  TARGET:             Java 1.1//  UPDATE HISTORY:     version 2.0        10/9/98     1.1 event model//                      version 1.1        7/9/97      the version in _p.j_//                      version 1.0        8/22/96     original: no Labels class//======================================================================//------------------------------ IMPORTS -------------------------------import java.applet.*;import java.awt.*;import java.awt.event.*;//========================== OrderPlease CLASS =========================/** * This applet implements a simple order form, using exceptions * to catch some of the possible input errors the user might make. */public class OrderPlease extends    Applet                         implements ActionListener{    private final String EMPTY = new String(),                          CRLF = new String("\n");   	    private Label        title = new Label("Monty's Musical Madness");	        private TextField    custName = new TextField(20),                         custStreet = new TextField(20),                         custCityState = new TextField(20),                         custZip = new TextField(20),                         custPhone = new TextField(20),                         custCC = new TextField(15),                         custExp = new TextField("  /  ",5),                         custQuantity = new TextField(4);	    private Labels       label = new Labels();						    private Choice       ccChoice = new Choice(),                         productChoice = new Choice();       private Button       bAddItem = new Button("Add Item"),                         bReset = new Button("Reset"),                         bSubmit = new Button("Submit");                             private TextArea     order = new TextArea(6,40);   	    /**     * Lay out the (many!) widgets and register the     * three buttons as sources of action events     */    public void init()    {    	title.setForeground(Color.red);    	title.setFont(new Font("TimesRoman", Font.BOLD, 24));        add(title);		        //----- p1 is the panel that holds the basic customer information.                Panel p1 = new Panel();        p1.setBackground(Color.cyan);        p1.setLayout(new GridLayout(5,2, 6, 2));      	        p1.add(label.name);        custName.setBackground(Color.white);        p1.add(custName);      	        p1.add(label.street);        custStreet.setBackground(Color.white);        p1.add(custStreet);      	        p1.add(label.cityState);        custCityState.setBackground(Color.white);        p1.add(custCityState);      	        p1.add(label.zip);        custZip.setBackground(Color.white);        p1.add(custZip);      	        p1.add(label.phone);        custPhone.setBackground(Color.white);        p1.add(custPhone);      	        add(p1);                    //----- p2 is the credit card information panel.                Panel p2 = new Panel();        p2.setBackground(Color.cyan);		        ccChoice.addItem("Visa");        ccChoice.addItem("AMEX");        ccChoice.addItem("MasterCard");        ccChoice.addItem("Discover");      	        p2.add(label.ccName);        ccChoice.setBackground(Color.white);        p2.add(ccChoice);      	        p2.add(label.ccNum);        custCC.setBackground(Color.white);        p2.add(custCC);      	        p2.add(label.ccExp);        custExp.setBackground(Color.white);        p2.add(custExp);      	        add(p2);              //----- p3 is the product panel.                Panel p3 = new Panel();        p3.setBackground(Color.cyan);		        productChoice.addItem("Amazing Accordian");        productChoice.addItem("Harmonious Harmonica");        productChoice.addItem("Dapper Drum Set");        productChoice.addItem("Going Going Gong!");        productChoice.addItem("Trump No Trumpet");        productChoice.addItem("Kabloowie Kazooie!");        productChoice.addItem("Power Panflute");        productChoice.addItem("Supersizer Synthesizer");        productChoice.addItem("Zany Zither");      	        p3.add(label.product);        productChoice.setBackground(Color.white);        p3.add(productChoice);      	        p3.add(label.quantity);        custQuantity.setBackground(Color.white);        p3.add(custQuantity);      	        add(p3);              //----- Panel p4a is the button panel.                Panel p4a = new Panel();        p4a.add(bAddItem);        p4a.add(bReset);        p4a.add(bSubmit);                // Register the three buttons        bAddItem.addActionListener(this);        bReset.addActionListener(this);        bSubmit.addActionListener(this);  	        //----- p4b holds the information about the current order.                Panel p4b = new Panel();        p4b.add(label.order);        p4b.add(order);                //----- p4 holds the button panel and the info panel                Panel p4 = new Panel();        p4.setLayout(new BorderLayout());        p4.add("North", p4a);        p4.add("Center", p4b);                add(p4);    }        /**     * This action routine not only responds to the button clicks      * for processing the order information, but also catches the possible     * MissingData exception thrown by method processSubmitButton().     */    public void actionPerformed(ActionEvent e)     {    	Object source = e.getSource();        if (source == bAddItem)        {            processAddItemButton();        }        else if (source == bReset)        {            processResetButton();        }        else if (source == bSubmit)        {            try            {                processSubmitButton();            }            catch (MissingData ex)	// Some data was missing            {                order.appendText("*** " + ex.getMessage() + CRLF);            }        }    }    //----------------------- PRIVATE UTILITIES ------------------------	    /**     * Before adding a new item to the order, this method first checks that     * the quantity entered in the custQuantity Textfield is     * (1) an integer and (2) greater than zero.     * CALLED BY: actionPerformed()     */    private void processAddItemButton()    {        try        {            // This might throw a NumberFormatException            int quantity = Integer.parseInt(custQuantity.getText());            // This might throw our own IllegalQuantity exception            doAddItem(quantity);        }        catch (NumberFormatException ex) 	// The quantity is not an integer.        {            order.appendText("*** Must provide an integer value" + CRLF);            order.appendText("*** in the quantity field" + CRLF);        }        catch (IllegalQuantity ex) 		// The quantity is not positive.        {            order.appendText("*** " + ex.getMessage() + CRLF);            order.appendText("*** in the quantity field" + CRLF);        }    }    /**     * Try to add the current item to the order, checking that the     * number entered was positive.     * CALLED BY: processAddItemButton()     * THROWS: IllegalQuantity     */    private void doAddItem(int quant) throws IllegalQuantity    {        if (quant < 1) // Scream if it isn't!        {            throw new IllegalQuantity("Must have a positive amount");        }        else // Quantity is OK, so go ahead and add the item to our order.        {            order.appendText("" + productChoice.getSelectedItem() +                             "   (" + custQuantity.getText() + ")" + CRLF);        }    }	    /**     * Clear all the data fields.     * CALLED BY: actionPerformed()     */    private void processResetButton()    {        custName.setText(EMPTY);        custStreet.setText(EMPTY);        custCityState.setText(EMPTY);        custZip.setText(EMPTY);        custPhone.setText(EMPTY);        custCC.setText(EMPTY);        custQuantity.setText(EMPTY);        custExp.setText("  /  ");        order.setText(EMPTY);    }		    /**     * Make sure all data is filled in before submitting the order.     * CALLED BY: actionPerformed()     * THROWS: MissingData     */    private void processSubmitButton() throws MissingData    {        if ((custName.getText().equals(EMPTY))        ||             (custStreet.getText().equals(EMPTY))      ||            (custCityState.getText().equals(EMPTY))   ||            (custZip.getText().equals(EMPTY))         ||            (custPhone.getText().equals(EMPTY))       ||            (custCC.getText().equals(EMPTY))          ||            (custQuantity.getText().equals(EMPTY))    ||            (custExp.getText().equals("  /  "))       ||            (order.getText().equals(EMPTY))	)         {            throw new MissingData("Must enter data in all fields");        }        else // Data is all there, so submit the order.        {            order.appendText(CRLF + "	***  ORDER PLACED  ***");        }    }	}//============================ Labels CLASS ============================/** * The sole purpose of this class is to encapsulate the labels used * in the OrderPlease applet. */class Labels{    // Since this class is private within the applet, we can get    // away with giving these variables package access.    Label   name,            street,            cityState,            zip,            phone,            ccName,            ccNum,            ccExp,            product,            quantity,            order;						    /**     * All the constructor has to do is initialize the labels.     */    public Labels()    {        name =      new Label("Name:", Label.RIGHT);        street =    new Label("Street:", Label.RIGHT);        cityState = new Label("City/State:", Label.RIGHT);        zip =       new Label("Zip Code:", Label.RIGHT);        phone =	    new Label("Phone #:", Label.RIGHT);        ccName =    new Label("Use:", Label.RIGHT);        ccNum =	    new Label("Card #:", Label.RIGHT);        ccExp =     new Label("Exp. date:", Label.RIGHT);        product =   new Label("Product:", Label.RIGHT);        quantity =  new Label("Qty:", Label.RIGHT);        order =     new Label("Order:", Label.RIGHT);    }}//========================== EXCEPTION CLASSES =========================/** * This is thrown by the doAddItem() method when the number  * of items isn't positive. */class IllegalQuantity extends Exception {    public IllegalQuantity()    {        super();    }        public IllegalQuantity(String s)    {        super(s);    }}/** * Thrown by the processSubmitButton() method when one or more * of the data fields are empty. */class MissingData extends Exception {    public MissingData()    {        super();    }        public MissingData(String s)    {        super(s);    }}