PATH  WebObjects 4.0 Documentation > Getting Started With WebObjects

Table of Contents Previous Section

Creating a Guest List

To store the information from all guests that have accessed the application, you'll create an application variable called allGuests, which exists for the life of the application.

 

  1. In Project Builder, select Classes in the first column of the Browser. Then select Application.java from the second column.

    The application's code appears in the window. The following listing shows the code generated by the Wizard, along with code you will add.

    // Generated by the WebObjects Wizard
       
    import com.apple.yellow.foundation.*;
    import com.apple.yellow.webobjects.*;
       
    public class Application extends WOApplication {
       
       protected NSMutableArray allGuests;
       public Application() {
       	super();
       	allGuests = new NSMutableArray();
       	System.out.println("Welcome to " + this.name() + " !");
       	/* ** put your initialization code in here ** */
       }
    	
       public void addGuest(Guest aGuest){
       	allGuests.addObject(aGuest);
       }
    	
       public void clearGuests(){
       	allGuests.removeAllObjects();
       }
    }
    	

    Note that there is one method already defined: Application, which is the constructor for the application object. The first line calls the constructor for Application's superclass (which is the class WOApplication). The second line prints a message, which you see in the Launch panel when you launch your application.

     

  2. After the call to super, enter this code:
    allGuests = new NSMutableArray();
       

    This statement initializes allGuests to be a new object of class NSMutableArray. This class is the Java equivalent of the Objective-C class NSMutableArray, which provides an interface that allows you to add, change and delete objects from an array.

     

  3. At the top of the Application class definition, enter this declaration:
    protected NSMutableArray allGuests;
       

    This declares allGuests to be of type NSMutableArray. Declaring it protected means that it is accessible only from this class or one of its subclasses. It is standard object-oriented practice for a class to prevent other classes from directly manipulating its instance variables. Instead, you provide accessor methods that other objects use to read or modify the instance variables.

     

  4. Add the accessor methods addGuest and clearGuests, as shown in the listing.

    The addGuest method adds an object of class Guest to the end of the allGuests array, using the NSMutableArray method addObject.

    The clearGuests method removes all the objects from the array using the NSMutableArray method removeAllObjects.

     

  5. Save Application.java.

Table of Contents Next Section





Find: 

Copyright © 1998 Apple Computer, Inc. All rights reserved.