This section provides the completed code for the Shipping Costs Flex application. To see the application in action, see the Part II Introduction or the Code Anatomy. For step-by-step instructions to build this application, see the Tutorial.
The Shipping Costs Flex application code is available below in two categories, with three versions each, that only differ by one line of code:
<?php
// shipping.php contains our get_shipping_options($zipcode, $pounds) function.
// It returns an array mapping service name to price in US dollars.
require ('shipping.php');
$options = get_shipping_options($_REQUEST[zipcode], $_REQUEST[pounds]);
foreach ($options as $service => $price) {
$result[] = "$service: $price USD";
}
print implode("\n", $result);
?>
JSP and JAVA files
Plain text example
PlainHttpService.jsp
<%@page import="quickstart.ShippingCalculator,
quickstart.ShippingOption,
java.util.List" %>
<%
ShippingCalculator calc = new ShippingCalculator();
List options;
int zipcode;
double pounds;
zipcode = Integer.parseInt(request.getParameter("zipcode"));
pounds = Double.parseDouble(request.getParameter("pounds"));
options = calc.getShippingOptions(zipcode, pounds);
for (int i = 0; i < options.size(); i++) {
ShippingOption option = (ShippingOption) options.get(i);
%><%= option.getService() %>: <%= option.getPrice() %> USD
<%
}
%>
XML example
xmlHttpService.java
<%@page import="quickstart.ShippingCalculator,
quickstart.ShippingOption,
java.util.List" %>
<?xml version="1.0" encoding="utf-8"?>
<options>
<%
ShippingCalculator calc = new ShippingCalculator();
List options;
int zipcode;
double pounds;
zipcode = Integer.parseInt(request.getParameter("zipcode"));
pounds = Double.parseDouble(request.getParameter("pounds"));
options = calc.getShippingOptions(zipcode, pounds);
for (int i = 0; i < options.size(); i++) {
ShippingOption option = (ShippingOption) options.get(i);
%>
<option>
<service><%= option.getService() %></service>
<price><%= option.getPrice() %></price>
</option>
<%
}
%>
</options>
ShippingCalculator.java
package quickstart;
import java.util.ArrayList;
import java.util.List;
import java.lang.Math;
public class ShippingCalculator {
/* Returns a list of made-up ShippingOptions.
*/
public List getShippingOptions(int zipcode, double pounds) {
List options = new ArrayList();
double baseCost;
baseCost = Math.round(zipcode / 10000) + (pounds * 5);
options.add(new ShippingOption("Next Day", baseCost * 4));
options.add(new ShippingOption("Two Day Air", baseCost * 2));
options.add(new ShippingOption("Saver Ground", baseCost));
return options;
}
}
ShippingOption.java
package quickstart;
public class ShippingOption {
privateString service;
privatedouble price;
public ShippingOption() {
}
public ShippingOption(String aService, double aPrice) {
this.service = aService;
this.price = aPrice;
}
public void setService(String value) {
this.service = value;
}
public void setPrice(double value) {
this.price = value;
}
publicString getService() { returnthis.service; }
publicdouble getPrice() { returnthis.price; }
}
ASP.NET files
To use the ASP.NET files, you must create a virtual directory in the IIS Manager named
"quickstart". You must then create a subdirectory named "bin". Compile the two C# files
into quickstart.dll, and copy that DLL into the bin directory.