Messages Order

Sort by

recency

|

91 Discussions

|

  • + 0 comments

    class MessageFactory { public: MessageFactory() = default; Message create_message(const string& text) { static int counter = 0 ; return {text, counter++}; } };

  • + 0 comments
    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    class Message {
    private:
        std::string _text;
        int _id;
    public:
        Message(const string& text, const int id) : _text(text), _id(id) {}
    
        const string& get_text() {
            return _text;
        }
    
        bool operator<(const Message& message)const{
            if(_id < message._id) return true;
            return false;
        }
    };
    
    class MessageFactory {
        int counter = 0;
    public:
        MessageFactory() = default;
        Message create_message(const string& text)
        {
            counter++;
            return {text, counter};
        }
    };
    
    class Recipient {
    public:
        Recipient() {}
        void receive(const Message& msg) {
            messages_.push_back(msg);
        }
        void print_messages() {
            fix_order();
            for (auto& msg : messages_) {
                cout << msg.get_text() << endl;
            }
            messages_.clear();
        }
    private:
        void fix_order() {
            sort(messages_.begin(), messages_.end());
        }
        vector<Message> messages_;
    };
    
    class Network {
    public:
        static void send_messages(vector<Message> messages, Recipient& recipient) {
        // simulates the unpredictable network, where sent messages might arrive in unspecified order
            random_shuffle(messages.begin(), messages.end());         
            for (auto msg : messages) {
                recipient.receive(msg);
            }
        }
    };
    
    
    
    int main() {
        MessageFactory message_factory;
        Recipient recipient;
        vector<Message> messages;
        string text;
        while (getline(cin, text)) {
            messages.push_back(message_factory.create_message(text));
        }
        Network::send_messages(messages, recipient);
        recipient.print_messages();
    }
    
  • + 0 comments

    Here is Messages Order solution in c++ - https://programmingoneonone.com/hackerrank-messages-order-solution-in-cpp.html

  • + 0 comments

    Message's id assignment should be handled by the factory.

    class Message {
    public: 
        Message() {}
        Message(string text, int id):msg_body(text), msg_id(id) {}
        const string& get_text() {
            return msg_body;
        }
        bool operator<(const Message& other) const {
            return this->msg_id < other.msg_id;
        }
        
    private:
        string msg_body;
        int msg_id;
    };
    
    class MessageFactory {
    public:
        MessageFactory() {}
        Message create_message(const string& text) {
            msg_counter += 1;
            return Message(text, msg_counter);
        }
    private:
        int msg_counter = -1;
    };
    
  • + 0 comments
    class Message {
    private:
        string text;
        static int id;
        int current_id;
    public:
        Message() { current_id = ++id; }
        Message(string t){ current_id = ++id; text=t; }
        const string& get_text() {
            return text;
        }
        bool operator < (const Message& M2) {
            if(current_id < M2.current_id)
                return true;
            else
                return false;
        }
    };
    int Message::id = 0;
    class MessageFactory {
    public:
        MessageFactory() {}
        Message create_message(const string& text) {
            Message m = Message(text);
            return m;
        }
    };