0

I want to book one seat in a cinema hall according to input but when I am trying to do that it changes all the rows.

Booked seat is denoted by 'B'.

Method that changes the state of cinema hall:

public void bookSeat(int row, int seat) {
        this.seatsArrangement.get(row  - 1).set(seat - 1, 'B');
    }

Input and output:

Enter the number of rows:
> 7
Enter the number of seats in each row:
> 8
Cinema:
  1 2 3 4 5 6 7 8 
1 S S S S S S S S 
2 S S S S S S S S 
3 S S S S S S S S 
4 S S S S S S S S 
5 S S S S S S S S 
6 S S S S S S S S 
7 S S S S S S S S 
Enter a row number:
> 2
Enter a seat number in that row:
> 4
Cinema:
  1 2 3 4 5 6 7 8 
1 S S S B S S S S 
2 S S S B S S S S 
3 S S S B S S S S 
4 S S S B S S S S 
5 S S S B S S S S 
6 S S S B S S S S 
7 S S S B S S S S

whole code:

package cinema;
import java.util.*;
public class Cinema {

    private final int rows;
    private final int seats;
    private final List<List<Character>> seatsArrangement = new ArrayList<>();

    public Cinema (int rows, int seats) {
        this.rows = rows;
        this.seats = seats;
        List<Character> rowArrangement = new ArrayList<>();
        while (seats-- != 0) {
            rowArrangement.add('S');
        }
        while (rows-- != 0) {
            seatsArrangement.add(rowArrangement);
        }
    }

    public int calculateProfit() {
        if (this.rows * this.seats <= 60) {
            return this.rows * this.seats * 10;
        } else {
            return (int) ((Math.floor(this.rows / 2.0) * 10 + (this.rows - Math.floor(this.rows / 2.0)) * 8) * this.seats);
        }
    }

    public void showSeatsArrangement() {
        System.out.print("Cinema:\n  ");
        int i = 1;
        while (i <= this.seats) {
            System.out.printf("%d ", i++);
        }
        i = 1;
        for (var row : this.seatsArrangement) {
            System.out.print("\n" + i + " ");
            for (var seat : row) {
                System.out.printf("%c ", seat);
            }
            ++i;
        }
    }

    public void bookSeat(int row, int seat) {
        this.seatsArrangement.get(row  - 1).set(seat - 1, 'B');
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the number of rows:");
        int rows = sc.nextInt();
        System.out.println("Enter the number of seats in each row:");
        int seats = sc.nextInt();

        Cinema cinema = new Cinema(rows, seats);
        cinema.showSeatsArrangement();

        System.out.println("\nEnter a row number:");
        int row = sc.nextInt();
        System.out.println("Enter a seat number in that row:");
        int seat = sc.nextInt();

        cinema.bookSeat(row, seat);
        cinema.showSeatsArrangement();
    }
}
Piyush Keshari
  • 170
  • 2
  • 10

2 Answers2

1

Each element of your seatsArrangement array is the same ArrayList. You're adding the same ArrayList, rowArrangement, to seatsArrangement. To fix this create a copies of rowArrangement in the second while loop. e.g.

        while (rows-- != 0) {
            List<Integer> tmp = new ArrayList<>(rowArrangement);
            seatsArrangement.add(tmp);
        }
MCI
  • 888
  • 1
  • 7
  • 13
0

Thanks @MCI for your help it was a silly mistake.

made few changes in constructor:

original version:

 public Cinema (int rows, int seats) {
        this.rows = rows;
        this.seats = seats;
        List<Character> rowArrangement = new ArrayList<>();
        while (seats-- != 0) {
            rowArrangement.add('S');
        }
        while (rows-- != 0) {
            seatsArrangement.add(rowArrangement);
        }
    }

Working version:

public Cinema(int rows, int seats) {
        this.rows = rows;
        this.seats = seats;
        List<Character> rowArrangement = new ArrayList<>();
        while (seats-- != 0) {
            rowArrangement.add('S');
        }
        while (rows-- != 0) {
            seatsArrangement.add(new ArrayList<>(rowArrangement));
        }
    }

But for some reason rowArrangment.clone() is not working, getting this error

'clone()' has protected access in 'java.lang.Object'

This solved the issue but I have no idea why?

((ArrayList) rowArrangement).clone();

EDIT: Reason why we need to explicitly cast rowArrangment is because clone() method is integral property of ArrayList and since I used List to refer rowArrangement so I can not call clone() method directly on it because upcasting does not support to access methods of subclass.

Piyush Keshari
  • 170
  • 2
  • 10