-
Notifications
You must be signed in to change notification settings - Fork 0
/
Topic.cpp
118 lines (114 loc) · 3.03 KB
/
Topic.cpp
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
#include "Topic.h"
#include <time.h>
#include <stdlib.h>
int Topic::getQuestionIdx(const MyString& title) const
{
size_t count = _questions.size();
for (int i = 0; i < count; i++)
{
if (_questions[i].getTitle() == title)
return i;
}
return -1;
}
int Topic::getQuestionIdx(unsigned id) const
{
size_t count = _questions.size();
for (int i = 0; i < count; i++)
{
if (_questions[i].getId() == id)
return i;
}
return -1;
}
Topic::Topic(const MyString& title, unsigned idxOfAuthor, const MyString& content, unsigned id):
_title(title), _content(content)
{
_id = id;
_idxOfAuthor = idxOfAuthor;
}
void Topic::addQuestion(unsigned idxOfAuthor, const MyString& title, const MyString& content)
{
srand(time(NULL));
unsigned id = rand();
_questions.push_back(Question(title, idxOfAuthor, content, id));
}
bool Topic::containsText(const MyString& text) const
{
size_t len = _title.length();
size_t tLen = text.length();
int iter = 0;
while (len >= tLen)
{
if (_title.substr(iter, tLen) == text)
return true;
iter++;
len--;
}
return false;
}
void Topic::printTopicInfo() const
{
std::cout << " >> " << _title << " {id:" << _id << "} " << std::endl;
}
const MyString& Topic::getTitle() const
{
return _title;
}
unsigned Topic::getId() const
{
return _id;
}
void Topic::printTopicData(const User* author) const
{
std::cout << "Name: " << _title << std::endl;
std::cout << "Description: " << _content << std::endl;
std::cout << "Created by: " << author->getFName() << " " << author->getLName() << " { id:" << author->getId() << "} " << std::endl;
std::cout << "Questions asked: " << _questions.size() << std::endl;
}
void Topic::getCurrentQ(const MyString& title, Question*& currentQ)
{
int idx = getQuestionIdx(title);
if (idx == -1)
throw std::invalid_argument("No such question exists");
currentQ = &_questions[idx];
}
void Topic::getCurrentQ(unsigned id, Question*& currentQ)
{
int idx = getQuestionIdx(id);
if (idx == -1)
throw std::invalid_argument("No such question exists");
currentQ = &_questions[idx];
}
void Topic::printQuestions() const
{
size_t count = _questions.size();
for (int i = 0; i < count; i++)
_questions[i].printQuestion();
}
void Topic::writeToFile(std::ofstream& ofs) const
{
writeStringToFile(ofs, _title);
ofs.write((const char*)&_idxOfAuthor, sizeof(unsigned));
writeStringToFile(ofs, _content);
ofs.write((const char*)&_id, sizeof(unsigned));
size_t countOfQs = _questions.size();
ofs.write((const char*)&countOfQs, sizeof(size_t));
for (int i = 0; i < countOfQs; i++)
_questions[i].writeToFile(ofs);
}
void Topic::readFromFiLe(std::ifstream& ifs)
{
_title = readStringFromFile(ifs);
ifs.read((char*)&_idxOfAuthor, sizeof(unsigned));
_content = readStringFromFile(ifs);
ifs.read((char*)&_id, sizeof(unsigned));
size_t countOfQs;
ifs.read((char*)&countOfQs, sizeof(size_t));
for (int i = 0; i < countOfQs; i++)
{
Question read;
read.readFromFiLe(ifs);
_questions.push_back(read);
}
}