Extending the Product object

In this lesson, you customize the product object to define the user data and add the corresponding set/get methods to access the custom data.

About this task

The class Product.java is to define the Plain Old Java Object (POJO) for the Product noun. The data model POJO has a flat structure. It contains strings such as name, partNumber, short description, and more. It also creates the set/get methods so that to access the corresponding attribute data from the Product noun. Since warranty is the userData against Product, you need to customize the POJO to define the user data and add the corresponding set/get methods to access the custom data.

Procedure

  1. Launch your Eclipse for developing the Android application.
  2. Ensure that you have the Android 2.2 package installed.
    1. Click Window > Android SDK Manager.
    2. Scroll to the Android 2.2 (API 8) package.
    3. If the status is Not installed, click the package and click Install 6 packages.
      When the status changes to Installed, continue with the next step.

      Screen capture to show Android 2.2 installed

  3. Create an Android project and import the Android sample code.
    Note: Depending on your version of Eclipse, you can have different ways of importing sample code.
    1. Click File > New > Android Project

      Image of Android Project

    2. Enter the Project name to be MyProject, choose to create project from existing source
    3. Browse the root location to be WCDE_installdir\components\store-enhancements\samples\Android\Madisons-AndroidNativeMobile
      Note: Within the WCDE_installdir\components\store-enhancements\samples\Android folder, extract the Madisons-AndroidNativeMobile.zip compressed file first.
    4. Select the option Google APIs with platform 2.2. Click Finish to create the project.

      A screen capture of the new Android project wizard.

  4. In the Package Explorer view of the Eclipse, navigate to MyProject > src > com.ibm.commerce.android.nativeapp.mobile.objects. Open the file Product.java.
  5. Scroll to the bottom of the existing field definitions
    private String parentCatEntryId;
    private boolean isGiftItemInCart;
    private boolean isSubscriptionItem;
  6. Add two new fields to the class as shown. The first field stores the warrantyTerm while the second field stores the warrantyType.
    /**
    * Define the private string for the user data warrantyTerm
    */
    
    private String warrantyTerm;
    
    /**
    * Define the private string for the user data warrantyType
    */
    
    private String warrantyType;
  7. Add getter and setter methods for the new fields so that the user data is accessible.
    public String getWarrantyTerm() {
      return warrantyTerm;
    }
    public void setWarrantyTerm(String warrantyTerm) {
      this.warrantyTerm = warrantyTerm;
    }
    public String getWarrantyType() {
      return warrantyType;
    }
    public void setWarrantyType(String warrantyType) {
      this.warrantyType = warrantyType;
    }
  8. Save and close the file.
    Note: You also can add the getter and setter methods for the new fields by right-clicking and selecting Source > Generate Getters and Setters from the context menu.