import java.util.Random;

public class Agent2 extends Thread {
  private String name;
  private Flight2 flight;

  public Agent2(String name, Flight2 flight) {
    this.name = name;      
    this.flight = flight;
  }

  public void run()  {
    Random rand = new Random();
    while(true) {                          // Loop indefinitely...
      try {
        sleep(rand.nextInt(2));              // sleep for a random time
        int seatNum = rand.nextInt(12);
        String availability = (flight.isSeatFree(seatNum) == 0) ?
          "free" : "not free";
        System.out.println("Agent " + name + " says seat number " + seatNum +
          " is " + availability);  
        flight.reserveSeat(seatNum);
      } catch(InterruptedException e) {
        System.out.println(e);
      }
    }
  }
}
