diff options
Added my prep work for exam2
1 files changed, 59 insertions, 0 deletions
| -rw-r--r-- | Year 1/Object-oriented Programming/Exam 2 Preparation/Garage.cpp | 59 |
diff --git a/Year 1/Object-oriented Programming/Exam 2 Preparation/Garage.cpp b/Year 1/Object-oriented Programming/Exam 2 Preparation/Garage.cpp new file mode 100644 index 0000000..3490293 --- /dev/null +++ b/Year 1/Object-oriented Programming/Exam 2 Preparation/Garage.cpp @@ -0,0 +1,59 @@ +#include "Garage.h" + +void Garage::copyFrom(const Garage& other) { + vehicles = new Vehicle*[other.maxCapacity]; + for (unsigned i = 0; i < other.count; i++) + vehicles[i] = other.vehicles[i]->clone(); + count = other.count; + maxCapacity = other.maxCapacity; +} + +void Garage::free() { + for (unsigned i = 0; i < count; i++) + delete vehicles[i]; + delete[] vehicles; +} + +Garage::Garage(unsigned maxCapacity) { + if (maxCapacity == 0) + maxCapacity = 1; + + vehicles = new Vehicle*[maxCapacity]; + this->maxCapacity = maxCapacity; + count = 0; +} + +bool Garage::parkVehicle(const Vehicle& vehicle) { + if (count == maxCapacity) + return false; + + vehicles[count++] = vehicle.clone(); + return true; +} + +unsigned Garage::countOfCapableVehiclesForOrder(Road road, double roadLength, double time) { + unsigned counter = 0; + for (unsigned i = 0; i < count; i++) { + if (vehicles[i]->getSpeedOnRoad(road) >= roadLength / time) + counter++; + } + return counter; +} + +Garage::Garage() : Garage(1) {} + +Garage::Garage(const Garage& other) { + copyFrom(other); +} + +Garage& Garage::operator=(const Garage& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +Garage::~Garage() { + free(); +} |
