-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream.cpp
153 lines (117 loc) · 4.23 KB
/
stream.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
//
// Created by joaog on 10/25/2020.
//
#include <string>
#include "stream.h"
using namespace std;
unsigned int Stream::nextID= 0;
NotSubscribed::NotSubscribed(const string& reason) {
this->reason = reason;
}
string NotSubscribed::what() const {
return reason;
}
NoCapacity::NoCapacity(const string& reason) {
this->reason = reason;
}
string NoCapacity::what() {
return reason;
}
Stream::Stream(const string& title, const Date& startDate, const string& language, int minAge, const string& streamerNick) {
this->title = title, this->startDate = startDate, this->language = language, this->minAge = minAge, this->numViewers = 0;
this->streamerNick = streamerNick; noLikes = 0;
this->id = nextID, nextID++;
}
Stream::~Stream() = default;
void Stream::removeUser(unsigned int user) {
this->viewers.erase(viewers.find(user));
this->numViewers--;
}
int Stream::getNumViewers() const {
return this->numViewers;
}
string Stream::getTitle() const {
return this->title;
}
Date Stream::getStartDate() const {
return this->startDate;
}
string Stream::getLanguage() const {
return this->language;
}
int Stream::getMinAge() const {
return this->minAge;
}
int Stream::getNoLikes() const {
return this->noLikes;
}
string Stream::getStreamerNick() const {
return streamerNick;
}
std::set<unsigned int> & Stream::getViewers() {
return viewers;
}
void Stream::feedback(int megaLikezao) {
this->noLikes += megaLikezao;
}
void Stream::add50likes() {
this->noLikes += 50;
}
PrivateStream::PrivateStream(const string& title, const Date& startDate, const string& language, int minAge, const string& streamerNick,
std::set<unsigned int> &subscribers, int capacity) : Stream(title, startDate, language, minAge, streamerNick) {
this->subscribers = subscribers;
this->capacity = capacity;
}
PrivateStream::~PrivateStream() = default;
void PrivateStream::addUser(unsigned int user) {
if (numViewers >= capacity) throw NoCapacity("The stream is full. Try again later");
if (subscribers.find(user) == subscribers.end()) throw NotSubscribed("You are not subscribed to this streamer");
this->viewers.insert(user);
this->numViewers++;
}
void Stream::showStream() const {
cout << "Title: " << this->title << ", Start Date: " << this->startDate.getDate() << ", Language: "
<< this->language << ", Min Age: " << this->minAge << ", No. Likes: " << this->noLikes << ", No. Viewers: "
<< this->numViewers << endl << "Being streamed by " << streamerNick;
}
PublicStream::PublicStream(const string& title, const Date& startDate, const string& language, int minAge,
const string& streamerNick) : Stream(title, startDate, language, minAge, streamerNick) {}
PublicStream::~PublicStream() = default;
void PublicStream::showStream() const {
Stream::showStream();
cout << endl << "This stream is accessible to everyone" << endl;
}
void PublicStream::addUser(unsigned int user) {
this->viewers.insert(user);
this->numViewers++;
}
void PrivateStream::showStream() const {
Stream::showStream();
cout << endl << "This stream is only accessible to subscribers and has " << capacity - numViewers
<< " space"; if (capacity-numViewers != 1) cout << "s";
cout << "available" << endl << " and " << subscribers.size() << " subscriber";
if (subscribers.size() != 1) cout << "s"; cout << endl;
}
void PrivateStream::showMessages() const {
if (messages.empty()) cout << "You haven't received any messages yet :(" << endl;
else {
for (int i = 0; i < messages.size(); ++i) {
cout << messages.at(i) << endl << endl;
}
}
}
void PrivateStream::addMessage(const std::string& message) {
messages.push_back(message);
}
ostream& operator<<(ostream& out, Stream& stream){
out << "Title: " << stream.getTitle() << " Start Date: " << stream.getStartDate().getDate()
<< " Language: " << stream.getLanguage() << " Min Age: " << stream.getMinAge() << " No. Likes: "
<< stream.getNoLikes() << " No. Viewers: " << stream.getNumViewers();
return out;
}
unsigned int Stream::getId() const{
return this->id;
}
void Stream::setStreamerNick(const string& nick) {
this->streamerNick = nick;
}