1  /* 
  2   *  File:  pizza.java
  3   *
  4   *  Java pizza applet using CGI scripts and GridBagLayout
  5   *
  6   */
  7  
  8  import java.awt.*;
  9  import java.awt.event.*;
 10  import java.applet.*;
 11  import java.util.*;
 12  import java.net.*;
 13  import java.io.*;
 14  
 15  public class pizza extends Applet implements ActionListener
 16  {
 17     private Choice yesNo;
 18     private Choice name;
 19    
 20    
 21     private TextField addressField = new TextField();
 22     private TextField phoneField = new TextField();
 23     private TextField pizzaField = new TextField();
 24     
 25     private CheckboxGroup topping;
 26     private CheckboxGroup Ordertopping;
 27     private String str;
 28     private TextArea TAspecials;
 29     private TextArea TAorder;
 30  
 31  
 32     private void add(Component c, 
 33        GridBagLayout gbl, GridBagConstraints gbc,
 34        int x, int y, int w, int h)
 35  	
 36     {  gbc.gridx = x;
 37        gbc.gridy = y;
 38        gbc.gridwidth = w;
 39        gbc.gridheight = h;
 40        gbl.setConstraints(c, gbc);
 41        add(c);
 42     }
 43  
 44  
 45  
 46    public void init()
 47     {  
 48  	Component c;
 49  	resize(600,1000);
 50  	GridBagLayout gbl=new GridBagLayout();
 51  	setLayout(gbl);
 52  	GridBagConstraints gbc= new GridBagConstraints();
 53  	gbc.fill=GridBagConstraints.BOTH;
 54  	gbc.weightx=100;
 55  	gbc.weighty=100;
 56  	setFont(new Font("TimesRoman", Font.BOLD, 16));
 57  	int yoff=1;
 58  	
 59  
 60  	c=new Label("Fill-Out Form Example");
 61  	c.setFont(new Font("TimesRoman", Font.BOLD, 34));
 62  	add(c,gbl,gbc,0,0,4,2);
 63  	
 64  	c=new Label(" A fill-out form example, with multiple text entry fields and radio buttons..");
 65  	c.setFont(new Font("Curier", Font.BOLD, 13));
 66   	add(c,gbl,gbc,0,2,4,1);
 67  
 68  
 69  	c=new Label("Internet Pizza Delivery Service");
 70  	c.setFont(new Font("TimesRoman", Font.BOLD, 24));
 71  	add(c,gbl,gbc,0,3,4,1);
 72  
 73  	c=new Label(" ====================================================================================================================");
 74  	c.setFont(new Font("TimesRoman", Font.BOLD, 8));
 75  	add(c,gbl,gbc,0,4,4,1);
 76  	
 77       	/*_____________________________________________________________*/
 78  	yoff=5;
 79  	add(new Label(" "),gbl,gbc,0,yoff,4,1);
 80  	c=new Label("Would you like to view (today's) specials?");
 81  	c.setFont(new Font("TimesRoman", Font.BOLD, 18));
 82  	c.setForeground(Color.red);
 83  	add(c,gbl,gbc,0,yoff+1,4,1);
 84  	
 85  	
 86  	gbc.fill=GridBagConstraints.NONE;
 87  	yesNo= new Choice();
 88  	yesNo.setFont(new Font("TimesRoman", Font.BOLD, 16)); 
 89  	yesNo.addItem("Yes");
 90  	yesNo.addItem("No");
 91   	add(yesNo,gbl,gbc,1,yoff+2,1,1);
 92  	
 93  	gbc.fill=GridBagConstraints.VERTICAL;
 94  	Button vs=new Button("View Specials");
 95  	vs.addActionListener(this);
 96  	c=vs;
 97  	c.setBackground(Color.yellow);
 98  	c.setForeground(Color.blue);
 99  	add(vs,gbl,gbc,2,yoff+2,1,1);
100  	
101  	gbc.fill=GridBagConstraints.BOTH;
102  	TAspecials=new TextArea(10,40);
103  	add(TAspecials,gbl,gbc,0,yoff+3,4,4);
104  	
105        /*______________________________________________________________*/ 	
106  	yoff=yoff+9;
107  	
108  	add(new Label(" "),gbl,gbc,0,yoff-1,4,1);
109  	c=new Label("Order Form");
110  	c.setFont(new Font("TimesRoman", Font.BOLD, 18));
111  	c.setForeground(Color.red);
112  	add(c,gbl,gbc,0,yoff,4,1);
113  
114  	
115  	Panel address =new Panel();
116  	address.setLayout(new GridLayout(3,2));
117  	address.setFont(new Font("TimesRoman", Font.BOLD, 14));
118  	address.add(new Label("Type in your street address:"));
119  	address.add(addressField=new TextField(20));
120  	address.add(new Label("Type in your phone number:"));
121  	address.add(phoneField=new TextField(10));
122  	address.add(new Label("Choose a special pizza:"));
123  	address.add(pizzaField=new TextField(20));
124  	add(address,gbl,gbc,0,yoff+1,4,4);
125  	c=new Label("Or you may prefer to choose a topping for your pizza: ");
126  	c.setFont(new Font("TimesRoman", Font.BOLD, 15));
127  	add(c,gbl,gbc,0,yoff+5,4,1);
128  	
129  	Panel pTop2=new Panel();
130  	pTop2.setLayout(new GridLayout(3,1));	
131  	Ordertopping = new CheckboxGroup();
132  	pTop2.add(new Checkbox("Pepperoni",Ordertopping,true));
133  	pTop2.add(new Checkbox("Sausage",Ordertopping,false));
134  	pTop2.add(new Checkbox("Anchovy",Ordertopping,false));
135  	gbc.fill=GridBagConstraints.NONE;
136  	add(pTop2,gbl,gbc,2,yoff+6,2,2);
137  	
138  	
139  	gbc.fill=GridBagConstraints.HORIZONTAL;
140  	Button op = new Button("Order Pizza");
141  	op.addActionListener(this);
142  	c = op;
143  	c.setBackground(Color.yellow);
144  	add(c,gbl,gbc,0,yoff+7,1,1);
145  
146  	
147  
148  	gbc.fill=GridBagConstraints.BOTH;
149  	TAorder=new TextArea(10,40);
150  	add(TAorder,gbl,gbc,0,yoff+8,4,4);
151  	
152  	add(new Label(" "),gbl,gbc,0,yoff+12,4,1);
153  	c=new Label("class example. Please send comments to njm@npac.syr.edu. 2/28/96.");
154  	c.setFont(new Font("Courier", Font.BOLD, 12));
155  	c.setForeground(Color.green);
156  	add(c,gbl,gbc,0,yoff+13,4,1);
157  	
158    }
159   
160     public void actionPerformed(ActionEvent evt)
161     {
162       if (evt.getActionCommand().equals("View Specials"))
163        { handleViewSpecials();    }   
164  
165        else if (evt.getActionCommand().equals("Order Pizza"))
166        { handleOrderPizza();      }
167     }
168  
169  
170      public void handleViewSpecials()
171  	{
172  	String sdata;
173  	String rdata;
174  	sdata= "todayspecials=" + yesNo.getSelectedItem();
175  	String Script="/users-cgi/njm/Java/javapizzaspecials.pl";
176  	rdata=Network(sdata,Script);
177  	TAspecials.setText(rdata);
178  	}
179  
180     public void handleOrderPizza()
181  	{
182  	String sdata;
183  	String rdata;
184  	Checkbox c=Ordertopping.getSelectedCheckbox();
185  
186  	sdata = "address=" + addressField.getText()
187  		+ "&phone=" + phoneField.getText()
188  		+ "&specialorder="+pizzaField.getText()
189  		+ "&topping="+c.getLabel();
190  	String Script="/users-cgi/njm/Java/javapizza2.pl";
191  	rdata=Network(sdata,Script);
192  	TAorder.setText(rdata);
193  	 
194  	}
195   
196     public String Network(String sdata,String script)
197     {  String home = "osprey7.npac.syr.edu";
198       
199        int port = 3768;
200        Socket s = null;
201        String rdata = "";
202  /*	return sdata; */
203        try
204        {  
205           s = new Socket(home, port);
206  
207           DataOutputStream os
208              = new DataOutputStream(s.getOutputStream());
209           BufferedReader is
210              = new BufferedReader
211  		(new InputStreamReader(s.getInputStream()));
212              
213           os.writeBytes("POST " + script 
214              + " HTTP/1.0\r\n" 
215              + "Content-type: application/octet-stream\r\n"
216              + "Content-length: " 
217              + sdata.length() + "\r\n\r\n");
218           os.writeBytes(sdata);
219  
220           String line="";
221  	   while (((line = is.readLine()) != null)
222  			&& !(line.startsWith("Content-type:")))
223                     { };
224  
225             while ((line = is.readLine()) != null)
226           	     { rdata += line + "\n";} 
227           showStatus(rdata);
228           is.close();
229           os.close();
230        }
231        catch (Exception e)
232        {  showStatus("Error " + e);
233           if (s != null)
234              try
235                { s.close(); }
236              catch (IOException ex) {}
237        }
238         return rdata; 
239     }
240  }
241  
242  
243