     /****************************************************************
      * Copyright (C) 1996 - 2001 Micro Focus International Ltd.     *
      * All Rights Reserved.                                         *
      ****************************************************************/

import com.microfocus.cobol.lang.*;
import java.text.*;

public class RecordData implements DataType
{
    private String customerName, customerAddress;
    private int customerRef;
    private byte[] bytes;

    RecordData(String name, String address, int ref)
    {
        customerName = name;
        customerAddress = address;
        customerRef = ref;
    }

    public void synchronizeData()
    {
	// This method is called from COBOL prior to return from a method.
	//
        // We use it here to update each field to contain the
        // data returned by COBOL in the updated byte array.

        customerName = new String(bytes, 0, 30);
        customerAddress = new String(bytes, 30, 30);
        customerRef = Integer.parseInt(new String(bytes, 60, 6));
    }

    public byte[] getBytes()
    {
	// This method is called from COBOL on entry to a method to obtain
        // the data to be moved into the COBOL data type.

	// To pass the information, a temporary string is created from the
        // fields in the class, to allow them to be passed to COBOL as a
        // byte array.

        int i;
        StringBuffer tmpCustomerName = new StringBuffer(customerName);
        // The COBOL program is expecting 30 character strings, so space fill.
        for (i = tmpCustomerName.length(); i < 30; i++)
             tmpCustomerName.append(" ");

        StringBuffer tmpCustomerAddress = new StringBuffer(customerAddress);
        for (i = tmpCustomerAddress.length(); i < 30; i++)
             tmpCustomerAddress.append(" ");

        // Format the reference number to match PIC 9(6)
        DecimalFormat pic9display = new DecimalFormat("000000");
        StringBuffer tmpCustomerRef = pic9display.format(customerRef,
                                                         new StringBuffer(""),
                                                         new FieldPosition(0));

        String tmpString = new String(tmpCustomerName.toString() +
                                      tmpCustomerAddress.toString() +
                                      tmpCustomerRef.toString());
	bytes = tmpString.getBytes();
 	return bytes;
    }

    public String getCustomerName()
    {
      return this.customerName;
    }

    public String getCustomerAddress()
    {
      return this.customerAddress;
    }

    public int getCustomerRef()
    {
      return this.customerRef;
    }

    public void showRef()
    {
	// Method used to illustrate that the customerRef field was updated.

        System.out.println("Updated customerRef is : " + customerRef);
    }

    public String toString()
    {
        return "Customer Name    : "+this.customerName+"\n"+
               "Customer Address : "+this.customerAddress+"\n"+
	       "Customer Ref     : "+this.customerRef;
    }
}

