I have these six files:
Reservation.hpp;
#ifndef RESERVATION_HPP
#define RESERVATION_HPP
#include <string>
#include "Date.hpp"
class Reservation
{
std::string referenceID;
Date startDate;
Date endDate;
public:
Reservation(const std::string& referenceID, const Date& start, const Date& end);
Reservation(const Reservation& other);
Reservation& operator=(const Reservation& other);
bool operator==(const Reservation& other) const;
bool operator!=(const Reservation& other) const;
bool Overlaps(const Reservation& other) const;
const std::string& GetReferenceID() const;
const Date& GetStartDate() const;
const Date& GetEndDate() const;
};
#endif // RESERVATION_HPP
Reservation.cpp;
\#include "Reservation.hpp"
\#include \<stdexcept\>
Reservation::Reservation(const std::string& referenceID, const Date& start, const Date& end)
{
this->referenceID = referenceID;
this->startDate = start;
this->endDate = end;
if (startDate > endDate)
{
throw std::invalid_argument("Invalidate date values");
}
}
Reservation::Reservation(const Reservation& other)
{
this->referenceID = other.referenceID;
this->startDate = other.startDate;
this->endDate = other.endDate;
}
Reservation& Reservation::operator=(const Reservation& other)
{
if (this != &other)
{
this->endDate = other.endDate;
this->startDate = other.startDate;
this->referenceID = other.referenceID;
}
return *this;
}
bool Reservation::operator==(const Reservation& other) const
{
if (this->referenceID == other.referenceID && this->endDate == other.endDate && this->startDate == other.startDate)
{
return true;
}
return false;
}
bool Reservation::operator!=(const Reservation& other) const
{
if (this->referenceID != other.referenceID || this->endDate != other.endDate || this->startDate != other.startDate)
{
return false;
}
return true;
}
bool Reservation::Overlaps(const Reservation& other) const
{
if (startDate>endDate)
{
return true;
}
if (this->referenceID == other.referenceID)
{
if (startDate >= endDate)
{
return true;
}
}
return false;
}
const std::string& Reservation::GetReferenceID() const
{
return this->referenceID;
}
const Date& Reservation::GetStartDate() const
{
return this->startDate;
}
const Date& Reservation::GetEndDate() const
{
return this->endDate;
}
BookingSystem.hpp
\#ifndef BOOKING_SYSTEM_HPP
\#define BOOKING_SYSTEM_HPP
\#include "Reservation.hpp"
class BookingSystem
{
int capacity;
int reservationCount;
Reservation** reservations;
void Expand();
public:
BookingSystem(int capacity);
~BookingSystem();
BookingSystem(const BookingSystem& other);
BookingSystem& operator=(const BookingSystem& other);
bool Reserve(const std::string& referenceID, const Date& start, const Date& end);
int GetReservationCount() const;
int GetReservationCapacity() const;
Reservation** GetReservations() const;
};
\#endif // BOOKING_SYSTEM_HPP
BookingSystem.cpp
\#include "BookingSystem.hpp"
void BookingSystem::Expand()
{
this->capacity = 10;
Reservation** temp = new Reservation * [this-> capacity] {nullptr};
for (int i = 0; i < reservationCount; i )
{
temp[i] = this->reservations[i];
}
delete[]this->reservations;
this->reservations = temp;
}
BookingSystem::BookingSystem(int capacity)
:reservationCount(0), capacity(capacity)
{
this->reservations = new Reservation * [this->capacity] {nullptr};
}
BookingSystem::~BookingSystem()
{
for (int i = 0; i < this->reservationCount; i )
{
delete this->reservations;
}
delete[]this->reservations;
}
BookingSystem::BookingSystem(const BookingSystem& other)
{
this->capacity = other.capacity;
this->reservationCount = other.reservationCount;
this->reservations = new Reservation * [other.capacity] {nullptr };
for (int i = 0; i < reservationCount; i )
{
this->reservations[i] = new Reservation(*other.reservations[i]);
}
}
BookingSystem& BookingSystem::operator=(const BookingSystem& other)
{
if (this != &other)
{
for (int i = 0; i < this->reservationCount; i )
{
delete this->reservations[i];
}
delete[]this->reservations;
this->capacity = other.capacity;
this->reservationCount = other.reservationCount;
this->reservations = new Reservation * [other.reservationCount] {nullptr };
for (int i = 0; i < reservationCount; i )
{
this->reservations[i] = new Reservation(*other.reservations[i]);
}
}
return *this;
}
bool BookingSystem::Reserve(const std::string& referenceID, const Date& start, const Date& end)
{
if (this->reservationCount == this->capacity)
{
Expand();
}
Reservation temp(referenceID, start, end);
for (int i = 0; i < reservationCount; i )
{
if (this->reservations[i]->Overlaps(temp))
{
this->reservations[this->reservationCount] = new Reservation(referenceID, start, end);
}
}
reservationCount ;
return false;
}
int BookingSystem::GetReservationCount() const
{
return this->reservationCount;
}
int BookingSystem::GetReservationCapacity() const
{
return this->capacity;
}
Reservation** BookingSystem::GetReservations() const
{
return nullptr;
}
Date.hpp
\#ifndef DATE_HPP
\#define DATE_HPP
\#include <string>
class Date
{
public:
Date();
Date(unsigned int day, unsigned int month, unsigned int year);
Date(const Date& other);
Date& operator=(const Date& other);
bool operator==(const Date& other) const;
bool operator!=(const Date& other) const;
bool operator>(const Date& other) const;
bool operator<(const Date& other) const;
bool operator>=(const Date& other) const;
bool operator<=(const Date& other) const;
void SetDay(unsigned int day);
void SetMonth(unsigned int month);
void SetYear(unsigned int year);
void SetDate(unsigned int day, unsigned int month, unsigned int year);
unsigned int GetDay() const;
unsigned int GetMonth() const;
unsigned int GetYear() const;
std::string GetString() const;
private:
unsigned int day;
unsigned int month;
unsigned int year;
unsigned int GetCombinedDateValue() const;
static unsigned int GetTotalDaysOfMonth(unsigned int month, unsigned int year);
static bool IsLeapYear(unsigned int year);
static bool IsValidDate(unsigned int day, unsigned int month, unsigned int year);
};
\#endif // DATE_HPPs
Date.cpp
\#include "Date.hpp"
\#include <stdexcept>
\#include <string>
Date::Date() : day(1), month(1), year(0)
{
}
Date::Date(unsigned int day, unsigned int month, unsigned int year) :
day(day), month(month), year(year)
{
if (!IsValidDate(this->day, this->month, this->year))
throw std::invalid_argument(
"Invalid date values: day=" std::to_string(this->day)
", month=" std::to_string(this->month)
", year=" std::to_string(this->year));
}
Date::Date(const Date& other) :
day(other.day), month(other.month), year(other.year)
{
}
Date& Date::operator=(const Date& other)
{
this->day = other.day;
this->month = other.month;
this->year = other.year;
return *this;
}
bool Date::operator==(const Date& other) const
{
return this->GetCombinedDateValue() == other.GetCombinedDateValue();
}
bool Date::operator!=(const Date& other) const
{
return !(*this == other);
}
bool Date::operator>(const Date& other) const
{
return this->GetCombinedDateValue() > other.GetCombinedDateValue();
}
bool Date::operator<(const Date& other) const
{
return this->GetCombinedDateValue() < other.GetCombinedDateValue();
}
bool Date::operator>=(const Date& other) const
{
return this->GetCombinedDateValue() >= other.GetCombinedDateValue();
}
bool Date::operator<=(const Date& other) const
{
return this->GetCombinedDateValue() <= other.GetCombinedDateValue();
}
void Date::SetDay(unsigned int day)
{
this->day = day;
}
void Date::SetMonth(unsigned int month)
{
this->month = month;
}
void Date::SetYear(unsigned int year)
{
this->year = year;
}
void Date::SetDate(unsigned int day, unsigned int month, unsigned int year)
{
this->day = day;
this->month = month;
this->year = year;
}
unsigned int Date::GetDay() const
{
return this->day;
}
unsigned int Date::GetMonth() const
{
return this->month;
}
unsigned int Date::GetYear() const
{
return this->year;
}
std::string Date::GetString() const
{
return "Date(" std::to_string(this->day) ", " std::to_string(this->month) ", " std::to_string(this->year) ")";
}
unsigned int Date::GetCombinedDateValue() const
{
/\*\*
\* Priority: year-month-date
\*
\* january 15th 2022 as a combined value: 20220115
\* october 9th 2023 as a combined value: 20231009
\*
\* Comparing example: 20231009 > 20220115 Ok!
\*/
return this->year * 10000 this->month * 100 this->day;
}
unsigned int Date::GetTotalDaysOfMonth(unsigned int month, unsigned int year)
{
static const unsigned int JANUARY = 1;
static const unsigned int FEBRUARY = 2;
static const unsigned int MARCH = 3;
static const unsigned int APRIL = 4;
static const unsigned int MAY = 5;
static const unsigned int JUNE = 6;
static const unsigned int JULY = 7;
static const unsigned int AUGUST = 8;
static const unsigned int SEPTEMBER = 9;
static const unsigned int OCTOBER = 10;
static const unsigned int NOVEMBER = 11;
static const unsigned int DECEMBER = 12;
// February is a special case -> handle it first
unsigned int day = 0;
if (month == FEBRUARY)
day = 28 ((IsLeapYear(year)) ? 1 : 0);
else
{
switch (month)
{
case JANUARY:
case MARCH:
case MAY:
case JULY:
case AUGUST:
case OCTOBER:
case DECEMBER:
day = 31;
break;
case APRIL:
case JUNE:
case SEPTEMBER:
case NOVEMBER:
day = 30;
break;
};
}
return day;
}
bool Date::IsLeapYear(unsigned int year)
{
/\*\*
\* https://en.wikipedia.org/wiki/Leap_year :
\*
\* "Every year that is exactly divisible by four is a leap year, except
\* for years that are exactly divisible by 100, but these centurial
\* years are leap years if they are exactly divisible by 400. For
\* example, the years 1700, 1800, and 1900 are not leap years, but the
\* years 1600 and 2000 are."
\*/
bool divisableByFour = (year % 4 == 0);
bool divisableByHundred = (year % 100 == 0);
bool divisableByFourHundred = (year % 400 == 0);
if (!divisableByFour)
return false;
if (!divisableByHundred) // && divisableByFour
return true;
return divisableByFourHundred; // && divisableByFour && divisableByHundred
}
bool Date::IsValidDate(unsigned int day, unsigned int month, unsigned int year)
{
bool isValid = true;
isValid = isValid && (month <= 12);
isValid = isValid && (day <= GetTotalDaysOfMonth(month, year));
return isValid;
}
Description:
I want to create a code that can be used to book certain things. These things is defined with 'referenceID', examples on this would be a cabin, bike or a car (it is only a name so it works with anything).
If it looks like there are missing a main() it is because that one are irrelevant in this situation and I only need some guidence with one function.
In 'Reservation.cpp' there are a function that is named Overlaps (bool Reservation::Overlaps() const). The use of this is to check if there has been an overlap in the booking.
There so called rules for Overlaps() are as followed:
- It´s end date is not before it´s start date.
- No other booking exists with the same reference ID for the same period of time. - However, two bookings are allowed to share the same time period if they have different referer IDs.
For example:
Reservation foo("Bastuflotta", Date(1, 1, 2022), Date(5, 1, 2022));
Reservation bar("Bastuflotta", Date(3, 1, 2022), Date(8, 1, 2022));
foo.Overlaps(bar); // returns true
The following two are not an overlap since they do not share the same reference Id:
Reservation foo("Bastuflotta", Date(1, 1, 2022), Date(5, 1, 2022));
Reservation bar("Badtunna", Date(3, 1, 2022), Date(8, 1, 2022));
foo.Overlaps(bar); // returns false
I have tried and tried but can't get Overlaps to work. Pls if you have any examples or tips on how to do it, pls share you´r thoughts.
Thank you in beforehand!
CodePudding user response:
To implement the Overlaps function, you can check if the start or end date of the given Reservation object (other) falls between the start and end dates of the current Reservation object (this), or if the start or end date of the current Reservation object falls between the start and end dates of the given Reservation object. If either of these conditions is true, then the two reservations overlap.
Here is one possible implementation of the Overlaps function:
bool Reservation::Overlaps(const Reservation& other) const
{
// Check if the start or end date of the other reservation falls between the start and end dates of this reservation.
if ((other.startDate >= this->startDate && other.startDate <= this->endDate) ||
(other.endDate >= this->startDate && other.endDate <= this->endDate))
{
return true;
}
// Check if the start or end date of this reservation falls between the start and end dates of the other reservation.
if ((this->startDate >= other.startDate && this->startDate <= other.endDate) ||
(this->endDate >= other.startDate && this->endDate <= other.endDate))
{
return true;
}
// If none of the above conditions are true, then the reservations do not overlap.
return false;
}