//*******************************************************************
//
//   Printer2.java          In Text          Application
//
//   Authors:  Lewis and Loftus
//
//   Classes:  Printer2
//             File
//             Text_File
//             Binary_File
//             Image_File
//             Print_Logger
//
//   Interfaces:  Printable
//
//*******************************************************************


//-------------------------------------------------------------------
//
//  Class Printer2 demonstrates the use of an interface.
//
//  Methods:
//
//     public static void main (String[] args)
//
//-------------------------------------------------------------------
public class Printer2 {

   //===========================================================
   //  Creates two file objects and logs some printing
   //  activity.
   //===========================================================
   public static void main (String[] args) {

      byte[] logo_data  = {41, 42, 49, 44};

      Text_File report = new Text_File
         ("Sand Reconner", 66, "One two three");

      Image_File logo = new Image_File
         ("Number 1", 45, logo_data);

      Print_Logger daily = new Print_Logger();

      daily.log (report);
      daily.log (logo);

   } // method main

} // class Printer2

//-------------------------------------------------------------------
//
//  Class File represents a generic file.
//
//  Constructors:
//
//     public File (String file_id, int file_size)
//
//  Methods:
//
//     public String name()
//
//-------------------------------------------------------------------

class File {

   protected String id;
   protected int size;

   //===========================================================
   //  Sets up the object using the specified data.
   //===========================================================
   public File (String file_id, int file_size) {
      id = file_id;
      size = file_size;
   } // constructor File

   //===========================================================
   //  Returns the name of the file.
   //===========================================================
   public String name() {
      return id;
   } // method name

} // class File

//-------------------------------------------------------------------
//
//  Class Text_File represents a file that contains text.  It
//  implements the Printable interface, therefore it must define
//  the print method.  The definition for the name method comes
//  from class File.
//
//  Constructors:
//
//     public Text_File (String id, int size, String file_contents)
//
//  Methods:
//
//     public String print()
//
//-------------------------------------------------------------------

class Text_File extends File implements Printable {

   protected String text;

   //===========================================================
   //  Sets up the object using the specified data.
   //===========================================================
   public Text_File (String id, int size, String file_contents) {
      super(id, size);
      text = file_contents;
   } // constructor Text_File

   //===========================================================
   //  Returns the file contents for printing.
   //===========================================================
   public String print() {
      return text;
   } // method print

} // class Text_File

//-------------------------------------------------------------------
//
//  Class Binary_File represents a file that contains binary data.
//
//  Constructors:
//
//     public Binary_File (String id, int size, byte[] file_data)
//
//-------------------------------------------------------------------

class Binary_File extends File {

   protected byte[] data;

   //===========================================================
   //  Sets up the object using the specified data.
   //===========================================================
   public Binary_File (String id, int size, byte[] file_data) {
      super(id, size);
      data = file_data;
   } // constructor Binary_File

} // class Binary_File


//-------------------------------------------------------------------
//
//  Class Image_File represents a file that contains an image.  It
//  implements the Printable interface, therefore it must define
//  the print method.  The definition for the name method comes
//  from class File.
//
//  Constructors:
//
//     public Image_File (String id, int size, byte[] file_data)
//
//  Methods:
//
//     public String print()
//
//-------------------------------------------------------------------

class Image_File extends Binary_File implements Printable {

   //===========================================================
   //  Sets up the object using the specified data.
   //===========================================================
   public Image_File (String id, int size, byte[] file_data) {
      super(id, size, file_data);
   } // constructor Image_File

   //===========================================================
   //  Returns a string version of the data for printing.
   //===========================================================
   public String print() {
      return new String (data);
   } // method print

} // class Image_File

//-------------------------------------------------------------------
//
//  Interface Printable lists the methods to support printing.
//
//  Methods:
//
//     public String print()
//     public String name()
//
//-------------------------------------------------------------------

interface Printable {

   //===========================================================
   //  Should be defined to return the name of the objects
   //  being printed.
   //===========================================================
   public String name();

   //===========================================================
   //  Should be implemented to return the contents of the
   //  file as a string for printing.
   //===========================================================
   public String print();

} // interface Printable

//-------------------------------------------------------------------
//
//  Class Print_Logger monitors printing activity.
//
//  Methods:
//
//     public void log (File file)
//
//-------------------------------------------------------------------

class Print_Logger {

   //===========================================================
   //  Prints a standard message about printing activity.
   //===========================================================
   public void log (Printable file) {
      System.out.println (file.name() + " : " + file.print());
   } // method log

} // class Print_Logger


