Website Summary Files Commits Branches @commit @diff @stats
path: root / Week07 / Exercise01 / User.cpp
diff options
context:
space:
mode:
Commit945f7e67d6ca8047c961457786fac5e2d9a13d82 (patch)
Author
 Syndamia <kamen@syndamia.com>
2026-04-03 18:17:51 +0300
Committer
 Syndamia <kamen@syndamia.com>
2026-04-03 18:17:51 +0300
Files5c6b23aaee4eb653f9c461d7303edcd31d697f10 /Week07/Exercise01/User.cpp
oop-2025-solutions-945f7e67d6ca8047c961457786fac5e2d9a13d82.tar
oop-2025-solutions-945f7e67d6ca8047c961457786fac5e2d9a13d82.tar.gz
oop-2025-solutions-945f7e67d6ca8047c961457786fac5e2d9a13d82.zip
Parent8f162cde46a34d04e3a8a655067ee5174258e249 (diff)
feat(Week07): Add solutions
1 files changed, 128 insertions, 0 deletions
-rw-r--r--Week07/Exercise01/User.cpp128
diff --git a/Week07/Exercise01/User.cpp b/Week07/Exercise01/User.cpp
new file mode 100644
index 0000000..be5f76f
--- /dev/null
+++ b/Week07/Exercise01/User.cpp
@@ -0,0 +1,128 @@
+#include "User.h"
+#include <iostream>
+
+void User::copyFrom(const User& other) {
+ email = other.email;
+ password = other.password;
+ bankCard = other.bankCard;
+
+ libraryCount = other.libraryCount;
+ libraryAlloc = other.libraryAlloc;
+ library = new const Book*[libraryAlloc];
+ for (unsigned i = 0; i < libraryCount; ++i)
+ library[i] = other.library[i];
+}
+
+void User::free() {
+ delete[] library;
+}
+
+void User::resizeLibrary() {
+ libraryAlloc = libraryAlloc == 0 ? 8 : libraryAlloc * 2;
+ const Book** libraryLarger = new const Book*[libraryAlloc];
+
+ for (unsigned i = 0; i < libraryCount; ++i)
+ libraryLarger[i] = library[i];
+
+ delete[] library;
+ library = libraryLarger;
+}
+
+User::User() : email(), password(), bankCard(0), library(nullptr), libraryCount(0), libraryAlloc(0)
+{ }
+
+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) {
+ email = other.email;
+ password = other.password;
+ bankCard = other.bankCard;
+
+ library = other.library;
+ libraryCount = other.libraryCount;
+ libraryAlloc = other.libraryAlloc;
+
+ other.email.Clear();
+ other.password.Clear();
+ other.bankCard = 0;
+
+ other.library = nullptr;
+ other.libraryCount = other.libraryAlloc = 0;
+}
+
+User& User::operator=(User&& other) {
+ if (this != &other) {
+ free();
+
+ email = other.email;
+ password = other.password;
+ bankCard = other.bankCard;
+
+ library = other.library;
+ libraryCount = other.libraryCount;
+ libraryAlloc = other.libraryAlloc;
+
+ other.email.Clear();
+ other.password.Clear();
+ other.bankCard = 0;
+
+ other.library = nullptr;
+ other.libraryCount = other.libraryAlloc = 0;
+ }
+ return *this;
+}
+
+void User::SetEmail(const String& newEmail) {
+ email = newEmail;
+}
+
+const String& User::GetEmail() const {
+ return email;
+}
+
+void User::SetPassword(const String& newPassword) {
+ password = newPassword;
+}
+
+bool User::Login(const String& email, const String& password) {
+ return this->email.EqualTo(email) && this->password.EqualTo(password);
+}
+
+void User::AddBookToLibrary(const Book* book) {
+ if (libraryCount >= libraryAlloc)
+ resizeLibrary();
+ library[libraryCount++] = book;
+}
+
+void User::ListLibrary() {
+ for (unsigned i = 0; i < libraryCount; ++i) {
+ std::cout << '[' << i << "] ";
+ library[i]->PrintBookInfo();
+ }
+}
+
+const Book* User::GetBook(unsigned index) const {
+ return library[index];
+}
+
+void User::SetBankCard(unsigned newBankCard) {
+ bankCard = newBankCard;
+}
+
+unsigned User::GetBankCard() const {
+ return bankCard;
+}