/**--- Zonkers Text File ---------------------------------------------**\
In the beginning, Zonker put all his customer information
into a SERIAL file - that means it is a simple text file,
created with Notepad, and the records are not in alphabetical order.
Later, Zonker sorted the records in his text file, thus changing
it to a SEQUENTIAL file.

The program SERIAL.JAVA (below) shows the basic commands and techniques
for reading a text file in Java.

(1) Create a text file named "zonker.txt" containing 10 records:
      aaa
      bbb
      ccc
      ...
      jjj
    The file must be stored in the same folder (directory) as the program.

(2) Run the program and check that it compiles correctly,
    and that each method gives correct results. If not fix any errors.

(3) Create a new sample file containing customer data, e.g.:
       Allen Allstar
       AAllstar@hotmail.com
       AAAAAA
       Tommy Lee Tunes
       TomTune27@aol.com
       a1b2c3xxx
       Carla Charles
       TooCool@yahoo.com
       sekret
        ....
        ....
        ....
       Bob Baker
       BB@hotmail.com
       BeeBee
    Notice there are 3 fields in each record:  Name, E-mail, Password
    The records are not sorted.  You should have 10 records.
    Be careful that the file contains EXACTLY 30 lines (fields).

(4) Change the SERIAL program so that it works correctly for reading
    the new Zonker.txt file.  It will need extra "readLine" commands.
    The CheckSorted method should check whether the NAMES are sorted -
    it should not compare a name to an E-mail address.

(5) Using Notepad, sort the records in the text file, putting names
    in alphabetical order.  Notice this means the first names are sorted.

(6) Check that the CheckSorted routine now works correctly.

(7) Change CheckSorted so that it is not case-sensitive -
    before comparing two names, use "toUpperCase" to change them
    both to capital letters.  Notice this does not change the data
    in the file, and you should not use Notepad to change the data file.

(8) Change the Search method so that it stops early
    if a name is not found.  That means if it is searching for "Cherk",
    then it can quit searching (returning "not found") as soon as it
    reaches a name starting with "D" or any other letter after "C".

(9) Write a new method which searches the SEQUENTIAL file for DUPLICATE
    names.  It should output a list of all the duplicate names.
    To test the method, you may wish to add some duplicate names
    to the file. This method should be non-case-sensitive.


\**--------------------------------------------------------------------**/
/**--- SERIAL.JAVA ---------------------------------------**\
    Reads the contents of a Serial file.
    Serial means it is a text file, read one line at a time,
    and it is not sorted.
\**-------------------------------------------------------**/

   import java.awt.*;
   import java.awt.event.*;
   import java.io.*;

   public class Serial extends EasyApp  
   {
      public static void main(String[] args)
      { new Serial(); }

/**--- Controls - added to form ------**/
      Button bDisplay = addButton("Display",40,50,70,20,this);
      List lData = addList("",40,70,150,300,this);
      Button bQuit = addButton("Quit",120,50,70,20,this);
      Button bSearch = addButton("Search",200,50,70,20,this);
      Button bCheckSort = addButton("Check Sorted",200,100,90,30,this);

/**--- Constructor - Adjust controls at start  ---**/
      public Serial()
      { setVisible(true);
      }

/**--- Perform actions when buttons are clicked --**/
      public void actions(Object source,String command)
      {
         if (source == bQuit)
         { System.exit(0);
         }
         else if (source == bSearch)
         { String target = input("What do you want to find?");
           String result = search(target);
           output( result );
         }
         else if (source == bDisplay)
         { ShowFile();
         }
         else if (source == bCheckSort)
         { boolean check = checkSort();
           output("Sorted = " + check);
         }
      }

/**--- ShowFile - Reads file and displays all data in a list box ---**/
      public void ShowFile()
      {
         try
         {  BufferedReader inputs =
                  new BufferedReader(new FileReader("zonker.txt"));
            String data = "";
            while ( inputs.ready() )
            { data = inputs.readLine();
              lData.add(data);
            }
            inputs.close();
         }
         catch (IOException e) { output("Error during file access");}
      }

/**--- Search -------------------------------------------------**\
   Sequential Search
     Reads each line of the text file, searching for TARGET.
     Returns one of the following:
         "Found"
         "Not Found"
         "Error"
\**------------------------------------------------------------**/

      public String search(String target)
      {
         try
         {  BufferedReader textfile =
                  new BufferedReader(new FileReader("zonker.txt"));
                                          // opens the file
            String data = "";
            while ( textfile.ready() )    // checks if there is more data
            { data = textfile.readLine(); // reads one line of data
              if (data.equals(target))    // does this match the target?
              { return "Found"; }         // if so, quit
            }
            textfile.close();             // closes the file
            return "Not found";           // only gets here (end of file)
         }                                //  if target is not found
         catch (IOException e)
         { return "Error"; }              // happens if an error occured
      }                                   //  e.g. the file is missing

/**--- CheckSort ---------------------------------------------**\
   Checks whether the text file is sorted or not.
   This is done by comparing each data line to the previous.
   If it ever finds a place where the current data is smaller
    than the previous data, then the file is not sorted.
   Otherwise, the file is sorted.
   In the following example, it would quit at position 3, returning FALSE:
      aaa
      ccc
      bbb
      ddd
      eee

   If it IS sorted, then it is actually a SEQUENTIAL file.
\**-----------------------------------------------------------**/
      public boolean checkSort()
      {
        try
         {  BufferedReader file =
                   new BufferedReader(new FileReader("zonker.txt"));

            boolean sorted = true;      // is okay until mistake is found
            String thisData = "";
            String previous = "";

            previous = file.readLine();
            while ( file.ready() )
            { thisData = file.readLine();
              if ( thisData.compareTo(previous) < 0 )   // out of order
              { sorted = false;
              }
              previous = thisData;
            }
            file.close();
            return sorted;
         }
         catch (IOException e)
         { output("Error during file access");
           return false;
         }
      }
   }