import java.text.*;

/**
 * That is, you can set the field width of a FieldFormat and
 * it will right-justify the formatted String in a field
 * of that width.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class FieldFormat
{
    private int        fieldWidth = -1;


    /**
     * Default Constructor
     */
    public FieldFormat()
    {
       super();
    }





    /**
     * Change the length of a String to fit exactly into
     * a field with the current width
     *
     * If the String is lengthened it will be right-justified
     *
     * @param original  The original String
     * @return          The shortened or lengthened String
     */
    public String fitStringToField(String original)
    {
       int            i;
       String         returnString;



       returnString = "";

       if (fieldWidth > 0)  // The field width has been set
       {
          if (original.length() <= fieldWidth) // Lengthen original
          {
             for (i=1; i <= fieldWidth-original.length(); i++) 
             {
                returnString += " ";
             }
             returnString += original;
          } 
          else                                // Shorten original
          {
             returnString = original.substring(0, fieldWidth);
          }
       }
       else   // The field width hasn't been set
       {
          returnString = original;
       }

       return returnString;

    }





    /**
     * Set the field width
     *
     * @param width  The width of the field
     */
    public void setFieldWidth(int width)
    {
       fieldWidth = width;
    }



}
