Advertisement
Shavit

P. 122 Ex. 12.4

Mar 16th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. // Shavit Borisov
  2. // CW
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class ElAl {
  7.  
  8.     public static void main(String[] args)
  9.     {
  10.         Scanner in = new Scanner(System.in);
  11.        
  12.         System.out.printf("How many rows does the plane have? ");
  13.         int rows = in.nextInt();
  14.        
  15.         System.out.printf("How many seats are in each row? ");
  16.         int seats = in.nextInt();
  17.        
  18.         Airplane Boing = new Airplane(rows, seats);
  19.        
  20.         System.out.printf("Enter the seat you would like to reserve: ");
  21.         int row = in.nextInt();
  22.         char seat = in.next().charAt(0);
  23.        
  24.         while(!(row == 1 && seat == 'a'))
  25.         {
  26.             System.out.printf("%s", Boing.reserveSeat(row, seat) ? "Success\n" : "Taken\n");
  27.            
  28.             Boing.currentState();
  29.            
  30.             System.out.printf("Enter the next seat you would like to reserve: ");
  31.             row = in.nextInt();
  32.             seat = in.next().charAt(0);
  33.         }
  34.        
  35.         Boing.reserveSeat(row, seat);
  36.         System.out.printf("Procedure complete. Final state:\n");
  37.         Boing.currentState();
  38.        
  39.         in.close();
  40.     }
  41. }
  42.  
  43. // Next class
  44.  
  45. public class Airplane
  46. {
  47.     boolean[][] seats;
  48.    
  49.     public Airplane(int rows, int spots)
  50.     {
  51.         seats = new boolean[rows][spots];
  52.         for(int i = 0; i < rows; i++)
  53.             for(int j = 0; j < spots; j++)
  54.                 seats[i][j] = true;
  55.     }
  56.    
  57.     public boolean reserveSeat(int row, char spot)
  58.     {
  59.         boolean success = true;
  60.         if(seats[row - 1][spot - 'a'])
  61.             seats[row - 1][spot - 'a'] = false;
  62.         else
  63.             success = false;
  64.         return success;
  65.     }
  66.    
  67.     public void currentState()
  68.     {
  69.         for(int i = 0; i < seats.length; i++)
  70.         {
  71.             for(int j = 0; j < seats[i].length; j++)
  72.                 System.out.printf("[%c] ", seats[i][j] ? 'A' : 'N');
  73.             System.out.printf("\n");
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement