diff options
added new examples
7 files changed, 287 insertions, 0 deletions
diff --git a/Big4_and_composition/Airlines_booking_system/Airline.cpp b/Big4_and_composition/Airlines_booking_system/Airline.cpp new file mode 100644 index 0000000..f5dd3e3 --- /dev/null +++ b/Big4_and_composition/Airlines_booking_system/Airline.cpp @@ -0,0 +1,110 @@ +#include "Airline.h" + +int Airline::firstFreeIndex() +{ + for (int i = 0; i < MAXFLIGHTS; i++) + { + if (fl[i] == nullptr) + return i; + } + return -1; +} +void Airline::CopyFrom(const Airline& other) +{ + name = new char[strlen(other.name) + 1]; //Копираме именето + strcpy(name, other.name); + + for (int i = 0; i < MAXFLIGHTS; i++) + { + if (other.fl[i] == nullptr) + fl[i] = nullptr; + else + { + fl[i] = new Flight(*other.fl[i]); //извикваме копи контруктора на обектите. + } // Понеже в other.fl[i] e указател, то трябва да го 'дерефрираме'. + } +} +void Airline::Free() //трябва да изтрием името и всички обекти, към които сочат указателите +{ + delete[] name; + for (int i = 0; i < MAXFLIGHTS; i++) + delete fl[i]; //някои указатели може да имат стойност nullptr, но това няма да доведе до грешка. +} + + +Airline::Airline(const char* name) +{ + this->name = new char[strlen(name) + 1]; + strcpy(this->name, name); + + for (int i = 0; i < MAXFLIGHTS; i++) + fl[i] = nullptr; +} + +bool Airline::addFlight(const char* originAirportName, const char* originAirportId, + const char* destAirportName, const char* destAirportId, int durration) +{ + int ind = firstFreeIndex(); + + if (ind == -1) //няма свободни места. + return false; + + fl[ind] = new Flight(originAirportName, originAirportId, destAirportName, destAirportId, durration); + return true; + +} +bool Airline::addFlight(const Flight& other) +{ + int ind = firstFreeIndex(); + + if (ind == -1)//няма свободни места. + return false; + fl[ind] = new Flight(other); + return true; +} +Airline::Airline(const Airline& other) +{ + CopyFrom(other); +} +Airline& Airline::operator=(const Airline& other) +{ + if (this != &other) + { + Free(); + CopyFrom(other); + } + return *this; +} +Airline::~Airline() +{ + Free(); +} +bool Airline::containsJourney(const char* originID, const char* destID) +{ + //TODO -> fix cycles + if (strcmp(originID, destID)) + return true; + + bool solution = false; + + for (int i = 0; i < MAXFLIGHTS && !solution; i++) + { + if (fl[i] != nullptr) + { + if (strcmp(fl[i]->getOrigin().getId(), originID) == 0) + solution = solution || containsJourney(fl[i]->getDestination().getId(), destID); + } + } + return false; +} +void Airline::print() +{ + for (int i = 0; i < MAXFLIGHTS; i++) + { + if (fl[i]) + { + std::cout << "Flight: "; + fl[i]->print(); + } + } +} diff --git a/Big4_and_composition/Airlines_booking_system/Airline.h b/Big4_and_composition/Airlines_booking_system/Airline.h new file mode 100644 index 0000000..0a8f34c --- /dev/null +++ b/Big4_and_composition/Airlines_booking_system/Airline.h @@ -0,0 +1,28 @@ +#include "Flight.h" + +const int MAXFLIGHTS = 10; + +class Airline +{ + char* name; + Flight* fl[MAXFLIGHTS]; //масив от указатели към обекти от тип Flight. + + int firstFreeIndex(); + + void CopyFrom(const Airline& other); + void Free(); + +public: + Airline(const char* name); + Airline(const Airline& other); + Airline& operator=(const Airline& other); + ~Airline(); + + bool addFlight(const char* originAirportName, const char* originAirportId, + const char* destAirportName, const char* destAirportId, int durration); + bool addFlight(const Flight& other); + + bool containsJourney(const char* origin, const char* dest); + + void print(); +}; diff --git a/Big4_and_composition/Airlines_booking_system/Airport.cpp b/Big4_and_composition/Airlines_booking_system/Airport.cpp new file mode 100644 index 0000000..d9f0c25 --- /dev/null +++ b/Big4_and_composition/Airlines_booking_system/Airport.cpp @@ -0,0 +1,56 @@ +#include "Airport.h" + +Airport::Airport(const char* cityName,const char* id) +{ + this->cityName = new char[strlen(cityName) + 1]; + strcpy(this->cityName, cityName); + + strcpy(this->id, id); +} + +void Airport::CopyFrom(const Airport& other) +{ + cityName = new char[strlen(other.cityName) + 1]; + strcpy(cityName, other.cityName); + + strcpy(id, other.id); //въпреки че не е динамично заделен, се налага до разпишем ръчно копирането, защото го разписваме за другите член-данни в класа. +} + +void Airport::Free() +{ + delete[] cityName; +} + +Airport::Airport(const Airport& other) +{ + CopyFrom(other); +} + +Airport& Airport::operator=(const Airport& other) +{ + if (this != &other) + { + Free(); + CopyFrom(other); + } + return *this; + +} + +Airport::~Airport() +{ + Free(); +} + +const char* Airport::getId() const +{ + return id; +} +const char* Airport::getCityName() const +{ + return cityName; +} +void Airport::print() const +{ + std::cout << cityName << '(' << id << ')'; +} diff --git a/Big4_and_composition/Airlines_booking_system/Airport.h b/Big4_and_composition/Airlines_booking_system/Airport.h new file mode 100644 index 0000000..98b3547 --- /dev/null +++ b/Big4_and_composition/Airlines_booking_system/Airport.h @@ -0,0 +1,22 @@ +#include <iostream> + +class Airport +{ + + char* cityName; + char id[4]; + + void CopyFrom(const Airport& other); + void Free(); + +public: + + Airport(const char* cityName,const char* id); + Airport(const Airport& other); + Airport& operator=(const Airport& other); + ~Airport(); + + const char* getId() const; + const char* getCityName() const; + void print() const; +};
\ No newline at end of file diff --git a/Big4_and_composition/Airlines_booking_system/Flight.cpp b/Big4_and_composition/Airlines_booking_system/Flight.cpp new file mode 100644 index 0000000..b7436a3 --- /dev/null +++ b/Big4_and_composition/Airlines_booking_system/Flight.cpp @@ -0,0 +1,27 @@ +#include "Flight.h" + +Flight::Flight(const char* originAirportName, const char* originAirportId, + const char* destAirportName, const char* destAirportId, int durration) : origin(originAirportName, originAirportId), dest(destAirportName,destAirportId) +{ + this->durration = durration; +} + +Flight::Flight(const Airport& _origin, const Airport& _dest, int durration) : origin(_origin), dest(_dest) +{ + this->durration = durration; +} +const Airport& Flight::getOrigin() +{ + return origin; +} +const Airport& Flight::getDestination() +{ + return dest; +} +void Flight::print() const +{ + origin.print(); + std::cout << "-->"; + dest.print(); + std::cout << std::endl; +}
\ No newline at end of file diff --git a/Big4_and_composition/Airlines_booking_system/Flight.h b/Big4_and_composition/Airlines_booking_system/Flight.h new file mode 100644 index 0000000..177aeaa --- /dev/null +++ b/Big4_and_composition/Airlines_booking_system/Flight.h @@ -0,0 +1,21 @@ +#include "Airport.h" + +class Flight +{ + + + Airport origin; + Airport dest; + int durration; + +public: + Flight(const char* originAirportName, const char* originAirportId, + const char* destAirportName, const char* destAirportId, int durration); + + Flight(const Airport& origin,const Airport& dest, int durration); + + const Airport& getOrigin(); + const Airport& getDestination(); + + void print() const; +};
\ No newline at end of file diff --git a/Big4_and_composition/Airlines_booking_system/Source.cpp b/Big4_and_composition/Airlines_booking_system/Source.cpp new file mode 100644 index 0000000..6d84dbd --- /dev/null +++ b/Big4_and_composition/Airlines_booking_system/Source.cpp @@ -0,0 +1,23 @@ + +#include "Airline.h" + + + +int main() +{ + + Airline a("Lufthansa"); + a.addFlight("Sofia", "SOF", "Berlin", "TGL", 33); + + a.addFlight("Berlin", "TGL", "London", "LTN", 55); + + a.addFlight("London", "LTN", "Sofia", "SOF", 66); + + a.addFlight("Berlin", "SCH", "Sofia", "SOF", 51); + + a.addFlight("London", "LTN", "Moscow", "MSC", 96); + + a.print(); + + +}
\ No newline at end of file |
