-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
277 lines (269 loc) · 8.14 KB
/
main.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <iostream>
#include <string>
#include <stdlib.h>
#include "user_network.h"
#include "wall_post.h"
#include "user.h"
#include "basic.h"
#include <fstream>
#include <sstream>
using namespace std;
void menu(User* user) {
string oper;
user->ShowRequests();
while(true) {
cout << endl << endl << endl;
cout << "Enter your command (type Help for help): " << endl;
getline(cin, oper);
if (oper == "Quit") {
return;
} else if (oper == "Dis") {
user->DisplayWallPosts();
user->DisplayMemory();
} else if (oper == "Help") {
cout << "Available Commands:" << endl;
cout << " Help : show this message." << endl;
cout << " Dis : show all posts of this user." << endl;
cout << " Add: add post" << endl;
cout << " Reply: Reply to a post" << endl;
cout << " Del: delete post" << endl;
cout << " Save: dump user data to a file" << endl;
cout << " Read: read user data from a file" << endl;
cout << " Search: search users by name" << endl;
cout << " AddFriend: add a friend by his/her username" << endl;
cout << " ShowFriends: show current friends list" << endl;
cout << " DeFriend: delete a friend by his/her username" << endl;
cout << " ShowRequests: show all pending friend requests" << endl;
cout << " Approve: approve a friend request" << endl;
cout << " PostTo: create a new post on a friend’s wall" << endl;
cout << " Ignore: ignore a friend request (request deleted after this)" << endl;
cout << " ShowDist: show distance to another user" << endl;
cout << " SearchDist: show all users with certain degree of separation" << endl;
cout << " Quit : exit." << endl;
} else if (oper == "Add") {
cout << "Enter your post: " << endl;
string post;
getline(cin, post);
user->AddWallPost(post);
} else if (oper == "Reply") {
cout << "Choose from the following options: " << endl;
cout << "1. reply to a post on your own wall " << endl;
cout << "2. reply to a post on your friend’s walls " << endl;
int option;
ReadInt(option);
if (option == 1) {
user->DisplayWallPosts();
cout << "Enter index to reply to: " << endl;
int num;
ReadInt(num);
cout << "Enter your response: " << endl;
string post;
getline(cin, post);
user->AddResponse(num, user->GetUserName(), post);
}
else if (option == 2) {
user->ShowFriends();
User* frd = NULL;
string uname;
cout << "Reply to which friend's wallpost? Enter his/her username: " << endl;
getline(cin, uname);
frd = user->QueryFriend(uname);
if (!frd) {
cout << "This is not your friend. " << endl;
continue;
}
frd->DisplayWallPosts();
cout << "Enter index to reply to: " << endl;
int num;
ReadInt(num);
cout << "Enter your response: " << endl;
string post;
getline(cin, post);
WallPost pp = frd->AddResponse(num, user->GetUserName(), post);
user->Remember(uname, pp);
}
} else if (oper == "Del") {
cout << "Choose from the following options: " << endl;
cout << "1. delete posts on your own wall " << endl;
cout << "2. delete your posts on friend’s walls " << endl;
int option;
ReadInt(option);
if (option == 1) {
user->DeleteWallPost(NULL);
}
else if (option == 2) {
user->DeleteFromFriendWall();
}
} else if (oper == "Save") {
cout << "Enter file name: " << endl;
string fname;
getline(cin, fname);
ofstream output_file(fname);
output_file << user->RetrieveInfo();
output_file.close();
cout << "User data saved." << endl;
} else if (oper == "Read") {
cout << "Enter file name: " << endl;
string fname;
getline(cin, fname);
ifstream input_file(fname);
if (!input_file) {
cout << "Invalid file." << endl;
continue;
}
string user_data ((std::istreambuf_iterator<char>(input_file)),
(std::istreambuf_iterator<char>()));
user->ConstructUserFromString(user_data);
cout << "User data restored." << endl;
} else if (oper == "Search") {
cout << "Enter keyword: " << endl;
string keyword;
getline(cin, keyword);
user->SearchFriend(keyword);
} else if (oper == "AddFriend") {
cout << "Enter friend's username: " << endl;
string uname;
getline(cin, uname);
user->AddFriend(uname);
} else if (oper == "ShowFriends") {
user->ShowFriends();
} else if (oper == "DeFriend") {
cout << "Enter friend's username: " << endl;
string uname;
getline(cin, uname);
user->DeFriend(uname);
} else if (oper == "ShowRequests") {
user->ShowRequests();
} else if (oper == "Approve") {
cout << "Enter index to approve: " << endl;
int num;
ReadInt(num);
user->Approve(num);
} else if (oper == "Ignore") {
cout << "Enter index to ignore: " << endl;
int num;
ReadInt(num);
user->Ignore(num);
} else if (oper == "PostTo") {
user->ShowFriends();
User* frd = NULL;
string uname;
while (true) {
cout << "Post on which friend's wall? Enter his/her username: " << endl;
getline(cin, uname);
frd = user->QueryFriend(uname);
if (!frd) cout << "This is not your friend. Re-enter: " << endl;
else break;
}
cout << "Enter your post to add to this friend's wall: " << endl;
string post;
getline(cin, post);
WallPost pp = frd->AddWallPost(user->GetUserName(), post);
user->Remember(uname, pp);
} else if (oper == "ShowDist") {
cout << "Enter a username: " << endl;
string name2;
string name1 = user->GetUserName();
getline(cin, name2);
if (user->GetNetwork()->UserExists(name2)) {
int distance = user->GetNetwork()->Distance(name1, name2);
if (distance == -1) {
cout << "There is no path. Distance is infinity.";
}
else {
cout << "The distance between " << name1 << " and "
<< name2 << " is " << distance << "." << endl;
}
}
else {
cout << "Fail to find distance." << endl;
}
} else if (oper == "SearchDist") {
cout << "Enter a distance: " << endl;
int dist;
ReadInt(dist);
if (dist < 0) {
cout << "Invalid distance. " << endl;
continue;
}
user->GetNetwork()->FindFriendWithDistance(user->GetUserName(),dist);
}
else {
cout << "Invalid command." << endl;
}
}
}
int main(int argc, char** argv)
{
if (argc < 2) {
cout << "Usage:./run datafile." << endl;
return -1;
}
UserNetwork * network = new UserNetwork;
network->CreateUsersFromFile(string(argv[1]));
int option;
string uname, pass, pass1, pass2, rname, bday;
string input = "";
User *user = NULL;
while (true) {
cout << endl << endl << endl << "What do you want to do?" << endl;
cout << "Option 1: Create a new user." << endl;
cout << "Option 2: Log in." << endl;
cout << "Option 3: Quit." << endl;
cout << "Please input your selection: " << endl;
if (!ReadInt(option) || option > 3 || option < 1) {
cout << "Invalid option, please input again: " << endl;
continue;
}
switch (option) {
case 1:
cout << "Create a new user: " << endl;
cout << "Please input your username: " << endl;
getline(cin, uname);
while (network->UserExists(uname)) {
cout << "Username already exists. Re-enter: " << endl;
getline(cin, uname);
}
while (true){
cout << "Set your password: " << endl;
getline(cin, pass1);
cout << "Confirm your password: " << endl;
getline(cin, pass2);
if (pass1 != pass2) {
cout << "Passwords don't match. Re-enter. " << endl;
continue;
}
break;
}
cout << "Enter your realname: " << endl;
getline(cin, rname);
cout << "Enter your birthday [MM/DD/YYYY] : " << endl;
getline(cin, bday);
network->AddUser(uname, pass1, rname, bday);
cout << "User " << uname << " added" << endl << endl;
break;
case 2:
cout << "Enter username: " << endl;
getline(cin, uname);
while (!network->UserExists(uname)) {
cout << "Username doesn't exist. Re-enter: " << endl;
getline(cin, uname);
}
while (true){
cout << "Enter your password: " << endl;
getline(cin, pass);
user = network->AuthorizeUser(uname, pass);
if (!user) {
cout << "Wrong password. Re-enter. " << endl;
continue;
}
break;
}
menu(user);
break;
case 3:
network->WriteUsersToFile(string(argv[1]));
return 0;
}
}
}