0% found this document useful (0 votes)
16 views3 pages

Zohotaxi

The document defines classes for a taxi booking system including Bookings, Taxis, and a TaxiBookingSystem class. The TaxiBookingSystem class handles booking taxis for customers by finding available or nearest taxis and assigning bookings. It also displays details of all taxis and their bookings.

Uploaded by

SUSEENDRAN V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Zohotaxi

The document defines classes for a taxi booking system including Bookings, Taxis, and a TaxiBookingSystem class. The TaxiBookingSystem class handles booking taxis for customers by finding available or nearest taxis and assigning bookings. It also displays details of all taxis and their bookings.

Uploaded by

SUSEENDRAN V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <vector>
#include <cmath> // For abs
#include <limits> // For numeric_limits
#include <algorithm> // For find

using namespace std;

// Class to represent a single booking


class Booking {
private:
int customerId;
int pickupPoint;
int dropPoint;
int pickupTime;
int dropTime;
double amount;

public:
Booking(int id, int pickup, int drop, int pTime, int dTime, double amt)
: customerId(id), pickupPoint(pickup), dropPoint(drop),
pickupTime(pTime), dropTime(dTime), amount(amt) {}

// Getters for booking details


int getCustomerId() const { return customerId; }
int getPickupPoint() const { return pickupPoint; }
int getDropPoint() const { return dropPoint; }
int getPickupTime() const { return pickupTime; }
int getDropTime() const { return dropTime; }
double getAmount() const { return amount; }
};

// Class to represent a Taxi


class Taxi {
private:
static int idGenerator; // Static ID generator for taxis
int taxiId;
double totalEarnings;
int currentPoint; // Current point of the taxi
vector<Booking> bookings; // Bookings made by the taxi

public:
Taxi() : taxiId(++idGenerator), totalEarnings(0), currentPoint(0) {}

// Getters for taxi details


int getId() const { return taxiId; }
double getTotalEarnings() const { return totalEarnings; }
const vector<Booking>& getBookings() const { return bookings; }

// Function to assign a booking to the taxi


void assignBooking(const Booking& booking) {
bookings.push_back(booking);
totalEarnings += booking.getAmount();
currentPoint = booking.getDropPoint(); // Update current point to drop point
}
};

// Static initialization for Taxi ID generator


int Taxi::idGenerator = 0;

// Class to represent the Taxi Booking System


class TaxiBookingSystem {
private:
vector<Taxi> taxis; // Vector to hold all taxis
const int numTaxis = 4; // Number of taxis
const int numPoints = 6; // Number of points (A, B, C, D, E, F)
const int distanceBetweenPoints = 15; // Distance between consecutive points in km
const int travelTimeBetweenPoints = 60; // Travel time between consecutive points in minutes
const double minCharge = 100; // Minimum charge for the first 5 km
const double subsequentCharge = 10; // Charge for subsequent kilometers

public:
TaxiBookingSystem() {
taxis.reserve(numTaxis); // Reserve space for taxis
for (int i = 0; i < numTaxis; ++i) {
taxis.push_back(Taxi()); // Initialize taxis
}
}

// Function to book a taxi for a customer


void bookTaxi(int customerId, char pickupPoint, char dropPoint, int pickupTime) {
// Convert characters to indices (A=0, B=1, C=2, ...)
int pickupIndex = pickupPoint - 'A';
int dropIndex = dropPoint - 'A';

// Calculate distance and amount


int distance = abs(dropIndex - pickupIndex) * distanceBetweenPoints;
double amount = minCharge + max(0.0, distance - 5) * subsequentCharge;

// Find a free taxi at pickup point


Taxi* freeTaxi = findFreeTaxiAtPoint(pickupIndex);
if (freeTaxi) {
// If a free taxi is available at pickup point, assign booking to that taxi
freeTaxi->assignBooking(Booking(customerId, pickupIndex, dropIndex, pickupTime, pickupTime + (
distance / distanceBetweenPoints), amount));
cout << "Taxi can be allotted. Taxi-" << freeTaxi->getId() << " is allotted." << endl;
} else {
// If no free taxi is available at pickup point, find the nearest free taxi
Taxi* nearestTaxi = findNearestFreeTaxi(pickupIndex);
if (nearestTaxi) {
// If a nearest free taxi is found, assign booking to that taxi
nearestTaxi->assignBooking(Booking(customerId, pickupIndex, dropIndex, pickupTime, pickupTime +
(distance / distanceBetweenPoints), amount));
cout << "Taxi can be allotted. Taxi-" << nearestTaxi->getId() << " is allotted." << endl;
} else {
// If no free taxi is available, reject booking
cout << "Booking rejected. No free taxi available." << endl;
}
}
}

// Function to find a free taxi at a given point


Taxi* findFreeTaxiAtPoint(int pointIndex) {
for (auto& taxi : taxis) {
if (taxi.getBookings().empty() || taxi.getBookings().back().getDropPoint() == pointIndex) {
return &taxi;
}
}
return nullptr;
}

// Function to find the nearest free taxi


Taxi* findNearestFreeTaxi(int pointIndex) {
int minDistance = numeric_limits<int>::max();
Taxi* nearestTaxi = nullptr;

for (auto& taxi : taxis) {


if (taxi.getBookings().empty()) {
int distance = abs(taxi.getId() - pointIndex);
if (distance < minDistance) {
minDistance = distance;
nearestTaxi = &taxi;
}
} else {
int lastDropPoint = taxi.getBookings().back().getDropPoint();
int distance = abs(lastDropPoint - pointIndex);
if (distance < minDistance) {
minDistance = distance;
nearestTaxi = &taxi;
}
}
}
return nearestTaxi;
}

// Function to display taxi details


void displayTaxiDetails() const {
cout << "Taxi Details:" << endl;
for (const auto& taxi : taxis) {
cout << "Taxi No: " << taxi.getId() << endl;
cout << "Total Earnings: Rs. " << taxi.getTotalEarnings() << endl;
for (const auto& booking : taxi.getBookings()) {
cout << booking.getCustomerId() << " ";
cout << (char)('A' + booking.getPickupPoint()) << " ";
cout << (char)('A' + booking.getDropPoint()) << " ";
cout << booking.getPickupTime() << " ";
cout << booking.getDropTime() << " ";
cout << booking.getAmount() << endl;
}
}
}
};

int main() {
TaxiBookingSystem bookingSystem;

// Sample bookings
bookingSystem.bookTaxi(1, 'A', 'B', 9);
bookingSystem.bookTaxi(2, 'B', 'D', 9);
bookingSystem.bookTaxi(3, 'B', 'C', 12);

// Display taxi details


bookingSystem.displayTaxiDetails();

return 0;
}

You might also like