-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_reservation.h
98 lines (72 loc) · 2.65 KB
/
common_reservation.h
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
//
// Created by hussein on 8/1/22.
//
#ifndef EXPEDIA_COM_COMMON_RESERVATION_H
#define EXPEDIA_COM_COMMON_RESERVATION_H
class Reservation {
public:
virtual Reservation* Clone() const = 0;
virtual double TotalCost() const = 0;
virtual string ToString() const = 0;
virtual ~Reservation() {
}
};
// You might think: let's create class HotelFlight reservation
// What in future about reservations for cars? Cruise? whatever?
// Think in future: can we handle things in generic way?
class ItineraryReservation: public Reservation {
protected:
vector<Reservation*> reservations; // As has pointers, we need copy constructor
public:
ItineraryReservation() {
}
ItineraryReservation(const ItineraryReservation& another_reservation) { // copy constructor
for (const Reservation* reservation : another_reservation.GetReservations())
AddReservation(*reservation);
}
void AddReservation(const Reservation& reservation) {
reservations.push_back(reservation.Clone());
}
virtual double TotalCost() const {
double cost = 0;
for (const Reservation* reservation : reservations)
cost += reservation->TotalCost();
return cost;
}
~ItineraryReservation() {
Clear();
}
const vector<Reservation*>& GetReservations() const {
return reservations;
}
void Clear() {
for (const Reservation* reservation : reservations)
delete reservation;
reservations.clear();
}
virtual string ToString() const override {
ostringstream oss;
oss <<"**********************************************\n";
oss << "Itinerary of " << reservations.size() << " sub-reservations\n";
for (const Reservation* reservation : reservations)
oss << reservation->ToString() << "\n";
oss << "Total Itinerary cost: " << TotalCost() << "\n";
oss <<"**********************************************\n";
return oss.str();
}
virtual Reservation* Clone() const override {
return new ItineraryReservation(*this);
}
};
class ReservationsSet: public ItineraryReservation {
public:
// We need another class that act like set of different reservations (not Itinerary)
// This has same functionalities as ItineraryReservation
// It is much better to have its own class, even empty class
// Then code is more semantic + we can do future changes easily
using ItineraryReservation::ItineraryReservation; // use its copy constructor
virtual Reservation* Clone() const {
return new ReservationsSet(*this);
}
};
#endif //EXPEDIA_COM_COMMON_RESERVATION_H