Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / Exam 2 Preparation / Garage.cpp
blob: 34902932f91b0f80d4d4a8a4f3403169418807f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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();
}