Курсовая работа по ООП, ведение блога. Ведение блога. Программа для ведения Блога
Скачать 380.86 Kb.
|
1 2 Client(){delete menu_now; delete login_user; delete guest_user;}Poster(){}BDWorker(){}#include #include #include #include "Paginator.h" class UsersDataBase { private: const std::string first_line_in_file = "login+password+hobbies+self-info+contacts+data+com_per"; std::string filepath; std::vector static UsersDataBase * p_instance; UsersDataBase(); UsersDataBase(const UsersDataBase&); UsersDataBase(std::string path) : filepath(path) {readUsersFullInfo();} UsersDataBase& operator=(UsersDataBase&); std::vector { std::vector std::stringstream ss(s); std::string item; while (std::getline (ss, item, delim)) { result.push_back (item); } return result; } void readUsersFullInfo() { std::ifstream file(filepath); if (file.is_open()) { std::string line; std::vector std::getline(file, line); while (std::getline(file, line)) { full_info = split(line, '+'); users.push_back(new User(full_info[0], full_info[1], full_info[2], full_info[3], full_info[4], full_info[5], std::stoi(full_info[6]))); } } else std::cout << "File missing!!!" << std::endl; } public: static UsersDataBase * getUsersData(std::string path) { if(!p_instance) p_instance = new UsersDataBase(path); return p_instance; } std::string getPath() { return filepath; } User* find(std::string login, std::string pass) { for (int i = 0; i < users.size(); i++) { if ((users[i]->getLogin() == login) && (users[i]->getPass() == pass)) return users[i]; } return nullptr; } void add(User* new_user) { std::ofstream file("log_pass.txt", std::ios_base::app); if (file.is_open()) { std::string line; line = line + new_user->getLogin() + "+" + new_user->getPass() + "+" + new_user->getSelfInfo() + "+" + new_user->getContacts() + "+" + new_user->getHobbies() + "+" + new_user->getBirthdate() + "+" + std::to_string(new_user->getComPer()); file << line << std::endl; } else { std::cout << "File is Missing..."; system("pause"); } users.push_back(new_user); } void update() { std::ofstream file("log_pass.txt"); file << first_line_in_file << std::endl; std::string line; for (auto u : users) { line = line + u->getLogin() + "+" + u->getPass() + "+" + u->getSelfInfo() + "+" + u->getContacts() + "+" + u->getHobbies() + "+" + u->getBirthdate() + "+" + std::to_string(u->getComPer()); file << line << std::endl; line.clear(); } } User* findUserInFriendFiles(std::string login) { for (int i = 0; i < users.size(); i++) { if (users[i]->getLogin() == login) { return users[i]; } } return nullptr; } void deleteMe(User* me) { for (int i = 0; i < users.size(); i++) { if (users[i]->getLogin() == me->getLogin()) { users.erase(users.begin() + i); update(); return; } } } }; class BDWorker { UsersDataBase* bd_users; public: BDWorker(){bd_users = UsersDataBase::getUsersData("log_pass.txt");} User* find(std::string login, std::string pass) { return (bd_users->find(login, pass)); } void add(User* new_user) { bd_users->add(new_user); } void update() { bd_users->update(); } User* getUserbyLogin(std::string login) { return bd_users->findUserInFriendFiles(login); } void deleteMe(User* me) { bd_users->deleteMe(me); } }; class Friender { public: Friender(User * user){user->setFriends(getFriendsFromFile(user->getLogin()));} { std::vector std::ifstream file("friends\\F"+username+".txt"); if (file.is_open()) { BDWorker users; std::string line; std::getline(file, line); User* buf; while (std::getline(file, line)) { buf = users.getUserbyLogin(line); if (buf != nullptr) { pals.push_back(buf); } } update(username, pals); return pals; } else { file.close(); std::ofstream file("friends\\F"+username+".txt"); update(username, pals); file.close(); } return getFriendsFromFile(username); } void findAndAddFriend(User * adder, std::string login) { std::vector BDWorker users; User* buf = users.getUserbyLogin(login); for (auto f : adders_pals) { if (f->getLogin() == login) { std::cout << "User already in friend list..."; system("pause"); return; } } if (buf != nullptr) { adders_pals.push_back(buf); adder->setFriends(adders_pals); } else { std::cout << "User isn't exist"; system("pause"); return; } update(adder->getLogin(), adders_pals); } void findAndDelFriend(User * adder, int num) { std::vector adders_pals.erase(adders_pals.begin() + num); update(adder->getLogin(), adders_pals); } void update(std::string username, std::vector { std::ofstream file("friends\\F"+username+".txt"); file << username << std::endl; std::string line; for (auto u : pals) { line = line + u->getLogin(); file << line << std::endl; line.clear(); } } void deleteMe(User* me) { std::string filepath = "del friends\\F"+me->getLogin()+".txt"; system(filepath.c_str()); } }; class Poster { std::vector { std::vector std::stringstream ss(s); std::string item; while (std::getline (ss, item, delim)) { result.push_back (item); } return result; } public: Poster(User * user){user->setPosts(getPostsFromFile(user->getLogin()));} |
std::vector
{
std::vector
std::ifstream file("Posts\\"+username + ".txt");
if (file.is_open())
{
BDWorker users;
std::string line;
std::string header;
std::string text;
std::string date;
std::vector
User * comment_author;
std::getline(file, line);
while (std::getline(file, line))
{
if (line[0] != '@')
{
if (line == "---")
{
posts.push_back(new Publication(header, text, date));
}
else
{
header = line;
_flushall();
std::getline(file, text);
std::getline(file, date);
}
}
else
{
if (line != "@@@")
{
header = split(line, '+')[1];
comment_author = users.getUserbyLogin(header);
_flushall();
std::getline(file, text);
std::getline(file, date);
if (comment_author != nullptr)
{
posts[posts.size()-1]->addComment(new Comment( split(text, '+')[1], split(date, '+')[1], comment_author));
}
}
}
}
return posts;
}
else
{
file.close();
std::ofstream file("Posts\\"+username+".txt");
update(username, posts);
file.close();
}
return getPostsFromFile(username);
}
void update(std::string username, std::vector
{
std::ofstream file("Posts\\"+username+".txt");
file << "@@@" << std::endl;
std::vector
std::string line;
for (auto p : posts)
{
file << p->getHeader() << std::endl <
comms_buff = p->getComments();
for (auto c : comms_buff)
{
file << "@+"+c->getHeader() << std::endl << "@+"+c->getText() << std::endl << "@+"+c->getDate() << std::endl;
}
file << "@@@" << std::endl;
line.clear();
}
}
void deleteMe(User* me)
{
std::string filepath = "del Posts\\"+ me->getLogin() + ".txt";
system(filepath.c_str());
}
};
Файл “Paginator.h”.
Данный файл хранит класс, отвечающий за «пагинацию» объектов, а если конкретно, то за «пагинацию» публикаций пользователя.
#include
#include
#include
#include
#include "User_Post.h"
class Paginator
{
std::vector
std::vector
int size;
int page;
int last_page;
int getAnswer(int size)
{
int answ;
do {
std::cout << "\nEnter item: ";
std::cin >> answ;
} while ((answ > size) || (answ < 1));
return answ;
}
void getNextItemToPages()
{
items.clear();
if (page != last_page-1)
page++;
else
{
std::cout << "This is the last page!" << std::endl;
system("pause");
}
for (int i = 0; (i < 10) && (i+page*10 < size); i++)
{
all_items[i+page*10]->show();
items.push_back(all_items[i+page*10]);
}
}
void getPreviousItemToPages()
{
if (page != 0)
{
page--;
}
else
{
std::cout << "This is the first page!" << std::endl;
system("pause");
}
items.clear();
for (int i = 0; (i < 10) && (i+page*10 < size); i++)
{
items.push_back(all_items[i+page*10]);
}
}
void toStart()
{
page = -1;
getNextItemToPages();
}
void toEnd()
{
page = last_page-2;
getNextItemToPages();
}
void showPage()
{
std::vector
for (int i = 0; i < items.size(); i++)
{
items[i]->show();
comms_buf = items[i]->getComments();
for (auto c : comms_buf)
{
c->show();
}
}
}
void addComment(User* guest)
{
int num;
if (guest->getComPer() == 1)
{
std::cout << "Enter Post number: ";
std::cin >> num;
num--;
std::string text;
_flushall();
std::getline(std::cin, text);
std::time_t t = std::time(nullptr);
char buf[64];
strftime(buf, sizeof(buf), "%d.%m.%Y", localtime(&t));
std::string date = buf;
all_items[page*10+num]->addComment(new Comment(text, date, guest));
}
else
{
std::cout << "User has forbidden to leave comments..." << std::endl;
system("pause");
}
}
public:
Paginator(std::vector
{
last_page = size / 10;
if (size % 10 != 0) last_page++;
getNextItemToPages();
}