Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions solutions/cpp/loggingframework/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ class Logger {
std::string name;
LogLevel minLevel;
std::vector<std::shared_ptr<LogAppender>> appenders;

void log(LogLevel level, const std::string& message);
public:
Logger(const std::string& name, LogLevel minLevel = LogLevel::INFO);

void addAppender(std::shared_ptr<LogAppender> appender);
void setMinLevel(LogLevel level);

void log(LogLevel level, const std::string& message);
void trace(const std::string& message);
void debug(const std::string& message);
void info(const std::string& message);
Expand Down
16 changes: 16 additions & 0 deletions solutions/cpp/parkinglot/FeeFlatRate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "FeeFlatRate.hpp"
//Default rate is Rs 2 per second

int FeeFlatRate::calculateFee(ParkingTicket* ticket) {
setDuration(ticket);
setRate();
return rate * getDuration();
}

int FeeFlatRate::getRate() {
return rate;
}

void FeeFlatRate::setRate() {
rate = 2;
}
15 changes: 15 additions & 0 deletions solutions/cpp/parkinglot/FeeFlatRate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef PARKING_FEE_FLAT
#define PARKING_FEE_FLAT

#include "FeeStrategy.hpp"

class FeeFlatRate: public FeeStrategy {
private:
int rate;
public:
int calculateFee(ParkingTicket* ticket) override;
int getRate();
void setRate();
};

#endif
14 changes: 14 additions & 0 deletions solutions/cpp/parkinglot/FeeStartegy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "FeeStrategy.hpp"

void FeeStrategy::setDuration(ParkingTicket* ticket) {
long long arrivalTime = ticket->getEntryTime();
long long exitTime = chrono::duration_cast<std::chrono::seconds>(
chrono::system_clock::now().time_since_epoch()).count();

duration = exitTime - arrivalTime;

}

int FeeStrategy::getDuration() {
return duration;
}
17 changes: 17 additions & 0 deletions solutions/cpp/parkinglot/FeeStrategy.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef PARKING_FEE_STRATEGY
#define PARKING_FEE_STRATEGY

#include "ParkingTicket.hpp"
#include <iostream>
using namespace std;
class FeeStrategy {
private:
int duration;
public:
virtual int calculateFee(ParkingTicket* ticket) = 0;
void setDuration(ParkingTicket* ticket);
int getDuration();

};

#endif
27 changes: 27 additions & 0 deletions solutions/cpp/parkinglot/FeeVehicleBased.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

#include "FeeVehicleBased.hpp"
#include "Vehicle.hpp"

int FeeVehicleBased:: calculateFee(ParkingTicket* ticket) {
setDuration(ticket);
setRate(ticket);
return rate * getDuration();

}

void FeeVehicleBased::setRate(ParkingTicket* ticket) {
SpotType type = ticket->getSpotType(); // get spot type to set rate;

switch (type) {
case SpotType::COMPACT :
rate = 3;
break;

case SpotType::REGULAR:
rate = 4;
break;

case SpotType::LARGE:
rate = 5;
}
}
15 changes: 15 additions & 0 deletions solutions/cpp/parkinglot/FeeVehicleBased.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef PARKING_FEE_VEHICLE_BASED
#define PARKING_FEE_VEHICLE_BASED

#include "FeeStrategy.hpp"

class FeeVehicleBased: public FeeStrategy {
private:
int rate;
public:
int calculateFee(ParkingTicket* ticket) override;
int getRate();
void setRate(ParkingTicket* ticket);
};

#endif
18 changes: 15 additions & 3 deletions solutions/cpp/parkinglot/ParkingLot.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "ParkingLot.hpp"
#include <iostream>

ParkingLot::ParkingLot(int numCompact, int numRegular, int numLarge)
: capacity(numCompact + numRegular + numLarge), availableSpots(capacity) {
#include <chrono>
#include <thread>
ParkingLot::ParkingLot(int numCompact, int numRegular, int numLarge, FeeStrategy* feeStrategy)
: capacity(numCompact + numRegular + numLarge), availableSpots(capacity), feeStrategy(feeStrategy) {

int spotNumber = 1;

Expand Down Expand Up @@ -31,6 +32,12 @@ ParkingLot::~ParkingLot() {
int ParkingLot::getCapacity() const { return capacity; }
int ParkingLot::getAvailableSpots() const { return availableSpots; }

void ParkingLot:: displayFee(ParkingTicket* ticket) {
// sleep the main thread to compute some fee based on duration
std::this_thread::sleep_for(std::chrono::seconds(3));
std::cout << "Amount Payable(Rs): " << feeStrategy->calculateFee(ticket) << std::endl;
}

bool ParkingLot::parkVehicle(Vehicle* vehicle) {
if (!vehicle) return false;

Expand All @@ -44,7 +51,9 @@ bool ParkingLot::parkVehicle(Vehicle* vehicle) {

if (spot->parkVehicle(vehicle)) {
occupiedSpots[vehicle->getLicensePlate()] = spot;
activeTickets[spot] = new ParkingTicket(spot);
availableSpots--;
activeTickets[spot]->printTicket();
return true;
}
return false;
Expand All @@ -60,6 +69,9 @@ Vehicle* ParkingLot::removeVehicle(const std::string& licensePlate) {
occupiedSpots.erase(it);
availableSpots++;
}

displayFee(activeTickets[spot]); // use active ticket for that spot

return vehicle;
}

Expand Down
9 changes: 7 additions & 2 deletions solutions/cpp/parkinglot/ParkingLot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@
#include <map>
#include <string>
#include "ParkingSpot.hpp"
#include "ParkingTicket.hpp"
#include "FeeFlatRate.hpp"
#include "FeeVehicleBased.hpp"

class ParkingLot {
private:
std::vector<ParkingSpot*> spots;
std::map<std::string, ParkingSpot*> occupiedSpots; // licensePlate -> spot
std::map<ParkingSpot*, ParkingTicket*> activeTickets; // ParkingSpot* -> ticket
int capacity;
int availableSpots;
FeeStrategy* feeStrategy;

public:
ParkingLot(int numCompact, int numRegular, int numLarge);
ParkingLot(int numCompact, int numRegular, int numLarge, FeeStrategy* feeStrategy);
~ParkingLot();

int getCapacity() const;
Expand All @@ -26,7 +31,7 @@ class ParkingLot {

void displayInfo() const;
void displayOccupancy() const;

void displayFee(ParkingTicket* ticket);
private:
ParkingSpot* findAvailableSpot(const Vehicle* vehicle) const;
};
Expand Down
3 changes: 2 additions & 1 deletion solutions/cpp/parkinglot/ParkingLotDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

int main() {
// Create parking lot with different types of spots
ParkingLot parkingLot(2, 3, 2); // 2 compact, 3 regular, 2 large spots
FeeStrategy* feeStrategy = new FeeVehicleBased(); // create fee strategy, you want to apply for fee calculation
ParkingLot parkingLot(2, 3, 2, feeStrategy); // 2 compact, 3 regular, 2 large spots , fee strategy

std::cout << "Initial parking lot status:" << std::endl;
parkingLot.displayInfo();
Expand Down
27 changes: 27 additions & 0 deletions solutions/cpp/parkinglot/ParkingTicket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "ParkingTicket.hpp"
#include <iostream>
using namespace std;

ParkingTicket:: ParkingTicket(ParkingSpot* spot) : allottedSpot(spot), license(allottedSpot->getVehicle()->getLicensePlate()) {
arrivalTime = chrono::duration_cast<std::chrono::seconds>(
chrono::system_clock::now().time_since_epoch()).count();
ticketNum = ++id;
}

long long ParkingTicket::getEntryTime() {
return arrivalTime;
}


void ParkingTicket::printTicket() {
cout << "-------------------------------------\n" ;
cout << "| Ticket Number : " << ticketNum << endl;
cout << "| SpotID : " << allottedSpot->getSpotNumber() << endl;
cout << "| Vehicle Number : " << license << endl;
cout << "-------------------------------------\n" ;
}
SpotType ParkingTicket::getSpotType () {
return allottedSpot->getType();
}

int ParkingTicket:: id = 0;
29 changes: 29 additions & 0 deletions solutions/cpp/parkinglot/ParkingTicket.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef PARKING_TICKET_HPP
#define PARKING_TICKET_HPP

#include "ParkingSpot.hpp"
#include <string>
#include <chrono>
#include <ctime>
using namespace std;

class ParkingTicket {
private:
ParkingSpot* allottedSpot;
static int id;
int ticketNum;
long long arrivalTime;
string license;

public:
ParkingTicket(ParkingSpot* spot);
void printTicket();
SpotType getSpotType();

// Will be used to calculate parking fee
long long getEntryTime();


};

#endif