-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.cpp
344 lines (272 loc) · 9.34 KB
/
user.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//
// Created by bruno on 25/10/2020.
//
#include <iostream>
#include <string>
#include <stack>
#include <iomanip>
#include "purchase.h"
#include "user.h"
using namespace std;
// Exceptions
NotOldEnough::NotOldEnough(const string& reason) {
this->reason = reason;
}
string NotOldEnough::what() const {
return reason;
}
NotInAStream::NotInAStream(const string& reason) {
this->reason = reason;
}
string NotInAStream::what() const {
return reason;
}
AlreadyInAStream::AlreadyInAStream(const string& reason) {
this->reason = reason;
}
string AlreadyInAStream::what() const {
return reason;
}
unsigned int User::nextID = 0;
int Streamer::merchSalesLimit = 50;
User::User(const std::string& name, const std::string& nick, const Date& birthDate, const int activity) {
this->name = name;
this->nick = nick;
this->birthDate = birthDate;
ID = nextID;
stream = nullptr;
this->active = activity;
}
User::User(const std::string& name, const std::string& nick, const Date& birthDate, const int activity, unsigned int id) {
this->name = name;
this->nick = nick;
this->birthDate = birthDate;
this->ID = id;
stream = nullptr;
this->active = activity;
}
User::~User() = default;
void User::setStreamHistory(std::set<unsigned int>& pastStreams) {
for(auto it = pastStreams.begin(); it != pastStreams.end(); it++)
this->streamHistory.insert(*it);
}
void User::setName(const std::string& name) {
this->name = name;
}
void User::setNick(const std::string &nick) {
this->nick = nick;
}
std::string User::getName() const {
return name;
}
unsigned int User::getAge() const {
return birthDate.getAge();
}
string User::getAgeString() const {
return birthDate.getDate();
}
std::string User::getNick() const {
return nick;
}
unsigned int User::getID() const {
return ID;
}
Stream * User::getStream() const {
if (stream == nullptr) throw NotInAStream(this->nick + " is currently not in any stream");
return stream;
}
bool User::inAStream() const {
return stream != nullptr;
}
std::set<unsigned int>& User::getStreamHistory() {
return streamHistory;
}
int User::getActivity() {
return this->active;
}
void User::setActivity(int activity) {
this->active = activity;
}
Streamer::Streamer(const std::string& name, const std::string& nick, const Date& birthDate, const int activity) : User(name, nick, birthDate, activity){
if (birthDate.getAge() < 15) throw NotOldEnough("You must be at least 15 years old to create a streamer account");
nextID++; // only after the check
this->soldMerch = 0;
}
Streamer::Streamer(const std::string& name, const std::string& nick, const Date& birthDate, const int activity, unsigned int id, int soldMerch) : User(name, nick, birthDate, activity, id) {
if (birthDate.getAge() < 15) throw NotOldEnough("You must be at least 15 years old to create a streamer account");
nextID++; // only after the check
this->soldMerch = soldMerch;
}
Streamer::~Streamer() = default;
int Streamer::getNumViewers() const {
if (stream == nullptr) throw NotInAStream(this->nick + " is currently not streaming");
return stream->getNumViewers();
}
int Streamer::getNumSubs() const {
return subscribers.size();
}
std::set<unsigned int>& Streamer::getSubscribers() {
return this->subscribers;
}
void Streamer::endStream() {
if (stream == nullptr) throw NotInAStream(this->nick + " is currently not streaming");
streamHistory.insert(stream->getId());
stream = nullptr;
}
void Streamer::startStream(Stream *stream) {
if (this->stream != nullptr) throw AlreadyInAStream(this->nick + " is already streaming");
this->stream = stream;
}
void Streamer::setSubscribers(set<unsigned int>& subscribers) {
this->subscribers = subscribers;
}
void Streamer::addSubscriber(unsigned int id) {
subscribers.insert(id);
}
void Streamer::removeSubscriber(unsigned int id) {
set<unsigned int>::iterator it = subscribers.find(id);
if (it == subscribers.end()) throw NotSubscribed("That user is not subscribed to " + this->nick);
subscribers.erase(it);
}
bool Streamer::isSubscriber(unsigned int id) const {
return subscribers.find(id) != subscribers.end();
}
void Streamer::showUser() const {
cout << nick << " (" << name << ")" << endl
<< getAge() << " Years Old" << endl << getNumSubs() << " Subscriber";
if (getNumSubs() != 1) cout << "s"; cout << endl;
if (!active){
cout << "This Streamer has his account deleted" << endl;
return;
}
if (stream != nullptr) {
cout << "Currently streaming " << stream->getTitle() << " with "
<< getNumViewers() << " viewer";
if (getNumViewers() != 1) cout << "s";
cout << endl;
}
else{
cout << "Currently not streaming" << endl;
}
}
priority_queue<Purchase>& Streamer::getPurchases(){
return this->purchases;
}
bool Streamer::hasPurchase(string name){
priority_queue<Purchase> aux = this->getPurchases();
while(!aux.empty()){
if( aux.top().getName() == name )
return true;
aux.pop();
}
return false;
}
void Streamer::removePurchase(string name) {
priority_queue<Purchase> aux;
while(!this->purchases.empty()){
if(this->purchases.top().getName() == name){
this->purchases.pop();
break;
}
else
aux.push(this->purchases.top());
this->purchases.pop();
}
while(!aux.empty()){
this->purchases.push(aux.top());
aux.pop();
}
}
void Streamer::addPurchase(string name, int numProducts, int availability){
Purchase p(name, numProducts, availability);
this->purchases.push(p);
}
void Streamer::setPurchases(priority_queue<Purchase>& purchases){
this->purchases = purchases;
}
void Streamer::showMerchPurchases(){
priority_queue<Purchase> aux = this->purchases;
cout << setfill(' ') << setw(15) << "Name" << " |" << setw(10) << "No. Items" << " |" << setw(10) << " Availability" << endl;
while(!aux.empty()){
cout << aux.top();
aux.pop();
}
}
int Streamer::getSoldMerch(){
return this->soldMerch;
}
int Streamer::getMerchSalesLimit() {
return this->merchSalesLimit;
}
void Streamer::addSoldMerch(int numOfMerch) {
this->soldMerch += numOfMerch;
}
void Streamer::setNewSalesLimit(int newLimit) {
Streamer::merchSalesLimit = newLimit;
}
ostream& operator<<(ostream& out, const Streamer& streamer){
out << streamer.getNick() << "( " << streamer.getName() << " )" << endl
<< streamer.getAge() << " Years Old" << endl << streamer.getNumSubs() << " Subscriber";
if (streamer.getNumSubs() != 1) cout << "s"; cout << endl;
if (streamer.inAStream()) {
out << "Currently streaming " << streamer.getStream()->getTitle() << " with "
<< streamer.getNumViewers() << " viewer";
if (streamer.getNumViewers() != 1) cout << "s";
cout << endl;
}
else{
out << "Currently not streaming" << endl;
}
out << endl;
return out;
}
Viewer::Viewer(const std::string& name, const std::string& nick, const Date& birthDate, const int activity) : User(name, nick, birthDate, activity) {
if (birthDate.getAge() < 12) throw NotOldEnough("You must be at least 12 years old to create an account");
nextID++; // only after the check
}
Viewer::Viewer(const std::string& name, const std::string& nick, const Date& birthDate, const int activity, unsigned int id) : User(name, nick, birthDate, activity, id) {
if (birthDate.getAge() < 12) throw NotOldEnough("You must be at least 12 years old to create an account");
nextID++; // only after the check
}
Viewer::~Viewer() = default;
void Viewer::joinStream(Stream *stream) {
if (this->stream != nullptr) throw AlreadyInAStream(this->nick + " is already watching a stream");
if (getAge() < stream->getMinAge())
throw NotOldEnough(this->name + " is not old enough to watch " + stream->getTitle());
this->stream = stream;
}
void Viewer::leaveStream() {
if (stream == nullptr) throw NotInAStream(this->nick + " is currently not watching any stream");
this->streamHistory.insert(this->stream->getId());
stream = nullptr;
}
void Viewer::feedback(int like) {
if (stream == nullptr) throw NotInAStream(this->nick + " can't give feedback because he's not watching any stream");
stream->feedback(like);
}
void Viewer::message(const std::string& text) const {
if (stream == nullptr) throw NotInAStream(this->nick + " can't message because he's not watching any stream");
PrivateStream* s = (PrivateStream*) stream;
s->addMessage(nick + ": " + text);
}
void Viewer::showUser() const {
cout << nick << " (" << name << ")" << endl
<< getAge() << " Years Old" << endl;
if (!active){
cout << "This Viewer has his account deleted" << endl;
}
if (stream != nullptr)
cout << "Currently watching " << stream->getTitle() << endl;
else
cout << "Currently not watching any stream" << endl;
}
ostream& operator<<(ostream& out, const Viewer& viewer){
out << viewer.getNick() << "( " << viewer.getName() << " )" << endl
<< viewer.getAge() << " Years Old" << endl;
if (viewer.inAStream())
out << "Currently watching " << viewer.getStream()->getTitle() << endl;
else
out << "Currently not watching any stream" << endl;
out << endl;
return out;
}