import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner scan =new Scanner(System.in);
boolean loop =true;
while(loop)
{
System.out.println(" 1. Book Ticket \n 2. Available Tickets \n 4. Booked Tickets \n 5. Exit");
int choice = scan.nextInt();
switch(choice)
{
case 1:
{
System.out.println("Enter name,age,berthprefer");
String name=scan.next();
int age =scan.nextInt();
String berthprefer=scan.next();
Passenger p = new Passenger(name,age,berthprefer);
book_ticket(p);
}
break;
case 2:
{
booker book = new booker();
book.printAvailable();
}
break;
case 4:
{
booker book =new booker();
book.bookedTickets();
}
break;
case 5:
{
System.out.println("\nlogging out...");
loop=false;
}
break;
default:
break;
}
}
}
public static void book_ticket(Passenger p){
booker book =new booker();
if(booker.availableWL==0){
System.out.println("No ticket available");
return;
}
//System.out.println("Processing");
if((p.berthprefer.equals("L") && booker.availableLB>0) ||
(p.berthprefer.equals("M") && booker.availableMB>0)||
(p.berthprefer.equals("U") && booker.availableUB>0))
{
System.out.println("Prefered berth is available");
if(p.berthprefer.equals("L")){
System.out.println("Lower Berth Given");
book.book_ticket(p,booker.LBposition.get(0),"L");
booker.LBposition.remove(0);
booker.availableLB--;
}
else if(p.berthprefer.equals("M")){
System.out.println("Middle Berth Given");
book.book_ticket(p,booker.MBposition.get(0),"M");
booker.MBposition.remove(0);
booker.availableMB--;
}
else if(p.berthprefer.equals("U")){
System.out.println("Upper Berth Given");
book.book_ticket(p,booker.UBposition.get(0),"U");
booker.UBposition.remove(0);
booker.availableUB--;
}
}
else if(booker.availableLB>0){
System.out.println("Lower berth given");
book.book_ticket(p,booker.LBposition.get(0),"L");
booker.LBposition.remove(0);
booker.availableLB--;
}
else if(booker.availableMB>0){
System.out.println("Middle Berth Given");
book.book_ticket(p,booker.MBposition.get(0),"M");
booker.MBposition.remove(0);
booker.availableMB--;
}
else if(booker.availableUB>0){
System.out.println("Upper Berth Given");
book.book_ticket(p,booker.UBposition.get(0),"U");
booker.UBposition.remove(0);
booker.availableUB--;
}
else if(booker.availableRAC>0){
System.out.println("Rac Available");
book.addToRAC(p,booker.RACposition.get(0),"RAC");
}
else{
System.out.println("Waiting list Available");
book.addToWaitList(p,booker.WLposition.get(0),"WL");
}
}
}
class booker{
static int availableLB =1;
static int availableMB =0;
static int availableUB =0;
static int availableRAC=1;
static int availableWL =1;
static Queue<Integer> RACList = new LinkedList<>();
static Queue<Integer> WaitList = new LinkedList<>();
static List<Integer> BookedList =new ArrayList<>();
static Map<Integer,Passenger> passenger = new HashMap<>();
static List<Integer> LBposition =new ArrayList<>(Arrays.asList(1));
static List<Integer> MBposition =new ArrayList<>(Arrays.asList(1));
static List<Integer> UBposition =new ArrayList<>(Arrays.asList(1));
static List<Integer> RACposition =new ArrayList<>(Arrays.asList(1));
static List<Integer> WLposition =new ArrayList<>(Arrays.asList(1));
public void book_ticket(Passenger p,int berthInfo, String allotedBerth){
p.seatNo=berthInfo;
p.allotedBerth=allotedBerth;
BookedList.add(p.passengerId);
passenger.put(p.passengerId,p);
System.out.println("Booked Successfull");
System.out.println("\n---------------------------------");
}
public void addToRAC(Passenger p, int RACInfo, String RACAvailable){
p.seatNo=RACInfo;
p.allotedBerth=RACAvailable;
RACList.add(p.passengerId);
passenger.put(p.passengerId,p);
System.out.println("RAC Booked Successfull");
RACposition.remove(0);
availableRAC--;
}
public void addToWaitList(Passenger p,int WLInfo, String WLAvailable){
p.seatNo=WLInfo;
p.allotedBerth=WLAvailable;
WaitList.add(p.passengerId);
passenger.put(p.passengerId,p);
System.out.println("Waiting List Booked Successfull");
WLposition.remove(0);
availableWL--;
}
public void printAvailable(){
System.out.println("Available LB: " + availableLB);
System.out.println("Available MB: " + availableMB);
System.out.println("Available UB: " + availableUB);
System.out.println("Available RAC: " + availableRAC);
System.out.println("Available WL: " + availableWL);
System.out.println("\n---------------------------------");
}
public void bookedTickets(){
for(Passenger p: passenger.values()){
System.out.println("Name: "+p.name);
System.out.println("Age: "+p.age);
System.out.println("AllotedBerth: "+p.seatNo+p.allotedBerth);
System.out.println("\n---------------------------------");
}
}
}
class Passenger{
static int id=1;
String name;
int age;
String berthprefer;
int passengerId;
String allotedBerth;
int seatNo;
Passenger(String name,int age, String berthprefer){
this.name=name;
this.age=age;
this.berthprefer=berthprefer;
this.passengerId=id++;
allotedBerth="";
seatNo=-1;
}
}