Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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();
- // Parameterized constructor;
- Message(const string &athr,
- const string &sbjct,
- const string &body);
- // Displays the Message using the following format:
- //
- // subject
- // from author: body
- void display() const;
- };
- #endif
- //Message.cpp
- #include <iostream>
- using namespace std;
- #include "Message.h"
- Message::Message() {
- author = "";
- subject = "";
- body = "";
- };
- Message::Message(const string &athr, const string &sbjct, const string &body) {
- author = athr;
- subject = sbjct;
- this->body = body;
- };
- void Message::display() const {
- cout << subject << endl;
- cout << "from " << author << ": " << body << endl;
- }
- *************************************************************************************
- //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 getUsername() const;
- // returns true if the stored username/password matches with the
- // parameters. Otherwise returns false.
- // Note that, even though a User with empty name and password is
- // actually a valid User object (it is the default User), this
- // function must still return false if given a empty uname string.
- bool check(const string &uname, const string &pass) const;
- // sets a new password.
- // This function should only set the new password if the current (old)
- // password is passed in. Also, a default User cannot have its
- // password changed.
- // returns true if password changed, false if not.
- bool setPassword(const string &oldpass, const string &newpass);
- };
- #endif
- //User.cpp
- #include <iostream>
- using namespace std;
- #include "User.h"
- User::User() {
- username = "";
- password = "";
- };
- User::User(const string &uname, const string &pass) {
- username = uname;
- password = pass;
- };
- string User::getUsername() const {
- return username;
- }
- bool User::check(const string &uname, const string &pass) const {
- if (uname == "") {
- return false;
- }
- else if ((uname == username) && (pass == password)) {
- return true;
- }
- else
- return false;
- }
- bool User::setPassword(const string &oldpass, const string &newpass) {
- if ((password == "") && (username == "")) {
- return false;
- }
- else if (oldpass == password) {
- password = newpass;
- return true;
- }
- else
- return false;
- }
- ********************************************************************************
- //BBoard.h
- #ifndef BBOARD_H
- #define BBOARD_H
- #include <string>
- #include <vector>
- #include <fstream>
- using namespace std;
- #include "Message.h"
- #include "User.h"
- class BBoard {
- private:
- string title;
- vector<User> userList;
- User currentUser;
- vector<Message> messageList;
- public:
- // Constructs a board with a default title,
- // empty user & message lists,
- // and the "default" User
- BBoard();
- // Same as the default constructor except
- // it sets the title of the board
- BBoard(const string &);
- // Imports all the authorized users from an input file,
- // storing them as User objects in the vector userList
- // The name of the input file is passed in as a parameter to this function.
- // Returns true if the file opened, false if it did not.
- // See specifications for file format.
- bool loadUsers(const string &str);
- // Asks for and validates a user/password.
- // Always asks for both user and password combo unless 'q' or 'Q' entered.
- // Checks userList to validate user and password.
- // If valid, sets currentUser to this User, outputs welcome message,
- // then returns true.
- // Otherwise outputs error message and then repeats request
- // for username/password.
- // Continues until valid user/password
- // or until 'q' or 'Q' is entered for username.
- // If 'q' or 'Q' is entered, does not ask for password, outputs "Bye!"
- // and then returns false.
- bool login();
- // Contains main loop of Bulletin Board.
- // First verifies a User has been logged in before calling this function.
- // (Do not call login function within this function.)
- // Returns from **function** immediately if no User logged in (Default User).
- // Continues to display menu options to user and performs requested action
- // until user chooses to quit.
- // See output samples for exact format of menu.
- void run();
- private:
- // These are only suggestions, not required helper functions.
- // Feel free to make your own private helper functions as you see fit.
- void displayMess();
- };
- #endif
- //BBoard.cpp
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- using namespace std;
- #include "Message.h"
- #include "User.h"
- #include "BBoard.h"
- BBoard::BBoard() {
- title = "";
- userList.resize(0);
- User currentUser();
- messageList.resize(0);
- Message currentMessage();
- };
- BBoard::BBoard(const string &str) {
- title = str;
- userList.resize(0);
- User currentUser();
- messageList.resize(0);
- Message currentMessage();
- };
- bool BBoard::loadUsers(const string &str) {
- ifstream fileIn;
- string uname, pass;
- fileIn.open(str.c_str());
- if(!fileIn.is_open()) {
- return false;
- }
- else {
- while (fileIn >> uname) {
- fileIn >> pass;
- User currentUser(uname, pass);
- userList.push_back(currentUser);
- }
- fileIn.close();
- return true;
- }
- }
- bool BBoard::login() {
- string uname, pass;
- int size = userList.size();
- cout << "Welcome to " << title << endl;
- while(true) {
- cout << "Enter your username ('Q' or 'q' to quit): ";
- cin >> uname;
- cout << endl;
- if ((uname == "q") || (uname == "Q")) {
- cout << "Bye!" << endl;
- return false;
- }
- cout << "Enter your password: ";
- cin >> pass;
- cout << endl;
- for(int i = 0; i < size; i++) {
- currentUser = userList.at(i);
- if(currentUser.check(uname, pass)) {
- cout << "Welcome back " << currentUser.getUsername() << "!" << endl;
- return true;
- }
- }
- cout << "Invalid Username or Password!" << endl << endl;
- }
- }
- void BBoard::run() {
- bool flag = false;
- int size = userList.size();
- for (int i = 0; i < size; i++) {
- if (currentUser.getUsername() == userList.at(i).getUsername()) {
- flag = true;
- }
- }
- if (!flag) {
- return;
- }
- else {
- displayMess();
- }
- }
- void BBoard::displayMess() {
- char choice = ' ';
- bool flag = true;
- int size = messageList.size();
- string sbjct;
- string body;
- string athr = currentUser.getUsername();
- while (flag) {
- cout << endl << "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 >> choice;
- cout << endl;
- if ((choice == 'q') || (choice == 'Q')) {
- cout << "Bye!" << endl;
- flag = false;
- }
- else if ((choice == 'd') || (choice == 'D')) {
- if (size == 0) {
- cout << "Nothing to Display." << endl;
- }
- else {
- cout << "---------------------------------------------------------" << endl;
- for (int i = 0; i < size; i++) {
- Message currentMessage = messageList.at(i);
- cout << "Message #" << i + 1 << ": ";
- currentMessage.display();
- cout << "---------------------------------------------------------" << endl;
- }
- }
- }
- else {
- getline(cin,sbjct);
- cout << "Enter Subject: ";
- getline(cin,sbjct);
- cout << endl;
- cout << "Enter Body: ";
- getline(cin, body);
- cout << endl;
- Message currentMessage(athr, sbjct, body);
- messageList.push_back(currentMessage);
- cout << "Message Recorded!" << endl;
- size = messageList.size();
- }
- }
- }
- ****************************************************************************************************
- //Main.cpp
- #include <iostream>
- #include <vector>
- #include <string>
- #include <fstream>
- using namespace std;
- #include "Message.h"
- #include "User.h"
- #include "BBoard.h"
- int main() {
- string userfile;
- cout << "User file?" << endl;
- cin >> userfile;
- cout << endl;
- BBoard bb("CS12 Bulletin Board");
- // load users from file
- if (!bb.loadUsers(userfile)) {
- cout << "Error loading users from file " << userfile << endl;
- return 1;
- }
- if (!bb.login()) {
- cout << "Login not successful" << endl;
- return 1;
- }
- bb.run();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement