/* Seating for an auditorium for a single performance */
public class Seating {
  private static final int ROWS = 25; // constant used to define rows  and cols
  private static final int SEATS = 20;
  private static int allSeatsSold; // this is a class variable (field) used to accrue
                                   // the total number of seats sold for all Seating
                                   // objects.
  private int seatsSold;           // an instance variable used to accrue the total
                                   // number of seats for this Seating object.
  private boolean [][] seats;      // The array holding the seats in the auditorium.
                                   // Each seat is initially set to false: this means
                                   // that the seat has not been sold.  When the seat
                                   // is sold, it is set to true.
                                   // This is an array declaration, the value of seats
                                   // will remain null until the new command is issued
                                   // which actually creates the array of boolean values.
  
  /** construct a seating object, initializing all seats to unsold. */
  public Seating() {
    seats = new boolean[ROWS][SEATS];
  }  
  
  /** Find a contiguous string of num unsold seats in a row starting from the
   *  first row.  If the number specified in num is not found, then return 
   *  "num seats not available together for this performance", otherwise,
   *  return a string consisting of the starting row and column of the num seats.
   * @param num the number of seats together that are required.
   * @return the row and column of the first seat that satisfies the condition or 
   *  a message indicating that they are not available. 
   */
  public String findSeats (int num) { 
    String result;
    int seatsFound = 0;
    int row = 0, seat = 0;
    // here we walk each in the auditorium.  Since the auditorium is an 
    // array of rows, and each row is an array of
    // seats (or cols), our first array is rows, from row 0 ... thru row 24,
    // and we can determine the number of rows using the .length of the 
    // first dimension.
    for ( row = 0; row < seats.length && seatsFound < num; row++) {
      // The seats (or cols) in each row, represent the second dimension.
      // To determine how many seats in a row (i.e., the length of a row), we
      // must first specify the row, and then use the .length - so
      // seats[row].length looks at the length of the array of seats in row "row".
      
      if (seatsFound < num) seatsFound = 0; // reset the number of seats found 
                                            // before looking in new row.
      
      for ( seat = 0; seat < seats[row].length && seatsFound < num; seat++) {
         if (seats[row][seat]) { // this is a boolean value that will be true if 
                                // the seat is sold.
          seatsFound = 0;
        } else {
          seatsFound++;
        }
      }
    }
    if (seatsFound < num )
      result = "num seats not available together for this performance";
    else
      result = row + "," + (seat - num + 1); // add 1 because the seats in the
                                             // array start with zero, but people
                                             // generally start counting seats with 
                                             // one.
    return result;
  }
  
  /** Sell a seat in the auditorium.  Increment all totals to reflect the sale.
   * @param row the row in in which the seat is sold.  In the mind of the buyer,
   *  rows start with row 1.
   * @param seat the specific seat number in the row to be sold. In the mind of
   *  the buyer, seats in a row are numbered starting with a 1.
   * @return true if the sale is successful, false if the seat is not 
   *  available for sale.
   */
  public boolean sellSeat(int row, int seat) {
    row--; 
    seat--;
                                             // Note: Java performs lazy
                                             // evaluation on the following compound
                                             // condition. The expression
                                             // is evaluated from left to right.
                                             // therefore, if the seats requested are
                                             // outside the array range, the expression
                                             // will evaluate to true and further 
                                             // evaluation will stop before an
                                             // out-of-bounds array access is performed.
    if (row < 0 || seat < 0                  // either an invalid seat number has
          || row > ROWS || seat >SEATS       // been submitted, or 
          || seats[row][seat]) return false; // this seat is not available for sale.
    this.seatsSold ++; // increment the cumulators
    allSeatsSold++;
    seats[row][seat] = true; // flag the seat as sold
    return true;
    }
  
  /** return a count of all the seats sold for this performance.
   * @return the total number of seats sold for this performance.
   */
  public int getSeatsSold() {
    return this.seatsSold;
  }
  
  /** return a count of all of the seats that have ever been sold. 
   * @return the total number of seats sold ever.
   */
  public static int getAllSeatSold() {
    return allSeatsSold;
  }
    
    
      
}