Check-Out List - Array of Objects

  Download Source Code

/*********************************************************
Susie is a school secretary. She keeps track of students and teachers
leaving school temporarily - to visit a doctor, a shop, etc.
She calls the list of missing people a "trip list".
She writes names and times on paper.  Sometimes a parent or friend
calls the school and wishes to contact someone.  If that person is
out on a trip, Susie wants to know this quickly.  So she wants
a computer program that will look this up quickly.  This program uses
an ARRAY of OBJECTS to store the information about each trip.
Each OBJECT contains the name, times and reason for one trip.
At the end of the day, the program prints a list of all the trips.
**********************************************************/


class Trip        // stores several pieces of data for one trip
{
  String name = "";
  String reason = "";
  String leaveTime = "";
  String returnTime = "";
}

public class TripList
{
  Trip[] list = new Trip[1000]; // list of check-outs
  int count = 0;                // number of trips recorded in the list
 
  public TripList()
  {
    String person = "";
    do
    {
      person = input("Name");
     
      int found = -1;
      for(int c=0; c < count; c = c+1)
      {
        if(list[c].name.equals(person))
        {
          found = c;         
        }
      }
      if(found >= 0)               // this person was already signed out
      {
        output(person + "\n left at " + list[found].leaveTime
                 + "\n and returned at " + list[found].returnTime
                 + "\n to " + list[found].reason);
        String now = input("Return time (blank if just checking)");
        list[found].returnTime = now;
      }
      else                         // this is a new check-out
      {
        Trip t = new Trip();
        t.name = person;
        t.leaveTime = input("Time now");
        t.reason = input("Reason");
        list[count] = t;                 // add new Trip to the list
        count = count + 1;               // count 1 more Trip
      }  
    } while(!person.equals("quit"));     // type "quit" to end the program
   
    for(int c=0; c < count-1; c = c+1)   // print the entire list
    {
      System.out.println(list[c].name);
      System.out.println("  left at " + list[c].leaveTime);
      System.out.println("  returned at " + list[c].returnTime);
      System.out.println("  to " + list[c].reason);
    }
  }
 
  public static void main(String[] args)
  {  new TripList(); }
 
  public String input(String prompt)
  { return javax.swing.JOptionPane.showInputDialog(null,prompt); }
 
  public void output(String message)
  {  javax.swing.JOptionPane.showMessageDialog(null,message);  }
}


Anna
  left at 09.15
  returned at 11.30
  to visit the doctor
Bob
  left at 09.45
  returned at
  to go home sick
Mr T
  left at 12.00
  returned at 13.00
  to get lunch at Macs

Data Objects

A data object is a collection of data items that are grouped together. 
In this program, a Trip is a data object containing

      name  ,  reason  ,  leaveTime  ,  returnTime.

To create a data object, we write a class.  This program begins with the Trip class, which contains the 4 data fields.
Then the main program uses the command new Trip to create a data object.  Since there are many Trips stored
int he list array, the command new Trip is executed each time that a person checks out to leave school.

Using a data object makes it easy to encapsulate several pieces of data and to save the data or move it around as
a single record.  The term record is the database term for a group of connected data items.  In a spreadsheet,
a data object would occupy one row, and the various data fields would be in the various columns in that row.

Dot Notation

To access individual pieces of data, we use dot notation.  For example, the following code puts data into
a new object for the person who is checking out:

    Trip t = new Trip();
    t.name = person;
    t.leaveTime = input("Time now");
    t.reason = input("Reason");

Since the Trip objects are stored in an array, we also use [index] along with .dot notation, like this to print all data:

    for(int c=0; c < count-1; c=c+1)
    { 
        println( list[c].name );
        println( list[c].reason );
        println( list[c].leaveTime );
        println( list[c].returnTime );
    }

Each time a name is typed in, the program searches through the array to see whether a record already exists
for that person - e.g. they have already signed out:

    found = -1;
    for(int c=0; c < count; c = c+1)
    {
       if(list[c].name.equals(person))
       {
           found = c;
       }
    }

This loop searches through the array objects, trying to find a name that matches the person's name.
It uses the flag found to keep record the position of the record if it is found.  If the name is found,
it attempts to input a returnTime and to copy that into the existing record.  Otherwise found is -1,
indicating that no record for this person was found.

The if command above may be a bit confusing - here is a clarification:

    if(list[c].name.equals(person))

       list[c] ==> look at the object in position c
        .name  ==> look at the name in the record at position c
       .equals ==> compare the name to (person)

The fancy name for dot notation is dereference.  Each subsequent .dot dereferences to the next item
inside the previous item.  This extended dot notation requires some practice to acquire competence.

Programming Practice