/************
 == Entering and Leaving the Building ==
 
 Our school has a problem similar to many businesses.
 We worry about people entering (and leaving) the building.
 We hired 4 security guards to keep track of people,
 and we make employees and parents wear ID cards.
 The building is locked on the weekends by a security system,
 which allows the door to open when an ID card is
 held next to a reader at the side of the door.
 The reader is connected to a computer that records
 people entering the building by recording the ID number.
 
 Unfortunately, the measures outlined above are not well
 organized, so we don't actually keep any accurate record
 of people entering and leaving the building.  Students
 enter and leave during the day without any record -
 unless they voluntarily sign in and out at the office,
 but many students don't do this.  Certainly all those
 who enter the building around 8:30 do not sign in.
 
 In the end, we really don't know who is in the building
 during the school day, so if there is a fire (drill), we
 can only count heads on the soccer pitch and hope that
 we notice if someone is missing.  This may be 99% reliable,
 but that means we are wrong about 1 out of 100 students,
 so we probably miss 8 upper school students during a fire drill.
 If a senior has left campus at lunch (this is permitted)
 but did not sign out, and then they don't show up on the
 soccer pitch, we should assume they are still in the building.
 But they might be at the pizzeria.
 
 == A Simple Solution ==

 The security ID cards carried by teachers contain an RFID chip -
 that is a Radio Frequency IDentification chip.  This can respond
 automatically to a monitoring device that constantly sends out
 radio queries.  So if you walk through a gate - similar to
 the security gates you see in stores - it's possible for the
 gate to query your ID card and record that you entered or exited.
 
 We could spend a lot of time worrying about the practical
 details of this system - like do we want to use a gate like
 the one in the library, which would block your entrance if
 you were not carrying an ID card?  But we won't spend time
 on that aspect.  
 
 == Keeping Records ==
 
 We are going to write a very simple program that keeps track
 of people entering and leaving.  We will pretend that it works
 like this:
 
 (1) There is a PC next to the door (instead of ID cards)
     This is a STAND-ALONE, DEDICATED system (only does this)
     
 (2) Each time a person enters or leaves, they stop at the PC
     and type their ID number into the PC. This includes adults.
     
 (3) The computer keeps a list of ID numbers in an array.
 
 (4) The computer can CHECK who has entered and left all day.

 We must design and implement (program) sensible logic for this
 system.  Don't spend time on hardware issues - the people really
 are just going to type in an ID number, nothing else.  We know
 this won't work in reality, but we'll pretend.
 
****************************************/

import java.awt.*;

public class Guard extends EasyApp
{
   public static void main(String[] args)
   {  new Guard(); }
   
   int[] id = new int[1000000]; // hopefully a million is enough
   int last = 0;   
   
   Button bEnter = addButton("Enter",50,50,200,100,this);
   Button bLeave = addButton("Leave",150,150,200,100,this);
   Button bCheck = addButton("Check",250,250,200,100,this);
   
   public Guard()
   {  
      Font big = new Font("Arial",0,36);
      bEnter.setFont(big);
      bLeave.setFont(big);
      bCheck.setFont(big);
      setTitle("GUARD");
   }
   
   public void actions(Object source,String command)
   {
      if (source == bEnter)
      {  enter(); }   
      else if (source == bLeave)
      {  leave(); }   
      else if (source == bCheck)
      {  check(); }
   }

   public void enter()
   {      
      int num = inputInt("ID number:");
      while (num == 0 || num > 9999 || num <= 999)
      {
         output("Error");
         num = inputInt("ID number:");
      }
       
      id[last] = num;
      last = last + 1;
   }   
   
   public void leave()
   {
      int num = inputInt("ID number:");  // 456
      id[last] = -num;
      last = last + 1;
   }
         
   public void check()
   {
      for (int x = 0; x < last; x = x+1)
      {  output(x + " : " + id[x]); }
   }
   
   //  id[0] = 123
   //  id[1] = 456
   //  id[2] = 789
   //  id[3] = -456
   //
   //  Leave
   //   id number? cannot be blank
   //   REMOVE 456
     
}
/****************************
 The STUB program (above) implements the ENTER and CHECK features.
 ENTER adds an ID number to the list (and increments LAST).
 CHECK is to be used by the office to see who is in school.
 
 A STUB program is a VERY SIMPLE, INCOMPLETE version.
 Each piece is present, but only VERY SIMPLE VERSIONS.
 The LEAVE method has not been written at all - it must be added.

 Programmers start with a STUB version so it's clear in their
 minds what they still need to do.  Then they can concentrate
 on individual MODULES (pieces), without being distracted
 by the "big picture" of the entire application.  Now you need
 to add the missing pieces, as well as improving the STUBs.
 
 (1) Class Exercise
     With teacher guidance, discuss what the LEAVE method
     should do. There are several possibilities:
     (a) erase the matching ENTER code
     (b) add the ID to the list, but make it negative
     (c) many other possibilities.
     
 (2) IMPLEMENT (program) the ALGORITHM that you agreed on (#1)
 
 (3) Add some ERROR-HANDLING code. Here are some possible errors:
     (a) the student makes a mistake and types an INVALID ID -
         for example, 12234 instead of 1234.
 
     (b) the student presses [Enter] WITHOUT typing their ID -
         this records 0 (zero) in the list
     
     (c) the student types something that is not a number,
         like their name.  This enters a 0 in the list.
     
     (d) the student types the WRONG ID, like 1243 instead of 1234
         It's probably not possible to fix this, but you could
         think about how it might be possible.

 (4) CHECK should be a bit smarter than just typing out all
     the ID numbers.  Assuming all the students entered
     correct numbers, how could the vice-principal check
     which students are still in the building?  IMPLEMENT
     an AUTOMATED ALGORITHM to do exactly that.
     
 (5) There are several reasons to SEARCH for an ID number in
     the id[] array:
     (a) when LEAVING, check that the same ID is already in the list.
     (b) when arriving, check that IF this ID is already in the list,
         that there is also a matching leaving ID (e.g. -ID).
     (c) during a fire-drill, if a student is reported missing,
         search the list for all the times that ID occurs, to see
         whether he already left the building (e.g. went home early)

     Write a SEARCH method that accepts an ID number as a parameter,
     then searches the list for that ID and prints it out every time
     it is found, like this:
     
            pos 327 : 1234
            pos 478 : -1234
            pos 1501: 1234
            
     Notice that it must print the POSITION in the list where the
     ID was found, and it must also find the negative entries.
         
*****************************/