Reservations.java 1.09 KB
Newer Older
Michele Fiori's avatar
Michele Fiori committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
package theatre;

public class Reservations {
    int totalSeats = 10;
    int reservedSeats = 8;

    public synchronized int checkFreeSeats(){
        if(totalSeats - reservedSeats > 0){ //if there are free seats
            System.out.println("reserved = " + reservedSeats);
            return reservedSeats;   //return the number of reserved seats
        }else{
            return 0;
        }
    }

    //the integer represent the ith seat taken. if it is -1, the ticket was not bought.
    public synchronized int buyTicket() {
            //if there are free seats...
            if(checkFreeSeats() > 0){

                //Simulate some processing time
                /*
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }*/

                reservedSeats++;
                System.out.println("Updated to: " + reservedSeats);
                return reservedSeats;
            }else{
                return -1;
            }
    }
}