Zoho
Zoho
Zoho
Main .java
import java.util.*;
if(TicketBooker.availableWaitingList == 0)
{
System.out.println("No Tickets Available");
return;
}
if((p.berthPreference.equals("L") &&
TicketBooker.availableLowerBerths > 0 )||
(p.berthPreference.equals("M") &&
TicketBooker.availableMiddleBerths > 0) ||
(p.berthPreference.equals("U") &&
TicketBooker.availableUpperBerths > 0))
{
System.out.println("Preferred Berth Available");
if(p.berthPreference.equals("L"))
{
System.out.println("Lower Berth Given");
booker.bookTicket(p,(TicketBooker.lowerBerthsPositions.get(0)),"L");
TicketBooker.lowerBerthsPositions.remove(0);
TicketBooker.availableLowerBerths--;
}
else if(p.berthPreference.equals("M"))
{
System.out.println("Middle Berth Given");
booker.bookTicket(p,(TicketBooker.middleBerthsPositions.get(0)),"M");
TicketBooker.middleBerthsPositions.remove(0);
TicketBooker.availableMiddleBerths--;
}
else if(p.berthPreference.equals("U"))
{
System.out.println("Upper Berth Given");
booker.bookTicket(p,(TicketBooker.upperBerthsPositions.get(0)),"U");
TicketBooker.upperBerthsPositions.remove(0);
TicketBooker.availableUpperBerths--;
}
}
booker.bookTicket(p,(TicketBooker.lowerBerthsPositions.get(0)),"L");
TicketBooker.lowerBerthsPositions.remove(0);
TicketBooker.availableLowerBerths--;
}
else if(TicketBooker.availableMiddleBerths > 0)
{
System.out.println("Middle Berth Given");
booker.bookTicket(p,(TicketBooker.middleBerthsPositions.get(0)),"M");
TicketBooker.middleBerthsPositions.remove(0);
TicketBooker.availableMiddleBerths--;
}
else if(TicketBooker.availableUpperBerths > 0)
{
System.out.println("Upper Berth Given");
booker.bookTicket(p,(TicketBooker.upperBerthsPositions.get(0)),"U");
TicketBooker.upperBerthsPositions.remove(0);
TicketBooker.availableUpperBerths--;
booker.addToWaitingList(p,(TicketBooker.waitingListPositions.get(0)),"WL");
if(!booker.passengers.containsKey(id))
{
System.out.println("Passenger detail Unknown");
}
else
booker.cancelTicket(id);
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
boolean loop = true;
while(loop)
{
System.out.println(" 1. Book Ticket \n 2. Cancel Ticket \n 3.
Available Tickets \n 4. Booked Tickets \n 5. Exit");
int choice = s.nextInt();
switch(choice)
{
case 1:
{
bookTicket(p);
}
break;
case 2:
{
case 3:
{
TicketBooker booker = new TicketBooker();
booker.printAvailable();
}
break;
case 4:
{
TicketBooker booker = new TicketBooker();
booker.printPassengers();
}
break;
case 5:
{
loop = false;
}
break;
default:
break;
}
}
}
}
Passenger.java
public class Passenger
{
static int id = 1;
String name;
int age;
String berthPreference;
int passengerId;
String alloted;
int number;//seat number
public Passenger(String name,int age,String berthPreference)
{
this.name = name;
this.age = age;
this.berthPreference = berthPreference;
this.passengerId = id++;
alloted = "";
number = -1;
}
}
TicketBooker.java
import java.util.*;
public class TicketBooker
{
//book ticket
public void bookTicket(Passenger p, int berthInfo,String allotedBerth)
{
p.number = berthInfo;
p.alloted = allotedBerth;
passengers.put(p.passengerId,p);
bookedTicketList.add(p.passengerId);
System.out.println("--------------------------Booked
Successfully");
}
p.number = racInfo;
p.alloted = allotedRAC;
passengers.put(p.passengerId,p);
racList.add(p.passengerId);
availableRacTickets--;
racPositions.remove(0);
System.out.println("--------------------------added to RAC
Successfully");
}
passengers.put(p.passengerId,p);
waitingList.add(p.passengerId);
availableWaitingList--;
waitingListPositions.remove(0);
//cancel ticket
public void cancelTicket(int passengerId)
{
Passenger p = passengers.get(passengerId);
passengers.remove(Integer.valueOf(passengerId));
bookedTicketList.remove(Integer.valueOf(passengerId));
if(p.alloted.equals("L"))
{
availableLowerBerths++;
lowerBerthsPositions.add(positionBooked);
}
else if(p.alloted.equals("M"))
{
availableMiddleBerths++;
middleBerthsPositions.add(positionBooked);
}
else if(p.alloted.equals("U"))
{
availableUpperBerths++;
upperBerthsPositions.add(positionBooked);
}
Passenger passengerFromWaitingList =
passengers.get(waitingList.poll());
int positionWL = passengerFromWaitingList.number;
waitingListPositions.add(positionWL);
waitingList.remove(Integer.valueOf(passengerFromWaitingList.passengerId));
passengerFromWaitingList.number = racPositions.get(0);
passengerFromWaitingList.alloted = "RAC";
racPositions.remove(0);
racList.add(passengerFromWaitingList.passengerId);
availableWaitingList++;
availableRacTickets--;
}
Main.bookTicket(passengerFromRAC);
}
TAXI BOOKING:
Taxi.java
import java.util.*;
int id;
booked = false;
freeTime = 6;//example 6 AM
totalEarnings = 0;
id = taxicount;
this.booked = booked;
this.currentSpot = currentSpot;
this.freeTime = freeTime;
this.totalEarnings = totalEarnings;
this.trips.add(tripDetail);
System.out.println("--------------------------------------------------------------------------------------");
}
//print total earningand taxi details like current location and free time
System.out.println("Taxi - "+ this.id + " Total Earnings - " + this.totalEarnings + " Current spot - "
+ this.currentSpot +" Free Time - " + this.freeTime);
Booking.java
/*
The are 6 points(A,B,C,D,E,F) 15 KM apart 60 min travel between each, n taxis all taxis at A starting
100 rs for first 5 KM and then 10 for each of the further KMs, rate from pickup to drop only
-If no free taxi is available at that point, a free taxi at the nearest point is allocated.
-If two taxi’s are free at the same point, one with lower earning is allocated
Input 1:
Customer ID: 1
Pickup Point: A
Drop Point: B
Pickup Time: 9
Output 1:
Taxi can be allotted.
Taxi-1 is allotted
*/
import java.util.*;
// to find nearest
int distanceBetweenpickUpandDrop = 0;
int earning = 0;
int nextfreeTime = 0;
//booked taxi
bookedTaxi = t;
nextfreeTime = dropTime;
nextSpot = dropPoint;
tripDetail = customerID + " " + customerID + " " + pickupPoint + " "+
dropPoint + " " + pickupTime + " " +dropTime + " " + earning;
min = distanceBetweenCustomerAndTaxi;
bookedTaxi.setDetails(true,nextSpot,nextfreeTime,bookedTaxi.totalEarnings +
earning,tripDetail);
//BOOKED SUCCESSFULLY
// create taxis
taxis.add(t);
return taxis;
for(Taxi t : taxis)
freeTaxis.add(t);
return freeTaxis;
}
public static void main(String[] args)
//create 4 taxis
int id = 1;
while(true)
switch(choice)
case 0:
return;
// get all free taxis that can reach customer on or before pickup time
if(freeTaxis.size() == 0)
return;
Collections.sort(freeTaxis,(a,b)->a.totalEarnings - b.totalEarnings);
bookTaxi(id,pickupPoint,dropPoint,pickupTime,freeTaxis);
id++;
break;
case 1:
for(Taxi t : taxis)
t.printTaxiDetails();
for(Taxi t : taxis)
t.printDetails();
break;
default:
return;