diff --git a/solutions/cpp/loggingframework/Logger.hpp b/solutions/cpp/loggingframework/Logger.hpp index 990b0b25..31b39e52 100644 --- a/solutions/cpp/loggingframework/Logger.hpp +++ b/solutions/cpp/loggingframework/Logger.hpp @@ -13,14 +13,13 @@ class Logger { std::string name; LogLevel minLevel; std::vector> appenders; - + void log(LogLevel level, const std::string& message); public: Logger(const std::string& name, LogLevel minLevel = LogLevel::INFO); void addAppender(std::shared_ptr 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); diff --git a/solutions/cpp/parkinglot/FeeFlatRate.cpp b/solutions/cpp/parkinglot/FeeFlatRate.cpp new file mode 100644 index 00000000..2148899a --- /dev/null +++ b/solutions/cpp/parkinglot/FeeFlatRate.cpp @@ -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; +} \ No newline at end of file diff --git a/solutions/cpp/parkinglot/FeeFlatRate.hpp b/solutions/cpp/parkinglot/FeeFlatRate.hpp new file mode 100644 index 00000000..c9192a29 --- /dev/null +++ b/solutions/cpp/parkinglot/FeeFlatRate.hpp @@ -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 diff --git a/solutions/cpp/parkinglot/FeeStartegy.cpp b/solutions/cpp/parkinglot/FeeStartegy.cpp new file mode 100644 index 00000000..975ba5a9 --- /dev/null +++ b/solutions/cpp/parkinglot/FeeStartegy.cpp @@ -0,0 +1,14 @@ +#include "FeeStrategy.hpp" + +void FeeStrategy::setDuration(ParkingTicket* ticket) { + long long arrivalTime = ticket->getEntryTime(); + long long exitTime = chrono::duration_cast( + chrono::system_clock::now().time_since_epoch()).count(); + + duration = exitTime - arrivalTime; + +} + +int FeeStrategy::getDuration() { + return duration; +} diff --git a/solutions/cpp/parkinglot/FeeStrategy.hpp b/solutions/cpp/parkinglot/FeeStrategy.hpp new file mode 100644 index 00000000..0833496e --- /dev/null +++ b/solutions/cpp/parkinglot/FeeStrategy.hpp @@ -0,0 +1,17 @@ +#ifndef PARKING_FEE_STRATEGY +#define PARKING_FEE_STRATEGY + +#include "ParkingTicket.hpp" +#include +using namespace std; +class FeeStrategy { + private: + int duration; + public: + virtual int calculateFee(ParkingTicket* ticket) = 0; + void setDuration(ParkingTicket* ticket); + int getDuration(); + +}; + +#endif \ No newline at end of file diff --git a/solutions/cpp/parkinglot/FeeVehicleBased.cpp b/solutions/cpp/parkinglot/FeeVehicleBased.cpp new file mode 100644 index 00000000..51ff9c0e --- /dev/null +++ b/solutions/cpp/parkinglot/FeeVehicleBased.cpp @@ -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; + } +} \ No newline at end of file diff --git a/solutions/cpp/parkinglot/FeeVehicleBased.hpp b/solutions/cpp/parkinglot/FeeVehicleBased.hpp new file mode 100644 index 00000000..ea05f6b4 --- /dev/null +++ b/solutions/cpp/parkinglot/FeeVehicleBased.hpp @@ -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 diff --git a/solutions/cpp/parkinglot/ParkingLot.cpp b/solutions/cpp/parkinglot/ParkingLot.cpp index d6e28e4e..72c00ae2 100644 --- a/solutions/cpp/parkinglot/ParkingLot.cpp +++ b/solutions/cpp/parkinglot/ParkingLot.cpp @@ -1,8 +1,9 @@ #include "ParkingLot.hpp" #include - -ParkingLot::ParkingLot(int numCompact, int numRegular, int numLarge) - : capacity(numCompact + numRegular + numLarge), availableSpots(capacity) { +#include +#include +ParkingLot::ParkingLot(int numCompact, int numRegular, int numLarge, FeeStrategy* feeStrategy) + : capacity(numCompact + numRegular + numLarge), availableSpots(capacity), feeStrategy(feeStrategy) { int spotNumber = 1; @@ -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; @@ -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; @@ -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; } diff --git a/solutions/cpp/parkinglot/ParkingLot.hpp b/solutions/cpp/parkinglot/ParkingLot.hpp index 4ce33d81..b55679f9 100644 --- a/solutions/cpp/parkinglot/ParkingLot.hpp +++ b/solutions/cpp/parkinglot/ParkingLot.hpp @@ -5,16 +5,21 @@ #include #include #include "ParkingSpot.hpp" +#include "ParkingTicket.hpp" +#include "FeeFlatRate.hpp" +#include "FeeVehicleBased.hpp" class ParkingLot { private: std::vector spots; std::map occupiedSpots; // licensePlate -> spot + std::map 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; @@ -26,7 +31,7 @@ class ParkingLot { void displayInfo() const; void displayOccupancy() const; - + void displayFee(ParkingTicket* ticket); private: ParkingSpot* findAvailableSpot(const Vehicle* vehicle) const; }; diff --git a/solutions/cpp/parkinglot/ParkingLotDemo.cpp b/solutions/cpp/parkinglot/ParkingLotDemo.cpp index 41332605..edf3e2f0 100644 --- a/solutions/cpp/parkinglot/ParkingLotDemo.cpp +++ b/solutions/cpp/parkinglot/ParkingLotDemo.cpp @@ -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(); diff --git a/solutions/cpp/parkinglot/ParkingTicket.cpp b/solutions/cpp/parkinglot/ParkingTicket.cpp new file mode 100644 index 00000000..971cf89c --- /dev/null +++ b/solutions/cpp/parkinglot/ParkingTicket.cpp @@ -0,0 +1,27 @@ +#include "ParkingTicket.hpp" +#include +using namespace std; + +ParkingTicket:: ParkingTicket(ParkingSpot* spot) : allottedSpot(spot), license(allottedSpot->getVehicle()->getLicensePlate()) { + arrivalTime = chrono::duration_cast( + 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; \ No newline at end of file diff --git a/solutions/cpp/parkinglot/ParkingTicket.hpp b/solutions/cpp/parkinglot/ParkingTicket.hpp new file mode 100644 index 00000000..a47c107c --- /dev/null +++ b/solutions/cpp/parkinglot/ParkingTicket.hpp @@ -0,0 +1,29 @@ +#ifndef PARKING_TICKET_HPP +#define PARKING_TICKET_HPP + +#include "ParkingSpot.hpp" +#include +#include +#include +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 \ No newline at end of file