Website Summary Files Commits Branches @commit @diff @stats
path: root / Week07 / Exercise01 / User.cpp
blob: be5f76f6cf94b13bd4d204c59e0c2cd1ff978d8b (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
#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;
}