Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / Project 1 / src / Models / Event.cpp
blob: c4557b7d927cd4ab150aa6f79fbcebcbaecf78a9 (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
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
#include "Event.h"
#include "Reservation.h"

Event::Event() : hall(), name(), date() {}

Event::Event(const Hall& hall, String name, Date date) : tickets(), reservations() {
	this->hall = hall;
	this->name = name;
	this->date = date;
}

OrderedList<Ticket>& Event::get_tickets() {
	return tickets;
}

const OrderedList<Ticket>& Event::get_tickets() const {
	return tickets;
}

OrderedList<Reservation>& Event::get_reservations() {
	return reservations;
}

const OrderedList<Reservation>& Event::get_reservations() const {
	return reservations;
}

const Hall& Event::get_hall() const {
	return hall;
}

const String& Event::get_name() const {
	return name;
}

const Date& Event::get_date() const {
	return date;
}

/*! \param istr An input stream
 *
 * Directly reads bytes from stream (calls read() function).
 * Stored types must have defined a read() function.
 * 
 * \remark Doesn't alter the stream in any other way.
 * \note Best used with binary ifstream
 */
void Event::read(std::istream& istr) {
	hall.read(istr);
	name.read(istr);
	date.read(istr);
	tickets.read(istr);
	reservations.read(istr);
}

/*! \param ostr An output stream
 *
 * Directly writes bytes to stream (calls write() function).
 * Stored types must have defined a read() function.
 * 
 * \remark Doesn't alter the stream in any other way.
 * \note Best used with binary ofstream
 */
void Event::write(std::ostream& ostr) {
	hall.write(ostr);
	name.write(ostr);
	date.write(ostr);
	tickets.write(ostr);
	reservations.write(ostr);
}

/*! \return compare of halls, or if they are equal, compare of dates
 */
int Event::compare(const Event& other) {
	if (hall.compare(other.hall) == 0)
		return date.compare(other.date);
	return hall.compare(other.hall);
}

/*!
 * Uses the stream's >> operator to read and parse all internal values.
 * Stored types must have defined operator >>.
 *
 * \note Best used with std::cin or text std::ifstream
 */
std::istream& operator>>(std::istream& istr, Event& event) {
	Hall newHall;
	String newString;
	Date newDate;
	OrderedList<Ticket> newTickets;
	OrderedList<Reservation> newReservation;

	istr >> newHall >> newString >> newDate >> newTickets >> newReservation;
	event = Event(newHall, newString, newDate);
	event.get_tickets() = newTickets;
	event.get_reservations() = newReservation;

	return istr;
}

unsigned insertionOpSet = 0;

/*! \param setting A number, defining the different options. It's best if it is passed as binary number.
 *
 * Sets a configuration setting for how operator<< should behave. Setting is stored as a global variable in the cpp file, so it's independent of Event instance.
 *
 * Each bit in the number is a certain setting. Each bit from left to right represents one of these options, from top to bottom:
 * 1. Write count of bought tickets (available only if 5. is set)
 * 2. Write count of reserved tickets (available only if 5. is set)
 * 3. Write reservations
 * 4. Write bought tickets
 * 5. Write Hall information
 *
 * \par Example
 * To configure printing the Hall information, bought tickets count and the bought tickets themselves, you would do:
 * \code{.cpp}
 * configureEventInsertionOp(0b10011);
 * \endcode
 */
void configureEventInsertionOp(unsigned setting) {
	insertionOpSet = setting;
}

/*!
 * Uses the stream's << operator to write all internal data, following setting from configureEventInsertionOp()
 * Stored types must have defined operator <<.
 *   
 * \note Best used with std::cout or text std::ofstream
 */
std::ostream& operator<<(std::ostream& ostr, const Event& event) {
	if (insertionOpSet & 1) {
		ostr << "| Hall: " << event.get_hall().get_number() << " | Name: " << event.get_name() << " | Date: " << event.get_date();

		if (insertionOpSet & 8)
			ostr << " || Tickets Count: " << event.get_tickets().get_count();
		if (insertionOpSet & 16)
			ostr << " || Reservations Count: " << event.get_reservations().get_count();
		ostr << std::endl;
	}
	if (insertionOpSet & 2) {
		ostr << "Bought tickets: ";
		for (unsigned i = 0; i < event.get_tickets().get_count(); i++)
			ostr << "[" << event.get_tickets()[i] << "] ";
		ostr << std::endl;
	}
	if(insertionOpSet & 4) {
		ostr << "Reserved tickets: ";
		for (unsigned i = 0; i < event.get_reservations().get_count(); i++)
			ostr << "[" << event.get_reservations()[i] << "] ";
		ostr << std::endl;
	}
	return ostr;
}