Wednesday 18 March 2015

Nagarro : Theatre Seat Planing


Question :Write a function for seat allocate and seat reserved.Seat allocate array and seat reserver array.Seat allocate array is of 10*20 and each row and column represent A1 ,A2....;B1 ,B2.....;........J1 ,J2... and so on i.e row are A to J whereas col starts from 0 to 1 9.Each cell in the table represent either 0 or 1 . 0 rep seat available , 1 rep  seat reserved. Seat allocation starts from highest to lowest.And row j is highest, i is second highest and so on.Max 20 seats can be booked at a time. 

if seat is available print the seat no like "B2" i.e (2 row, 3 col)and seat is booked." otherwise Print "Seat is not available."




package dailyPuzzle;

import java.io.ObjectInputStream.GetField;
import java.security.AllPermission;
import java.util.Scanner;

public class Theatre {
 static byte[][] seats;
 static Theatre theatre = null;

 static private Theatre getThreatre(int row, int each_row) {
  if (theatre == null)
   theatre = new Theatre();
  seats = new byte[row][each_row];
  return theatre;

 }

 @Override
 public String toString() {
  System.out.println("Threatre View");
  int i = 0;
  for (i = 0; i < seats.length; i++) {
   for (int j = 0; j < seats[0].length; j++) {
    System.out.printf("%3s", seats[i][j]);
   }
   System.out.println();
  }
  i = 0;
  for (; i <= 19; i++)
   System.out.printf("%3s", i);
  return "";
 }

 public static void main(String str[]) {
  int row = 10, row_each = 20;
  Theatre theatre = getThreatre(row, row_each);
  System.out.print("Welcome to Threatre ");
  System.out.println("Enter Number of seat you want to book.");
  Scanner in = new Scanner(System.in);
  int i = in.nextInt();

  i--;
  int j = 0;
  int available = 0;
  int r = 0;
  boolean allocateflag = true;
  int remainingSeat = 0;

  while (r >= 0 && r < 20 && i > 0) {
   if (i >= 20) {

    remainingSeat = i - 20;
    i = 19;
    allocateflag = false;
   }
   j = 0;
   while (j >= 0 && j < 20 && i > 0) {
    if (seats[r][j] == 1) 
    {
      j++;
      continue;
    }
    else 
    {
     available = row_each - j;
     if (available <= i) 
     { break;
     }
     else 
     {
      for (; i >= 0; i--) 
      {
       seats[r][j] = 1;
       available--;
       j++;
      }
      break;
     }
    }

   }
   r++;
   if (i == -1 && allocateflag) 
   {
    System.out.println("Enter more number of seats and -1 for exit");
    i = in.nextInt();
    i--;

   } else if (!allocateflag && i == -1) {
    i = remainingSeat;
    if (i < 20)
     allocateflag = true;

   }

  }
  System.out.println(theatre);

 }

}

OUTPUT:
Welcome to Threatre Enter Number of seat you want to book.
24
Enter more number of seats and -1 for exit
-1
Threatre View
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19