<\/span><\/h2>\n\n\n\nIn real life applications and systems, a common component is a messaging system. Thea idea is that a sender sends messages to the recipient. The messages might be sent for example over the network. However, some network protocols don’t guarantee to preserve the order of sent messages while they are received by the recipient. For example, if someone sends a text messages hello<\/code>, hi<\/code> and what's up<\/code>, they might be received in the order what's up<\/code>, hello<\/code>, hi<\/code>. In many systems the expected behavior is to preserve the order, so the order of sent messages is the same as the order of received messages.<\/p>\n\n\n\nIn this problem, the task is to implement a software layer over the top of a network protocol sending messages in arbitrary order, in such a way that the sent messages are printed by the recipient in the order they were sent.<\/p>\n\n\n\n
In the template code below, there are implementations of classes Recipient<\/code> and Network<\/code>.<\/p>\n\n\n\nYour task is to implement classes Message<\/code> and MessageFactory<\/code> according to the below specification:<\/p>\n\n\n\nClass Message<\/code> is required to store a text value of type std::string<\/code> and provide a public getter const string& get_text()<\/code> which is expected to return this text value. Besides that, it should implement the <<\/code> operator that will be used in fix_order()<\/code> method of the recipient to fix the order of received messages. Feel free to implement any other methods and class\/instance variables. In particular, you can implement any additional constructors, but make sure that you provide an empty constructor, i.e. the one without arguments.<\/p>\n\n\n\nClass MessageFactory<\/code> is required to have an empty constructor, and implement a method Message create_message(const string& text)<\/code> that is expected to return a Message<\/code> object storing the value of text<\/code> argument. Feel free to implement any other methods and class\/instance variables of this class.<\/p>\n\n\n\nThe locked code template will act as follows. First, it creates objects message_factory<\/code> and recipient<\/code>. These objects are of types MessageFactory<\/code> and Recipient<\/code> respectively. Then, it reads messages from the standard input, and then it will use the provided Network<\/code> class to simulate sending the messages to the recipient<\/code>. The Network<\/code> class randomly shuffles the passes messages and then it passes them to the recipient<\/code> using recipient.receive(const Message&)<\/code> method. After all messages are delivered, the recipient uses its own method print_messages<\/code> to print all the received messages to the standard output, and just before doing that, it uses its own fix_order<\/code> method to fix the order of retrieved messages. For that purpose, it uses std::sort()<\/code> algorithm to sort the std::vector<\/code> of received messages and this is the reason your Message<\/code> class implementation has to provide the <<\/code> operator.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe input is read by the provided locked code template. It contains several lines of text messages in the order that they will be sent over the network.<\/p>\n\n\n\n
<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- There will be at most 10<\/strong> lines in the input.<\/li>
- Each line will contain at most 20<\/strong> characters<\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nThe output should be produced by the provided locked code template and it is produced as described in details in the statement. The expected order of printed messages is the same as the one in the input.<\/p>\n\n\n\n
Sample Input 0<\/strong><\/p>\n\n\n\nAlex\nHello Monique!\nWhat'up?\nNot much :(<\/code><\/pre>\n\n\n\nSample Output 0<\/strong><\/p>\n\n\n\nAlex\nHello Monique!\nWhat'up?\nNot much :(<\/code><\/pre>\n\n\n\n<\/span>Solution – Messages Order in C++ <\/strong><\/span><\/h2>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\n#include <iostream>\n#include <algorithm>\n#include <vector>\n\nusing namespace std;\n\nclass Message {\nprivate:\n string text;\n static int id;\n int current_id;\npublic:\n Message() { current_id = ++id; }\n Message(string t){ current_id = ++id; text=t; }\n const string& get_text() {\n return text;\n }\n \/\/ overloaded < operator\n bool operator < (const Message& M2) {\n if(current_id < M2.current_id)\n return true;\n else\n return false;\n }\n};\nint Message::id = 0;\n\nclass MessageFactory {\npublic:\n MessageFactory() {}\n Message create_message(const string& text) {\n Message m = Message(text);\n return m;\n }\n};\n\nclass Recipient {\npublic:\n Recipient() {}\n void receive(const Message& msg) {\n messages_.push_back(msg);\n }\n void print_messages() {\n fix_order();\n for (auto& msg : messages_) {\n cout << msg.get_text() << endl;\n }\n messages_.clear();\n }\nprivate:\n void fix_order() {\n sort(messages_.begin(), messages_.end());\n }\n vector<Message> messages_;\n};\n\nclass Network {\npublic:\n static void send_messages(vector<Message> messages, Recipient& recipient) {\n \/\/ simulates the unpredictable network, where sent messages might arrive in unspecified order\n random_shuffle(messages.begin(), messages.end()); \n for (auto msg : messages) {\n recipient.receive(msg);\n }\n }\n};\n\n\n\nint main() {\n MessageFactory message_factory;\n Recipient recipient;\n vector<Message> messages;\n string text;\n while (getline(cin, text)) {\n messages.push_back(message_factory.create_message(text));\n }\n Network::send_messages(messages, recipient);\n recipient.print_messages();\n}<\/pre>\n\n\n\n