๐Ÿ“ฆ andrebreis / AEDA_Project

๐Ÿ“„ Vehicle.cpp ยท 170 lines
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170#include <stdlib.h>
#include "Vehicle.h"
#include "ConfigFile.h"

// ================================= VEHICLE ================================= //

Vehicle::Vehicle(string manufacturer, string model, string licensePlate) {
    this->manufacturer = manufacturer;
    this->model = model;
    this->licensePlate = licensePlate;
}

Vehicle::Vehicle(istream &in) {
    string testString = "";
    int classIdentifier = 0;
    in >> this->manufacturer >> this->model;
    in >> this->licensePlate;
}

void Vehicle::saveObjectInfo(ostream &out) {
    out << this->classIdentifier() << endl
    << this->manufacturer << " " << this->model << endl;
    out << this->licensePlate;
}

bool Vehicle::addService(Service *s1) {
    return addsIfNotExist(s1, services);
}

bool Vehicle::removeService(Service *s1) {
    int index = sequentialSearch(services, s1);
    if (index == -1)
        return false;
    services.erase(services.begin() + index);
    return true;
}

bool operator==(const Vehicle &v1, const Vehicle &v2) {
    return v1.getLicensePlate() == v2.getLicensePlate();
}

// ================================= AUTOMOBILE ================================= //

Automobile::Automobile(string manufacturer, string model, string licensePlate, int numDoors)
        : Vehicle(manufacturer, model, licensePlate) {
    this->numDoors = numDoors;
}

Automobile::Automobile(istream &in) : Vehicle(in) {
    in >> this->numDoors;
}

void Automobile::saveObjectInfo(ostream &out) {
    Vehicle::saveObjectInfo(out);
    out << endl << this->numDoors;
}

// ================================= MOTORCYCLE ================================= //

Motorcycle::Motorcycle(string manufacturer, string model, string licensePlate, string type)
        : Vehicle(manufacturer, model, licensePlate) {
    this->type = type;
}

Motorcycle::Motorcycle(istream &in) : Vehicle(in) {
    in >> this->type;
}

void Motorcycle::saveObjectInfo(ostream &out) {
    Vehicle::saveObjectInfo(out);
    out << endl << this->type;
}

// ================================= TRUCK ================================= //

Truck::Truck(string manufacturer, string model, string licensePlate, int maxWeight)
        : Vehicle(manufacturer, model, licensePlate) {
    this->maxWeight = maxWeight;
}

Truck::Truck(istream &in) : Vehicle(in) {
    in >> this->maxWeight;
}

void Truck::saveObjectInfo(ostream &out) {
    Vehicle::saveObjectInfo(out);
    out << endl << this->maxWeight;
}

// ================================= BUS ================================= //

Bus::Bus(string manufacturer, string model, string licensePlate, int numSittingSpots,
         int numStandingSpots)
        : Vehicle(manufacturer, model, licensePlate) {
    this->numSittingSpots = numSittingSpots;
    this->numStandingSpots = numStandingSpots;
}

Bus::Bus(istream &in) : Vehicle(in) {
    in >> this->numSittingSpots >> this->numStandingSpots;
}

void Bus::saveObjectInfo(ostream &out) {
    Vehicle::saveObjectInfo(out);
    out << endl << this->numSittingSpots << " " << this->numStandingSpots;
}

Vehicle *createVehicleObject(istream &in, int classIdentifier) {
    Vehicle *newVehicle;
    switch (classIdentifier) {
        case 1:
            newVehicle = new Automobile(in);
            break;
        case 2:
            newVehicle = new Motorcycle(in);
            break;
        case 3:
            newVehicle = new Truck(in);
            break;
        case 4:
            newVehicle = new Bus(in);
            break;
        default:
            string file = "Vehicles file";
            ConfigFile::BadFileException e(file);
            throw (e);
    }
    return newVehicle;
}

void Vehicle::printObjectInfo() const {
    cout << "Manufacturer and Model: " << this->manufacturer << " " << this->model << endl
    << "License Plate: " << this->licensePlate;
    for (size_t i = 0; i < services.size(); i++) {
        cout << endl;
        services[i]->printObjectInfo();
    }
}

void Automobile::printObjectInfo() const {
    Vehicle::printObjectInfo();
    cout << endl << "Automobile number of doors: " << this->numDoors;
}

void Motorcycle::printObjectInfo() const {
    Vehicle::printObjectInfo();
    cout << endl << "Motorcycle type: " << this->type;
}

void Truck::printObjectInfo() const {
    Vehicle::printObjectInfo();
    cout << endl << "Truck's maximum weight: " << this->maxWeight;
}

void Bus::printObjectInfo() const {
    Vehicle::printObjectInfo();
    cout << endl << "Bus' number of Sitting - Standing spots: " << this->numSittingSpots << " - " <<
    this->numStandingSpots;
}

void Vehicle::printServices() const {
    if (services.size() == 0)
        cout << "Vehicle " << this->manufacturer << " " << this->model << " never got any services from us.";
    for (size_t i = 0; i < services.size(); i++) {
        services[i]->printObjectInfo();
        if (i != services.size() - 1)
            cout << endl;
    }
}