Messages Order

  • + 0 comments

    use c++ version 2014 otherwise not work

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    class Message {
        string message;
        int order;
        static int tot_n;
    public: 
        Message(){ 
            tot_n++;
            order = tot_n;
        }
        Message(const string& s){
            tot_n++;
            order = tot_n;
            message = s;
        }
        const string& get_text() {
            return message;
        }
        bool operator<(const Message& m){
            return (order < m.order);
        }
    };
    
    int Message::tot_n = 0;
    
    class MessageFactory {
    public:
        MessageFactory() {}
        Message create_message(const string& text) {
            Message m(text);
            return m;
        }
    };
    
    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();
    }