aboutsummaryrefslogtreecommitdiff
path: root/week02/Exercise1.cpp
blob: cb0e0c28a010d3bd2a4a9b033458c8fa30d8cbb2 (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
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
#include <iostream>
#include <cstring>

struct Book {
private:
	char title[1024];
	char** authors;
	int authorsCount;
	int published;
	float price;

	void deleteAuthors() {
		for (int i = 0; i < authorsCount; i++) {
			delete[] authors[i];
		}
		delete[] authors;
	}

public:
	Book(const char* title, char** authors, int authorsCount, int published, float price) {
		setTitle(title);
		this->authorsCount = 0;
		setAuthors(authors, authorsCount);
		setPublished(published);
		setPrice(price);
	}

	~Book() {
		deleteAuthors();
	}

	char* getTitle() {
		return title;
	}
	bool setTitle(const char* newTitle) {
		if (newTitle[0] == '\0') return false;

		strcpy(title, newTitle);
		return true;
	}

	int getAuthors(char** ref) {
		ref = authors;
		return authorsCount;
	}
	bool setAuthors(char** newAuthors, int count) {
		if (count == 0) return false;

		deleteAuthors();

		authors = new char*[count];
		for (int i = 0; i < count; i++) {
			if (newAuthors[i][0] == '\0') continue;

			authors[i] = new char[strlen(newAuthors[i]) + 1];
			strcpy(authors[i], newAuthors[i]);
		}
		return true;
	}

	int getPublished() {
		return published;
	}
	bool setPublished(int newYear) {
		if (newYear < -2100 || newYear > 2024) return false;

		published = newYear;
		return true;
	}

	float getPrice() {
		return price;
	}
	bool setPrice(float newPrice) {
		if (newPrice <= 0) return false;

		price = newPrice;
		return true;
	}
};

struct Library {
private:
	Book** books;
	int* counts;
	int amountOfBooks;
	int reservedSpace;

	void resize() {
		reservedSpace *= 2;

		Book** newBooks = new Book*[reservedSpace];
		for (int i = 0; i < amountOfBooks; i++) {
			newBooks[i] = books[i];
		}
		delete[] books;
		books = newBooks;

		int* newCounts = new int[reservedSpace];
		for (int i = 0; i < amountOfBooks; i++) {
			newCounts[i] = counts[i];
		}
		delete[] counts;
		counts = newCounts;
	}

	int findBook(const char* title, int year) {
		for (int i = 0; i < amountOfBooks; i++) {
			if (books[i]->getPublished() == year && strcmp(books[i]->getTitle(), title) == 0) {
				return i;
			}
		}
		return -1;
	}

	void deleteBook(int index) {
		delete books[index];

		for (int i = index; i < amountOfBooks - 1; i++) {
			books[i] = books[i+1];
			counts[i] = counts[i+1];
		}

		amountOfBooks--;
		books[amountOfBooks] = nullptr;
	}

public:
	Library() {
		amountOfBooks = 0;
		reservedSpace = 2;
		books = new Book*[reservedSpace];
		counts = new int[reservedSpace];
	}

	~Library() {
		for (int i = 0; i < amountOfBooks; i++) {
			delete books[i];
		}
		delete[] books;
		delete[] counts;
	}

	bool AddBook(Book& book) {
		if (findBook(book.getTitle(), book.getPublished()) > -1)
			return false;

		if (amountOfBooks == reservedSpace)
			resize();

		char** authors = nullptr;
		int authorsCount = book.getAuthors(authors);
		books[amountOfBooks++] = new Book(book.getTitle(), authors, authorsCount, book.getPublished(), book.getPrice());
		return true;
	}
	bool RemoveBook(const char* title, int year) {
		int ind = findBook(title, year);
		if (ind < 0) return false;

		deleteBook(ind);
		return true;
	}

	bool StockBook(const char* title, int year, int stockWith) {
		int ind = findBook(title, year);
		if (ind < 0) return false;

		counts[ind] += stockWith;
		return true;
	}
	bool SellBook(const char* title, int year, int soldCount) {
		int ind = findBook(title, year);
		if (ind < 0) return false;

		counts[ind] -= soldCount;
		return true;
	}
};