// Computer Science Example
// Reading from a file into an array of classes.

import java.io.*;

/* TestFiles reads data from a file and stores it in an array of classes.   Each name and integer is stored in an instance of the DataItem class.  These are then stored in successive locations in the array managed by the ListManager class. */
public class TestFiles
{
     public static void main (String [] args)
     {
          ListManager reader = new ListManager ();
          reader.readData ();
          reader.displayData ();
     } // main method
}// class TestFiles

// The DataItem class stores a single name and item.
class DataItem
{
     private String name;
     private int item;
 
     public void readItem (BufferedReader infile, String n) throws IOException
     {
          name = n;
          item = Integer.parseInt (infile.readLine ());
     } // readItem
 
     public void displayItem ()
     {
          System.out.println (name + "'s score is " + item);
     } // displayItem
} // class DataItem

// The ListManger class instantiates an array that can hold instances of the DataItem class.
class ListManager
{
     private DataItem [] dataList;
     private int listSize;
     final int maxSize = 40;
 
     ListManager ()
     {
          dataList = new DataItem [maxSize];
          listSize = 0;
     } // constructor
 
     public void readData ()
     {
          String name;
          int item;
          DataItem dataItem;
          try
          {
               BufferedReader infile = new BufferedReader (new InputStreamReader (new FileInputStream ("scoreFile.txt")));
               name = infile.readLine ();
               while ((name != null) && (listSize < maxSize))
               {
                    dataItem = new DataItem ();
                    dataItem.readItem (infile, name);
                    dataList [listSize] = dataItem;
                    listSize ++;
                    name = infile.readLine ();
               } // while
               infile.close ();
              } catch (IOException e) {System.out.println ("File not found");}
              catch (NumberFormatException e) {System.out.println ("Data Error");}
     } // method readData
 
     public void displayData ()
     {
          for (int count = 0; count < listSize; count ++)
               dataList [count].displayItem ();
     } // method displayData
} // class ListManager