Website Summary Files Commits Branches @commit @diff @stats
path: root / Utills / Kindle / Book.cpp
blob: 8bfb0506e6d47a1e235b03d44308bb0b9dd0e860 (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
#include "Book.h"
#include<iostream>

void Book::addRating(unsigned short r, const string& name) {
	if (r > 10)
		return;

	int raterIndex = -1;

	for (size_t i = 0; i < ratings.getSize(); i++) {
		if (name == ratings[i].getFirst()) {
			raterIndex = i;
			break;
		}
	}

	if (raterIndex != -1)
		ratings[raterIndex].setSecond(r);
	else {
		Pair<string, unsigned> toPush(name, r);
		ratings.pushBack(std::move(toPush));
	}
}

void Book::print() const {
	std::cout << title << " by: " << author << std::endl;
}

Book::Book(const string& pAuthor, const string& pTitle) {
	author = pAuthor;
	title = pTitle;
}

void Book::writeToFile(std::ofstream& outFile) const {
	writeString(outFile, author);									// Author
	writeString(outFile, title);									// Title

	// Write pages to file
	int len = data.getSize();
	outFile.write((const char*)&len, sizeof(len));

	for (size_t i = 0; i < len; i++)
		data[i].writeToFile(outFile);

	// Write comments to file: 
	int commentsLen = comments.getSize();
	outFile.write((const char*)&commentsLen, sizeof(commentsLen));
	for (size_t i = 0; i < commentsLen; i++)
		writeCommentsToFile(outFile, comments[i]);

	// Write rates to file:
	int ratesCount = this->ratings.getSize();
	outFile.write((const char*)&ratesCount, sizeof(ratesCount));
	for (size_t i = 0; i < ratesCount; i++)
		writeRatesToFile(outFile, ratings[i]);
}

void Book::readFromFile(std::ifstream& inFile) {
	author = readString(inFile);								// Author
	title = readString(inFile);									// Title
	
	// Read pages:
	int len = 0;
	inFile.read((char*)&len, sizeof(len));

	for (size_t i = 0; i < len; i++) {
		data.pushBack(Page());
		data[i].readFromFile(inFile);
	}

	// Read comments
	int commentsLen = 0;
	inFile.read((char*)&commentsLen, sizeof(commentsLen));
	
	for (size_t i = 0; i < commentsLen; i++) {
		Pair<string, string> toInsert = readCommentsFromFile(inFile);
		comments.pushBack(std::move(toInsert));
	}

	// Read rates from file
	int ratesCount = 0;
	inFile.read((char*)&ratesCount, sizeof(ratesCount));
	for (size_t i = 0; i < ratesCount; i++) {
		Pair<string, unsigned> toPush = readRatesFromFile(inFile);
		ratings.pushBack(std::move(toPush));
	}
}

double Book::getAvarageRating() const {
	double sum = 0;
	for (size_t i = 0; i < ratings.getSize(); i++)
		sum += ratings[i].getSecond();
	
	return sum / (double)(ratings.getSize());
}

void Book::write(unsigned pagesCount) {
	unsigned short lines = 0;

	for (int i = 0; i < pagesCount; i++) {
		std::cout << "Enter lines in page " << i + 1 << ": ";
		std::cin >> lines;
		std::cin.ignore();
		Page toInsert;

		toInsert.write(lines);

		data.pushBack(std::move(toInsert));
	}
}

void Book::displayBook() const {
	bool reading = true;
	bool displayMenu = true;
	unsigned pageNumber = 0;

	char cmd = '\0';
	unsigned pageIndex = 0;

	while (reading) {
		system("cls");
		if (displayMenu)
			displayHelpMenu();

		data[pageIndex].displayPage();

		std::cin.get(cmd);

		switch (cmd) {
		case 'n':
			if (pageIndex < data.getSize() - 1)
				pageIndex++;
			break;
		case 'p':
			if (pageIndex > 0)
				pageIndex--;
			break;
		case 'h':
			displayMenu = !displayMenu;
			break;
		case 'q':
			reading = false;
			break;
		case '@':
			std::cin >> pageNumber;
			if (pageNumber >= 1 && pageNumber < data.getSize() + 1)
				pageIndex = pageNumber - 1;
			break;
		default:
			break;
		}
	}
	system("cls");
}

void Book::showComments() const {
	for (size_t i = 0; i < comments.getSize(); i++)
		std::cout << comments[i].getFirst() << " wrote: " << comments[i].getSecond() << std::endl;
}

void Book::showRates() const {
	for (size_t i = 0; i < ratings.getSize(); i++)
		std::cout << ratings[i].getFirst() << ": " << ratings[i].getSecond() << " start" << std::endl;
	std::cout << std::endl;

	std::cout << "Avarage rate: " << getAvarageRating() << std::endl;
}

void Book::displayHelpMenu() const {
	std::cout << "Entered reader mode." << std::endl
		<< "Press n for next, p for prev." << std::endl << 
		"Press @ + <number> to go to that page" << std::endl << 
		"Press h to hide or show this menu or q to exit" << std::endl;
}