import java.io.*;
import org.w3c.dom.*;


/**
 * An encapsulation of an Address.
 * 
 * This is an example of "naive" serialization.
 * 
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class Address
{
    private int    postalCode;
    private String city, state, streetAddress;

    /**
     * Construct an empty Person object.
     */
    private Address()
    {
    }

    /**
     * Populate the attributes of this Address object from
     * an XML representation.
     * 
     * @param source  The XML representation
     */
    public void fromXML(Element source)
    {
        streetAddress  = source.getAttribute("streetAddress");
        city       = source.getAttribute("city");
        state      = source.getAttribute("state");
        postalCode = Integer.parseInt(source.getAttribute("postalCode"));
    }
    
    /**
     * Construct an Address object from an XML representation.
     * 
     * @param source  The XML representation
     * @return        The Address object
     */
    public static Address parseAddress(Element source)
    {
        Address result = new Address();
        result.fromXML(source);
        return result;
    }

    /**
     * Create an XML representation of this Address.
     * 
     * @return The XML representation
     */
    public String toXML()
    {
        return String.format("<Address streetAddress=\"%s\" city=\"%s\" state=\"%s\" postalCode=\"%s\" />",
                streetAddress, city, state, postalCode);
    }
}
