aboutsummaryrefslogtreecommitdiff
path: root/week13/Exercise3
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2024-05-25 16:08:17 +0300
committerSyndamia <kamen@syndamia.com>2024-05-25 16:08:17 +0300
commit9c11fababb9b6b194e646fbeb62d141aacf74c43 (patch)
treef1a294fcd66b22463b9424c838ff171db8539a15 /week13/Exercise3
parentc6fae5f1a0cc8394b1de3b671f07de7b3d677c59 (diff)
downloadoop-2023-solutions-9c11fababb9b6b194e646fbeb62d141aacf74c43.tar
oop-2023-solutions-9c11fababb9b6b194e646fbeb62d141aacf74c43.tar.gz
oop-2023-solutions-9c11fababb9b6b194e646fbeb62d141aacf74c43.zip
[w13] Added solutions
Diffstat (limited to 'week13/Exercise3')
-rw-r--r--week13/Exercise3/Administrator.h6
-rw-r--r--week13/Exercise3/Administrator.h.gchbin0 -> 2140594 bytes
-rw-r--r--week13/Exercise3/Thread.cpp85
-rw-r--r--week13/Exercise3/Thread.h25
-rw-r--r--week13/Exercise3/ThreadModerator.cpp24
-rw-r--r--week13/Exercise3/ThreadModerator.h9
-rw-r--r--week13/Exercise3/User.cpp61
-rw-r--r--week13/Exercise3/User.h24
-rw-r--r--week13/Exercise3/UserModerator.cpp9
-rw-r--r--week13/Exercise3/UserModerator.h8
10 files changed, 251 insertions, 0 deletions
diff --git a/week13/Exercise3/Administrator.h b/week13/Exercise3/Administrator.h
new file mode 100644
index 0000000..ae34759
--- /dev/null
+++ b/week13/Exercise3/Administrator.h
@@ -0,0 +1,6 @@
+#pragma once
+#include "UserModerator.h"
+#include "ThreadModerator.h"
+
+class Administrator : public UserModerator, public ThreadModerator {
+};
diff --git a/week13/Exercise3/Administrator.h.gch b/week13/Exercise3/Administrator.h.gch
new file mode 100644
index 0000000..295e5f4
--- /dev/null
+++ b/week13/Exercise3/Administrator.h.gch
Binary files differ
diff --git a/week13/Exercise3/Thread.cpp b/week13/Exercise3/Thread.cpp
new file mode 100644
index 0000000..6cd13ba
--- /dev/null
+++ b/week13/Exercise3/Thread.cpp
@@ -0,0 +1,85 @@
+#include "Thread.h"
+#include <cstring>
+
+void Thread::resize() {
+ this->allocated *= 2;
+ char** moreMessages = new char*[this->allocated];
+ for (int i = 0; i < this->size; i++) {
+ moreMessages[i] = this->messages[i];
+ }
+ delete[] this->messages;
+ this->messages = moreMessages;
+}
+
+void Thread::free() {
+ for (int i = 0; i < size; i++) {
+ delete[] messages[i];
+ }
+ delete[] messages;
+}
+
+void Thread::copyFrom(const Thread& other) {
+ this->size = other.size;
+ this->allocated = other.allocated;
+ this->messages = new char*[allocated];
+ for (int i = 0; i < size; i++) {
+ this->messages[i] = new char[strlen(other.messages[i] + 1)];
+ strcpy(this->messages[i], other.messages[i]);
+ }
+}
+
+Thread::Thread() {
+ this->messages = nullptr;
+ this->size = this->allocated = 0;
+}
+
+Thread::~Thread() {
+ free();
+}
+
+Thread::Thread(const Thread& other) {
+ copyFrom(other);
+}
+
+Thread& Thread::operator=(const Thread& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+Thread::Thread(Thread&& other) {
+ this->size = other.size;
+ this->allocated = other.allocated;
+ this->messages = other.messages;
+ other.messages = nullptr;
+}
+
+Thread& Thread::operator=(Thread&& other) {
+ if (this != &other) {
+ free();
+
+ this->size = other.size;
+ this->allocated = other.allocated;
+ this->messages = other.messages;
+ other.messages = nullptr;
+ }
+ return *this;
+}
+
+void Thread::PostMessage(const User& poster, const char* message) {
+ if (poster.IsBanned()) {
+ throw "User cannot post!";
+ }
+ if (this->size == this->allocated) {
+ resize();
+ }
+
+ char* newMessage = new char[strlen(poster.GetUsername()) + 1 + strlen(message) + 1];
+ strcpy(newMessage, poster.GetUsername());
+ strcat(newMessage, " "); // За да знаем къде свършва потребителското име
+ strcat(newMessage, message);
+
+ this->messages[this->size++] = newMessage;
+}
diff --git a/week13/Exercise3/Thread.h b/week13/Exercise3/Thread.h
new file mode 100644
index 0000000..3b0a588
--- /dev/null
+++ b/week13/Exercise3/Thread.h
@@ -0,0 +1,25 @@
+#pragma once
+#include "User.h"
+
+class Thread {
+ char** messages;
+ unsigned size;
+ unsigned allocated;
+
+ void resize();
+
+ void free();
+ void copyFrom(const Thread& other);
+
+public:
+ Thread();
+ virtual ~Thread();
+ Thread(const Thread& other);
+ Thread& operator=(const Thread& other);
+ Thread(Thread&& other);
+ Thread& operator=(Thread&& other);
+
+ void PostMessage(const User& poster, const char* message);
+
+ friend class ThreadModerator;
+};
diff --git a/week13/Exercise3/ThreadModerator.cpp b/week13/Exercise3/ThreadModerator.cpp
new file mode 100644
index 0000000..047b0ec
--- /dev/null
+++ b/week13/Exercise3/ThreadModerator.cpp
@@ -0,0 +1,24 @@
+#include "ThreadModerator.h"
+#include <cstring>
+
+void ThreadModerator::RemoveMessage(Thread& thread, unsigned index) {
+ if (index >= thread.size) {
+ throw "Index out of range!";
+ }
+
+ delete[] thread.messages[index];
+ for (unsigned i = index; i < thread.size - 1; i++) {
+ thread.messages[i] = thread.messages[i-1];
+ }
+ thread.size--;
+}
+
+void ThreadModerator::RemoveByUser(Thread& thread, const User& poster) {
+ unsigned nameLen = strlen(poster.GetUsername());
+ for (unsigned i = 0; i < thread.size; i++) {
+ if (strncmp(thread.messages[i], poster.GetUsername(), nameLen) == 0) {
+ RemoveMessage(thread, i);
+ i--;
+ }
+ }
+}
diff --git a/week13/Exercise3/ThreadModerator.h b/week13/Exercise3/ThreadModerator.h
new file mode 100644
index 0000000..2872206
--- /dev/null
+++ b/week13/Exercise3/ThreadModerator.h
@@ -0,0 +1,9 @@
+#pragma once
+#include "User.h"
+#include "Thread.h"
+
+class ThreadModerator : virtual public User {
+public:
+ void RemoveMessage(Thread& thread, unsigned index);
+ void RemoveByUser(Thread& thread, const User& poster);
+};
diff --git a/week13/Exercise3/User.cpp b/week13/Exercise3/User.cpp
new file mode 100644
index 0000000..e87a665
--- /dev/null
+++ b/week13/Exercise3/User.cpp
@@ -0,0 +1,61 @@
+#include "User.h"
+#include <cstring>
+
+void User::free() {
+ delete[] name;
+ delete[] password;
+}
+
+void User::copyFrom(const User& other) {
+ this->name = new char[strlen(other.name) + 1];
+ strcpy(this->name, other.name);
+ this->password = new char[strlen(other.password) + 1];
+ strcpy(this->password, other.password);
+}
+
+User::User() {
+ this->name = this->password = nullptr;
+}
+
+User::~User() {
+ free();
+}
+
+User::User(const User& other) {
+ copyFrom(other);
+}
+
+User& User::operator=(const User& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+}
+
+User::User(User&& other) {
+ this->name = other.name;
+ other.name = nullptr;
+ this->password = other.password;
+ other.password = nullptr;
+}
+
+User& User::operator=(User&& other) {
+ if (this != &other) {
+ free();
+
+ this->name = other.name;
+ other.name = nullptr;
+ this->password = other.password;
+ other.password = nullptr;
+ }
+ return *this;
+}
+
+bool User::IsBanned() const {
+ return banned;
+}
+
+const char* User::GetUsername() const {
+ return name;
+}
diff --git a/week13/Exercise3/User.h b/week13/Exercise3/User.h
new file mode 100644
index 0000000..e742181
--- /dev/null
+++ b/week13/Exercise3/User.h
@@ -0,0 +1,24 @@
+#pragma once
+
+class User {
+ void free();
+ void copyFrom(const User& other);
+
+protected:
+ char *name;
+ char *password;
+ bool banned;
+
+public:
+ User();
+ virtual ~User();
+ User(const User& other);
+ User& operator=(const User& other);
+ User(User&& other);
+ User& operator=(User&& other);
+
+ bool IsBanned() const;
+ const char* GetUsername() const;
+
+ friend class UserModerator;
+};
diff --git a/week13/Exercise3/UserModerator.cpp b/week13/Exercise3/UserModerator.cpp
new file mode 100644
index 0000000..51f10e4
--- /dev/null
+++ b/week13/Exercise3/UserModerator.cpp
@@ -0,0 +1,9 @@
+#include "UserModerator.h"
+
+void UserModerator::BanUser(User& user) {
+ user.banned = true;
+}
+
+void UserModerator::UnbanUser(User& user) {
+ user.banned = false;
+}
diff --git a/week13/Exercise3/UserModerator.h b/week13/Exercise3/UserModerator.h
new file mode 100644
index 0000000..dfed054
--- /dev/null
+++ b/week13/Exercise3/UserModerator.h
@@ -0,0 +1,8 @@
+#pragma once
+#include "User.h"
+
+class UserModerator : virtual public User {
+public:
+ void BanUser(User& user);
+ void UnbanUser(User& user);
+};