-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.cpp
92 lines (70 loc) · 2.86 KB
/
View.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
#include "View.h"
#include "QuestionManager.h"
#include "UserManager.h"
int View::show_read_menu(std::vector<std::string> choices) {
std::cout << '\n';
std::cout << std::setw(40) << std::left << "" << "---------------------------------------------\n";
std::cout << std::setw(60) << std::left << "" << "Menu:\n";
for (int ch = 0; ch < (int)choices.size(); ++ch)
std::cout << std::setw(45) << std::left << "" << '[' << ch + 1 << "] " << choices[ch] << '\n';
std::cout << std::setw(40) << std::left << "" << "---------------------------------------------\n";
return read_int(1, (int)choices.size());
}
int View::read_int(int low, int high) {
std::cout << "\nEnter number in range " << low << " : " << high << ": ";
int value;
std::cin >> value;
if (low <= value && high >= value)
return value;
std::cout << "Invalid number! Try again...\n";
return read_int(low, high);
}
int View::read_question_id(User& user, QuestionManager& question_mange) {
int question_id;
std::cout << "Enter question ID or -1 for cancel: ";
std::cin >> question_id;
if (question_id == -1)
return -1;
if (!question_mange.get_question_id_to_QuestionObject().count(question_id)) {
std::cout << "\n\t Error! No question with such ID. Try again...\n\n";
return read_question_id(user, question_mange);
}
Question question = question_mange.get_question_id_to_QuestionObject()[question_id];
if (question.get_to_user_id() != user.get_user_id()) {
std::cout << "\n\t Error! Invalid question ID. Try again...\n\n";
return read_question_id(user, question_mange);
}
return question_id;
}
int View::read_question_id_thread(User& user) {
QuestionManager question_mange;
int question_id;
std::cout << "For thread question: Enter Question ID or -1 for new question: ";
std::cin >> question_id;
if (question_id == -1)
return -1;
if (!question_mange.get_question_id_to_question_ids_thread().count(question_id)) {
std::cout << "\n\t Error! No thread question with such ID. Try again...\n\n";
return read_question_id_thread(user);
}
return question_id;
}
void View::access_system(UserManager& user_manage) {
int choice = show_read_menu({ "Login", "Sign Up" });
if (choice == 1)
user_manage.log_in();
else
user_manage.sign_up();
}
std::vector<std::string> View::menu() const {
std::vector<std::string> menu;
menu.push_back("Print Questions To Me");
menu.push_back("Print Questions From Me");
menu.push_back("Answer Question");
menu.push_back("Delete Question");
menu.push_back("Ask Question");
menu.push_back("List System Users");
menu.push_back("Feed");
menu.push_back("Logout");
return menu;
}