import java.io.*;

/**
 * Writes formatted tables (i.e., 2-dimensional arrays of
 * String objects) to a PrintWriter
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class TableWriter
{
   private int               fieldWidth;
   private PrintWriter       out;

 /**
  * Explicit Value Constructor
  *
  * @param out   The PrintWriter to write to
  */
   public TableWriter(PrintWriter out)
   {
      this.out   = out;
      fieldWidth = 12;
   }


 /**
  * Print a table
  *
  * @param table   The table to print
  */
   public void printTable(String[][] table)
   {
      FieldFormat        formatter;
      int                i, j;
   
      formatter = new FieldFormat();
      formatter.setFieldWidth(fieldWidth);
   
      for (i=0; i < table.length; i++)
      {
      //for (j=0; j < table[0].length; j++)
         for (j=0; j < table[i].length; j++)
         
         {
            if (table[i][j] == null)
            {
               out.print(formatter.fitStringToField(" "));
            }
            else
            {
               out.print(formatter.fitStringToField(table[i][j]));
            }
         }
         out.println();
      }
   }



 /**
  * Set the field width to use for each 
  * entry in the table
  *
  * @param width
  */
   public void setFieldWidth(int width)
   {
      fieldWidth = width;
   }
 /**
  * write teh table with the references
  * in front of the item
  *
  * @param table The table to print
  */
   public void writeTable(String[][] table)
   {
      for (int row=0; row < table.length; row++)
      {
         for (int col=0; col < table[row].length; col++)
         {
            if (table[row][col] == null)
            {
               out.print("\t"); // leave a space
            }
            else
            {
               out.print(row + "," + col + ":" + table[row][col] + "\t");
            }
         }
      
         out.println();
      }
   }
   public void printTableWithHeaders(String[][] table)
   {
      FieldFormat        formatter;
      int                i, j;
   
      formatter = new FieldFormat();
      formatter.setFieldWidth(fieldWidth);
   
   // print top header choosing the first row to build headers
      out.print(formatter.fitStringToField(" ")); // empty spot
      for (i=0; i < table[0].length; i++)
      {
         out.print(formatter.fitStringToField("" + i)); // number
      }
   	out.println();
      for (i=0; i < table.length; i++)
      {
         for (j=0; j <= table[i].length; j++)  
         {
            if (j == 0)  // check for first element
               out.print(formatter.fitStringToField("" + i));
            else
            
               if (table[i][j-1] == null)
               {
                  out.print(formatter.fitStringToField(" "));
               }
               else
               {
                  out.print(formatter.fitStringToField(table[i][j-1]));
               }
         }
         out.println();
      }
   }
}
