Ticket Reservation for a set of 4 busses. 1. Admin should be able to reserve a s
ID: 3529403 • Letter: T
Question
Ticket Reservation for a set of 4 busses. 1. Admin should be able to reserve a seat in any of the 4 buses for today and tomorrow. 2. Admin should be able to cancel an alloted seat for today and tomorrow. Program should ask user for the seat preference.A bus can contain min 10 seats. For cancellation it should display all booked ticket and then a particular ticket can be cancelled. Program should be menu driven like.. Book Ticket Cancel Ticket Show Ticket Hint Create 2 class. Bus and TicketReservationExplanation / Answer
Below is the working code. I have checked it in eclipse Hope this help
---------------------------------
TicketReservation.java
-----------------------------
import java.text.*;
import java.util.*;
public class TicketReservation {
Scanner scan=new Scanner(System.in);
ArrayList<Bus> ticketList =new ArrayList<Bus>();
SimpleDateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy");
Date newDate=new Date();
int sec=1000*24*60*60;
String today=dateFormat.format(newDate.getTime());
String tomorrow=dateFormat.format(newDate.getTime()+sec);
public void initialize()
{
for(int j=1;j<=4;j++)
{
for(int k=1;k<=10;k++)
{
Bus b=new Bus(j,k,today,true) ;
ticketList.add(b);
}
}
for(int j=1;j<=4;j++)
{
for(int k=1;k<=10;k++)
{
Bus b=new Bus(j,k,tomorrow,true) ;
ticketList.add(b);
}
}
}
public void showMenu()
{
try{
System.out.println("Menu");
System.out.println("1.Book Ticket");
System.out.println("2.Cancel Tciket");
System.out.println("3.booked status");
System.out.println("enter your choice");
int choice=Integer.parseInt(scan.nextLine());
processMenu(choice);
}
catch(Exception e)
{
System.out.println("oops something went wrong!!");
}
}
public void processMenu(int choice)
{
switch(choice)
{
case 1:
bookTicket();
break;
case 2:
cancelTicket();
break;
case 3:
bookedStatus();
break;
default:
System.out.println("wrong choice");
}
}
public void bookTicket()
{
System.out.println("book your ticket...");
System.out.println("select date the date(1/2)");
System.out.println("1. "+today);
System.out.println("2. "+tomorrow);
int d=Integer.parseInt(scan.nextLine());
String date="";
if(d==1)
date=today;
else
date=tomorrow;
System.out.println("enter bus no (1/2/3/4):");
int busNo=Integer.parseInt(scan.nextLine());
if(busNo>4||busNo<1){
System.out.println("choose the correct bus number !");
showMenu();
}
else
{
System.out.println("Available seats are..");
bookedStatus(busNo,date,true);
System.out.println("");
System.out.println("enter your seat number to book:");
int seatNo=Integer.parseInt(scan.nextLine());
for(int i=0;i<ticketList.size();i++)
{
Bus b=null;
b=ticketList.get(i);
if(b.busNo==busNo&& b.available==true && b.date.equals(date) && b.seatNo==seatNo)
{
b.available=false;
ticketList.remove(i);
ticketList.add(i,b);
System.out.println("successfully booked !");
showMenu();
}
}
}
}
public void cancelTicket()
{
System.out.println("cancel the ticket...");
System.out.println("select date the date(1/2)");
System.out.println("1. "+today);
System.out.println("2. "+tomorrow);
int d=Integer.parseInt(scan.nextLine());
String date="";
if(d==1)
date=today;
else
date=tomorrow;
System.out.println("enter bus no (1/2/3/4):");
int busNo=Integer.parseInt(scan.nextLine());
if(busNo>4||busNo<1){
System.out.println("choose the correct bus number !");
showMenu();
}
else
{
System.out.println("Booked seats are..");
bookedStatus(busNo,date,false);
System.out.println("");
System.out.println("enter your seat number to book:");
int seatNo=Integer.parseInt(scan.nextLine());
for(int i=0;i<ticketList.size();i++)
{
Bus b=null;
b=ticketList.get(i);
if(b.busNo==busNo&& b.available==false && b.date.equals(date) && b.seatNo==seatNo)
{
b.available=true;
ticketList.add(i,b);
System.out.println("successfully cancelled !");
showMenu();
}
}
}
}
public void bookedStatus(int busNo,String date,boolean status){
for(int i=0;i<ticketList.size();i++)
{
Bus b=null;
b=ticketList.get(i);
if(b.busNo==busNo&& b.available==status && b.date.equals(date))
{
System.out.print(b.seatNo+" ");
}
}
}
public static void main(String[] args)
{
TicketReservation t=new TicketReservation();
t.initialize();
t.showMenu();
}
}