-
Notifications
You must be signed in to change notification settings - Fork 548
/
Copy path05_homework_08_answer.cpp
76 lines (67 loc) · 2.08 KB
/
05_homework_08_answer.cpp
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
#include <bits/stdc++.h>
using namespace std;
class CarSpecs {
private:
string trim;
string engine_type;
pair<int, int> horsepower;
string steering_ratio;
// and more
public:
string& GetEngineType() {
return engine_type;
}
void SetEngineType( string& engineType) {
engine_type = engineType;
}
pair<int, int> GetHorsepower() {
return horsepower;
}
void SetHorsepower(pair<int, int> horsepower) {
this->horsepower = horsepower;
}
string& GetSteeringRatio() {
return steering_ratio;
}
void SetSteeringRatio( string& steeringRatio) {
steering_ratio = steeringRatio;
}
string& GetTrim() {
return trim;
}
void SetTrim( string& trim) {
this->trim = trim;
}
};
class AutoTrader {
private:
vector<CarSpecs> current_cars_vec;
public:
void LoadDatabase() {
// Fill current_cars_vec
}
bool search_match( CarSpecs &query_car) {
for(auto available_car : current_cars_vec) {
if(available_car.GetEngineType() != query_car.GetEngineType()) continue;
if(available_car.GetHorsepower() != query_car.GetHorsepower()) continue;
if(available_car.GetSteeringRatio() != query_car.GetSteeringRatio()) continue;
if(available_car.GetTrim() != query_car.GetTrim()) continue;
return true;
}
return false;
}
};
/*
1- Above code violates encapsulation concept by forcing others to think how to compare to objects
The right way, CarSpecs provides is_equal functionality
2- Also from code change perspective, every time we change the data members in CarSpecs (e.g. details of exterior shape)
We will have to change AutoTrader class (2 classes changed).
If CarSpecs provides is_equal functionality, other users won't need to do/care about the changes
as they are still handled
3- On the other side, the code did not follow this nice tip:
“Don’t ask for the information you need to do the work; ask the object that has the information to do the work for you.” Allen Holub
We asked for all the gets to do the comparison! This should be the class responsibility
*/
int main() {
return 0;
}