-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
112 lines (98 loc) · 4.82 KB
/
main.cpp
File metadata and controls
112 lines (98 loc) · 4.82 KB
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
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include "SocialNet.hpp"
std::vector<std::string> parseCommand(const std::string& line) {
std::vector<std::string> tokens;
std::stringstream ss(line);
std::string token;
//This loop handles spaces in usernames and post content
while (ss >> token) {
if (token.front() == '"') {
std::string postContent = token;
if (token.back() != '"') {
std::string nextPart;
while (std::getline(ss, nextPart, '"')) {
postContent += nextPart;
if (ss.peek() == '"' || !ss) {
break;
}
postContent += '"';
}
if(token.back() != '"') postContent += '"';
}
if(postContent.length() >= 2) {
tokens.push_back(postContent.substr(1, postContent.length() - 2));
} else {
tokens.push_back("");
}
} else {
tokens.push_back(token);
}
}
return tokens;
}
int main() {
SocialNet net;
std::string line;
std::cout << "SocialNet Simulator started. Enter 'EXIT' to quit." << std::endl;
std::cout << "> ";
while (std::getline(std::cin, line)) {
if (line.empty()) {
std::cout << "> ";
continue;
}
if (line == "EXIT") {
break;
}
std::vector<std::string> args = parseCommand(line);
if (args.empty()) {
std::cout << "> ";
continue;
}
std::string command = args[0];
try {
if (command == "ADD_USER" && args.size() == 2) {
net.addUser(args[1]);
} else if (command == "ADD_FRIEND" && args.size() == 3) {
net.addFriend(args[1], args[2]);
} else if (command == "LIST_FRIENDS" && args.size() == 2) {
net.listFriends(args[1]);
} else if (command == "SUGGEST_FRIENDS" && args.size() == 3) {
net.suggestFriends(args[1], std::stoi(args[2]));
} else if (command == "DEGREES_OF_SEPARATION" && args.size() == 3) {
net.getDOS(args[1], args[2]);
} else if (command == "ADD_POST" && args.size() == 3) {
net.addPost(args[1], args[2]);
} else if (command == "OUTPUT_POSTS" && args.size() == 3) {
net.outputPosts(args[1], std::stoi(args[2]));
} else if (command == "LIST_USERS" && args.size() == 1) {
net.listUsers();
} else if (command == "HELP"){
std::cout << "=========================================" << std::endl;
std::cout << "ADD_USER -- Obviously for adding user, don't forget to give it a name" << std::endl;
std::cout << "ADD_FRIEND -- Takes 2 arguments -> guess what? name of the two users whom to add friends" << std::endl;
std::cout << "LIST_FRIENDS -- Gives the names of friends of a user" << std::endl;
std::cout << "SUGGEST_FRIENDS -- Takes input user_name and a number, N and suggest atmost N new friends for user" << std::endl;
std::cout << "DEGREES_OF_SEPARATION -- deg of sepn between two people on social net, in a real world must be less than 6 or 7 at average" <<std::endl;
std::cout << "ADD_POST -- Take input user_name and content_for_post in double quotes(if no spaces in post_content than you can skip double quotes) as input -> do I need to explain further?" <<std::endl;
std::cout << "OUTPUT_POSTS -- takes username and number N as input and gives most recent atmost N post of user" << std::endl;
std::cout << "LIST_USERS -- list all the users on social net alphabetically" <<std::endl;
std::cout << "=========================================" << std::endl;
std::cout << "PS: Things are case insensitive -> obviously. Is sensitivity even left in this cruel world?" <<std::endl;
std::cout << "PPS: But the commands are case sensitive, so type them in uppercase only" << std::endl;
std::cout << "=========================================" << std::endl;
} else {
std::cout << "Error: Invalid command or incorrect arguments. Please check syntax. Typed HELP for all possible commands" << std::endl;
}
} catch (const std::invalid_argument& e) {
std::cout << "Error: Invalid argument. Expected a number for N." << std::endl;
} catch (const std::exception& e) {
std::cout << "An unexpected error occurred: " << e.what() << std::endl;
}
std::cout << "> ";
}
std::cout << "Shutting down SocialNet Simulator." << std::endl;
return 0;
}