C Please do not change any of the functions and maincpp Tha
C++ - Please do not change any of the functions and main.cpp. Thank you
In this assignment you will create an extremely rudimentary bulletin board with a simple command-line interface (like in the old days!).
This will require three collaborating classes - BBoard, User, and Message. For the moment, the bulletin board will exist only inside the test harness (i.e. we won\'t store it to the file system yet), so you will construct a single BBoard object (giving it an appropriate title - e.g. \"Jack\'s Amazing Bulletin Board\"), and then invoke its methods.
First, you will \"load it up\" with a set of authorized Users (read in from a file);
then you will ask the user to log in with a username and password, which you will check against the list you just read in;
and then you will \"run\" the board: the program will enter a sentinel-controlled loop asking the user to choose from a menu of actions (corresponding, obviously, to BBoard\'s other public methods)
- list all current posts
- create a new post
- quit the system
Class Specifications:
User class interface
Message class interface
BBoard class interface
main.cpp
The following is the main function that will be used for the compare output tests.
You will want to create your own main function when unit testing each of the class member functions.
User File Specifications
One user and password per line. Can be any number of users. Last line will have the word \"end\" followed by a newline.
The output samples below use a file named users1.txt that contains the following:
Output Specifications
Read the examples below and make sure your output matches exactly with the sample runs below (given the same inputs).
This is what your output will look like in cloud9:
Sample run 1
Sample run 2
Solution
User.h
#ifndef __USER_H__
 #define __USER_H__
 #include <string>
 using namespace std;
 class User
 {
     private:
       string username;
       string password;
    public:
       //creates a user with empty name and password
       User();
      // creates a user with given username and password
       User(const string &uname, const string &pass);
      //returns the username
       string get_username() const;
bool check(const string &uname, const string &pass) const;
      // sets a new password
       bool set_password(const string &oldpass,const string &newpass);
 };
#endif
User.cpp
#include \"User.h\"
 #include <string>
 #include <iostream>
 #include <stdio.h>
 using namespace std;
User::User()
 {
     username = \"\";
     password = \"\";
 }
User::User(const string &uname, const string &pass)
 {
     username = uname;
     password = pass;
 }
string User::get_username() const
 {
     return username;
 }
bool User::check(const string &uname, const string &pass) const
 {
     if((uname == \"\") || (pass == \"\"))
         return false;
     else if((username == uname)&&(password == pass))
         return true;
     else
         return false;
   
 }
bool User::set_password(const string &oldpass,const string &newpass)
 {
     if(password == oldpass){
         password = newpass;
         return true;
     }
     else
     return false;
 }
Message.h
#ifndef __MESSAGE_H__
 #define __MESSAGE_H__
 #include <string>
 using namespace std;
 class Message
 {
     private:
       string author;
       string subject;
       string body;
    public:
       //default constructor
       Message();
      //Constructor with parameters
       Message(const string &athr,
               const string &sbjct,
               const string &body);
      //displays the message in the given format
       void display() const;
 };
#endif
Message.cpp
#include \"Message.h\"
 #include <string>
 #include <iostream>
 using namespace std;
Message::Message(){
     author = \"\";
     subject = \"\";
     body = \"\";
 }
Message::Message(const string &athr, const string &sbjct, const string &bdy){
     author = athr;
     subject = sbjct;
     body = bdy;
 }
//displays the message in the given format. See output specs.
 void Message::display() const
 {
     if(author == \"\")
         cout << \"Nothing to Display.\";
     else
     {
         cout << subject << endl;
         cout << \"from \" << author << \": \" << body << endl;
     }
 }
BBoard.h
#ifndef BBOARD_H
 #define BBOARD_H
#include <string>
 #include <vector>
#include \"User.h\"
 #include \"Message.h\"
using namespace std;
class BBoard
 {
    private:
        string title;
        vector<User> user_list;
        User current_user;
        vector<Message> message_list;
    public:
        BBoard();
        BBoard(const string &ttl);
        bool loadUsers (const string &input_file);
        bool login();
        void run();
    private:
       void add_user(const string &name, const string &pass);
        User get_user(const string &name) const;
        bool user_exists(const string &name, const string &pass) const;
        void display() const;
        void add_message();
 };
#endif
 BBoard.cpp
#include <iostream>
 #include <fstream>
 #include <cstdlib>
 #include \"BBoard.h\"
 #include \"User.h\"
 #include \"Message.h\"
//constructor that creates a board with a default title, empty user &
 //message lists, and the \"default\" User
 BBoard::BBoard()
 {
    title = \"Bulletin Board!\";
    user_list.clear();
    current_user = User();
    message_list.clear();
 }
//same as default constructor, except that it sets the title of the board.
 BBoard::BBoard(const string &ttl)
 {
    title = ttl;
    user_list.clear();
    current_user = User();
    message_list.clear();
 }
bool BBoard::loadUsers(const string &input_file)
 {
    string u, p;
    bool loaduser = true;
    ifstream allUsers;
allUsers.open(input_file.c_str());
   while (loaduser)
    {
        allUsers >> u;
        if (u != \"end\")
        {
             allUsers >> p;
             add_user(u, p);
             return true;
        }
        else        
            return false;
    }
    allUsers.close();
 }
//asks for and validates current user/password
bool BBoard::login()
 {
    string user,pass;
    cout << \"Enter your username: \";
    cin >> user;
    cout << \"Enter your password: \";
    cin >> pass;
   
     if ((user == \"Q\" || user == \"q\")||(pass == \"Q\" || pass == \"q\"))
     {
         exit(0);
     }
   
     else if(user_exists(user, pass))
     {
         current_user = get_user(user);
         cout << \"Welcome \" << user;
         return true;
        
     }
     else
     {
         cout << \"Username or Password entered does not exist.\" << endl << endl;
         return false;
     }
 }
 void BBoard::run()
 {
     string input;
     if(login()){    //checks user is logged in or not
         //Menu options:
         cout << \"Menu\" << endl;
         cout << \" - Display Messages (\\\'D\\\' or \\\'d\\\')\" << endl;
         cout << \" - Add New Message (\\\'N\\\' or \\\'n\\\')\" << endl;
         cout << \" - Quit (\\\'Q\\\' or \\\'q\\\')\" << endl;
         cout << \"Choose an action: \";
         cin >> input;
   
         if (input == \"D\" || input == \"d\")
         {
            display();
         }
         else if (input == \"N\" || input == \"n\")
         {
            add_message();
         }
        //Quit
        else if (input == \"Q\" || input == \"q\")
        {
             exit(0);
        }
        else
        {
             exit(0);
        }
        cout << endl;
    }
}
void BBoard::add_user(const string &name, const string &pass)
 {
    user_list.push_back(User(name, pass)); //to add users from user_list
 }
bool BBoard::user_exists(const string &name, const string &pass) const
 {
    for (unsigned int count = 0; count < user_list.size(); count++)
    {
        if (user_list.at(count).check(name, pass))
        {
            return true;
        }
    }
    return false;
 }
User BBoard::get_user(const string &name) const //to get user from username
 {
    for (unsigned int count = 0; count < user_list.size(); count++)
    {
        if (user_list.at(count).get_username() == name)
        {
            return user_list.at(count);
        }
    }
    return user_list.at(0);
 }
void BBoard::display() const
 {
    cout << endl;
    if (message_list.size() == 0)
    {
   
        cout << \"Nothing to Display.\" << endl;
    }
    else
    {
        for (unsigned int count = 0; count < message_list.size(); count++)
        {
            cout << \"-----------------------------------------------\" << endl;
            cout << \"Message #\" << count + 1 << \": \";
            message_list.at(count).display();
        }
        cout << \"-----------------------------------------------\" << endl;
    }
 }
void BBoard::add_message()
 {
    string subject, body;
   cout << endl;
    cout << \"Subject: \";
    cin.ignore();
    getline(cin, subject);
   cout << \"Body: \";
    getline(cin, body);
Message record = Message(current_user.get_username(), subject, body);
message_list.push_back(record);
   cout << \"Message recorded!\" << endl;
 }







