[Solved] Ticket Booking with Java

In our application, booking and cancelling tickets are some important features. As every user uses it to book/cancel tickets for every event/shows/fair. So we need to make it as quick and safe as possible. We are gonna use the synchronized method in this threading problem to get the First-come First-served basis. When one booking is done another will wait due to the synchronized method. Thus booking/cancellation can be done without much confusion.

Strictly adhere to the Object-Oriented specifications given in the problem statement. All class names, attribute names and method names should be the same as specified in the problem statement.

Create a class Seat with the following private attributes,

AttributeDatatype
seatNumberInteger
bookedBoolean


Create default constructor and a parameterized constructor with arguments in order Seat(Integer seatNumber, Boolean booked).
Create appropriate getters and setters.


Create a class TicketBookingThread which extends Thread with the following private attributes,

AttributeDatatype
startingSeatInteger
endingSeatInteger
isBookingBoolean


Create appropriate getters and setters.
Create default constructor and a parameterized constructor with arguments in order
TicketBookingThread(Integer startingSeat, Integer endingSeat, Boolean isBooking, Runnable target).


Create a class Booking which implements Runnable with the following attributes,

AttributeDatatype
private List<Seat> list;List<Seat>

Create appropriate getters and setters.

Create the following methods in Booking class.

MethodDescription
public void run()This function runs the thread and calls the doBooking() 
method each time. It displays the statements of acknowledgement.
public synchronized Boolean doBooking(Integer startingSeat, Integer endingSeat,Boolean isBooking)This method checks the availability of seats and book/cancel them.
After successful booking/cancellation, it returns true,
else if the ticket to be booked is not available or
the ticket to be cancelled is not done at all, then it returns false.


Note: Print “The seats (startingSeat)-(endingSeat) are booked” for successful booking and “The seats (startingSeat)-(endingSeat) are cancelled” for successful cancellation. And in case of failure to book/cancel print “The booking/cancellation cannot be done”.

Print these acknowledgements in the run() method.

Input Format:

The input for Booking tickets should be given as startingSeat,endingSeat, Booking.
Example: 
45,67,Booking – for booking all tickets from 45 to 67.
The input for cancellation startingSeat,endingSeat, Cancellation.
Example:
56,60,Booking  – to cancel all tickets from 56 to 60.

Output Format:

The available seats are shown as a tabular form with 10 seats in each row.
Booked seats are represented by “*” and for the available seats, the seat numbers are shown.
Leave a  space between all the seats.
Refer sample Input and Output for formatting specifications.

Create a driver class Main and use the main method for getting inputs and displaying the available seats at the last.

Sample Input and Output 1:
[All Texts in bold corresponds to the input and rest are output]


The available seats are:
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 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
Enter the number of Bookings:
4
2,96,Booking
The seats 2-96 are booked
95,97,Cancellation
The booking/cancellation cannot be done
3,7,Cancellation
The seats 3-7 are cancelled
4,6,Booking
The seats 4-6 are booked
The available seats are:
1 * 3 * * * 7 * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * * * * 97 98 99 100

Solution

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        //ArrayList<Integer> al=new ArrayList<>();
        TreeSet<Integer> al= new TreeSet<Integer>();
        System.out.println("The available seats are:");
        for(int i=0;i<100;i++){
            int s=i;
            al.add(s+1);
            if((s+1)%10==0){
                System.out.println((s+1)+" ");
            }else{
                System.out.print((s+1)+" ");
            }
            
        }
        
        System.out.println("Enter the number of Bookings:");
        String num=sc.nextLine();
        Integer n=Integer.parseInt(num);
        for(int j=0;j<n;j++){
            String temp=sc.nextLine();
            String[] arr=temp.split(",");
            int st=Integer.parseInt(arr[0]);
            int end=Integer.parseInt(arr[1]);
            String stat=arr[2];
            boolean t1=al.contains(st);
            boolean t2=al.contains(end);

            
            if(al.contains(st) && al.contains(end) && stat.equals("Booking")){
                System.out.println("The seats "+st+"-"+end+" are booked");
                int st1=st-1;
                int end1=end-1;
                for(int k=0;k<100;k++){
                    if(k>=st1 && k<=end1){
                        al.remove(k+1);
                    }
                }
            }
            else if(t1==false && t2==false && stat.equals("Cancellation")){
                System.out.println("The seats "+st+"-"+end+" are cancelled");
                int st1=st-1;
                int end1=end-1;
                for(int k=0;k<100;k++){
                    if(k>=st1 && k<=end1){
                        al.add((k+1));
                    }
                }
            }
            else{
                System.out.println("The booking/cancellation cannot be done");
                
            }
        }
        //System.out.println(al);
        System.out.println("The available seats are:");
        for(int i=0;i<100;i++){
            int s=i;
            boolean count=al.contains(s+1);
            //al.add(s+1);
            if((s+1)%10==0 && al.contains(s+1)){
                System.out.println((s+1));
            }
            else if((s+1)%10!=0 && al.contains(s+1)){
                System.out.print((s+1)+" ");
            }
            else if((s+1)%10==0 && count==false){
                System.out.println("*");
            }
            else if((s+1)%10!=0 && count==false){
                System.out.print("*");
            }
        }
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *