Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Shavit Borisov
- // CW
- import java.util.Scanner;
- public class ElAl {
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
- System.out.printf("How many rows does the plane have? ");
- int rows = in.nextInt();
- System.out.printf("How many seats are in each row? ");
- int seats = in.nextInt();
- Airplane Boing = new Airplane(rows, seats);
- System.out.printf("Enter the seat you would like to reserve: ");
- int row = in.nextInt();
- char seat = in.next().charAt(0);
- while(!(row == 1 && seat == 'a'))
- {
- System.out.printf("%s", Boing.reserveSeat(row, seat) ? "Success\n" : "Taken\n");
- Boing.currentState();
- System.out.printf("Enter the next seat you would like to reserve: ");
- row = in.nextInt();
- seat = in.next().charAt(0);
- }
- Boing.reserveSeat(row, seat);
- System.out.printf("Procedure complete. Final state:\n");
- Boing.currentState();
- in.close();
- }
- }
- // Next class
- public class Airplane
- {
- boolean[][] seats;
- public Airplane(int rows, int spots)
- {
- seats = new boolean[rows][spots];
- for(int i = 0; i < rows; i++)
- for(int j = 0; j < spots; j++)
- seats[i][j] = true;
- }
- public boolean reserveSeat(int row, char spot)
- {
- boolean success = true;
- if(seats[row - 1][spot - 'a'])
- seats[row - 1][spot - 'a'] = false;
- else
- success = false;
- return success;
- }
- public void currentState()
- {
- for(int i = 0; i < seats.length; i++)
- {
- for(int j = 0; j < seats[i].length; j++)
- System.out.printf("[%c] ", seats[i][j] ? 'A' : 'N');
- System.out.printf("\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement