Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2018
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.72 KB | None | 0 0
  1. //Message.h
  2. #ifndef MESSAGE_H
  3. #define MESSAGE_H
  4.  
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. class Message {
  10. private:
  11. string author;
  12. string subject;
  13. string body;
  14.  
  15. public:
  16.  
  17. // default constructor
  18. Message();
  19.  
  20. // Parameterized constructor;
  21. Message(const string &athr,
  22. const string &sbjct,
  23. const string &body);
  24.  
  25. // Displays the Message using the following format:
  26. //
  27. // subject
  28. // from author: body
  29. void display() const;
  30. };
  31.  
  32. #endif
  33.  
  34. //Message.cpp
  35. #include <iostream>
  36.  
  37. using namespace std;
  38.  
  39. #include "Message.h"
  40.  
  41. Message::Message() {
  42. author = "";
  43. subject = "";
  44. body = "";
  45. };
  46.  
  47. Message::Message(const string &athr, const string &sbjct, const string &body) {
  48. author = athr;
  49. subject = sbjct;
  50. this->body = body;
  51. };
  52.  
  53. void Message::display() const {
  54. cout << subject << endl;
  55. cout << "from " << author << ": " << body << endl;
  56. }
  57.  
  58. *************************************************************************************
  59.  
  60. //User.h
  61. #ifndef USER_H
  62. #define USER_H
  63.  
  64. #include <string>
  65.  
  66. using namespace std;
  67.  
  68. class User {
  69. private:
  70. string username;
  71. string password;
  72.  
  73. public:
  74. //creates a user with empty name and password.
  75. User();
  76.  
  77. // creates a user with given username and password.
  78. User(const string& uname, const string& pass);
  79.  
  80. //returns the username
  81. string getUsername() const;
  82.  
  83. // returns true if the stored username/password matches with the
  84. // parameters. Otherwise returns false.
  85. // Note that, even though a User with empty name and password is
  86. // actually a valid User object (it is the default User), this
  87. // function must still return false if given a empty uname string.
  88. bool check(const string &uname, const string &pass) const;
  89.  
  90. // sets a new password.
  91. // This function should only set the new password if the current (old)
  92. // password is passed in. Also, a default User cannot have its
  93. // password changed.
  94. // returns true if password changed, false if not.
  95. bool setPassword(const string &oldpass, const string &newpass);
  96. };
  97.  
  98. #endif
  99.  
  100. //User.cpp
  101. #include <iostream>
  102.  
  103. using namespace std;
  104.  
  105. #include "User.h"
  106.  
  107. User::User() {
  108. username = "";
  109. password = "";
  110. };
  111.  
  112. User::User(const string &uname, const string &pass) {
  113. username = uname;
  114. password = pass;
  115. };
  116.  
  117. string User::getUsername() const {
  118. return username;
  119. }
  120.  
  121. bool User::check(const string &uname, const string &pass) const {
  122. if (uname == "") {
  123. return false;
  124. }
  125. else if ((uname == username) && (pass == password)) {
  126. return true;
  127. }
  128. else
  129. return false;
  130. }
  131.  
  132. bool User::setPassword(const string &oldpass, const string &newpass) {
  133. if ((password == "") && (username == "")) {
  134. return false;
  135. }
  136. else if (oldpass == password) {
  137. password = newpass;
  138. return true;
  139. }
  140. else
  141. return false;
  142. }
  143.  
  144. ********************************************************************************
  145.  
  146. //BBoard.h
  147. #ifndef BBOARD_H
  148. #define BBOARD_H
  149.  
  150. #include <string>
  151. #include <vector>
  152. #include <fstream>
  153.  
  154. using namespace std;
  155.  
  156. #include "Message.h"
  157. #include "User.h"
  158.  
  159. class BBoard {
  160. private:
  161. string title;
  162. vector<User> userList;
  163. User currentUser;
  164. vector<Message> messageList;
  165.  
  166. public:
  167. // Constructs a board with a default title,
  168. // empty user & message lists,
  169. // and the "default" User
  170. BBoard();
  171.  
  172. // Same as the default constructor except
  173. // it sets the title of the board
  174. BBoard(const string &);
  175.  
  176. // Imports all the authorized users from an input file,
  177. // storing them as User objects in the vector userList
  178. // The name of the input file is passed in as a parameter to this function.
  179. // Returns true if the file opened, false if it did not.
  180. // See specifications for file format.
  181. bool loadUsers(const string &str);
  182.  
  183. // Asks for and validates a user/password.
  184. // Always asks for both user and password combo unless 'q' or 'Q' entered.
  185. // Checks userList to validate user and password.
  186. // If valid, sets currentUser to this User, outputs welcome message,
  187. // then returns true.
  188. // Otherwise outputs error message and then repeats request
  189. // for username/password.
  190. // Continues until valid user/password
  191. // or until 'q' or 'Q' is entered for username.
  192. // If 'q' or 'Q' is entered, does not ask for password, outputs "Bye!"
  193. // and then returns false.
  194. bool login();
  195.  
  196. // Contains main loop of Bulletin Board.
  197. // First verifies a User has been logged in before calling this function.
  198. // (Do not call login function within this function.)
  199. // Returns from **function** immediately if no User logged in (Default User).
  200. // Continues to display menu options to user and performs requested action
  201. // until user chooses to quit.
  202. // See output samples for exact format of menu.
  203. void run();
  204.  
  205. private:
  206. // These are only suggestions, not required helper functions.
  207. // Feel free to make your own private helper functions as you see fit.
  208. void displayMess();
  209. };
  210.  
  211. #endif
  212.  
  213. //BBoard.cpp
  214. #include <iostream>
  215. #include <string>
  216. #include <vector>
  217. #include <fstream>
  218.  
  219. using namespace std;
  220.  
  221. #include "Message.h"
  222. #include "User.h"
  223. #include "BBoard.h"
  224.  
  225. BBoard::BBoard() {
  226. title = "";
  227. userList.resize(0);
  228. User currentUser();
  229. messageList.resize(0);
  230. Message currentMessage();
  231. };
  232.  
  233. BBoard::BBoard(const string &str) {
  234. title = str;
  235. userList.resize(0);
  236. User currentUser();
  237. messageList.resize(0);
  238. Message currentMessage();
  239. };
  240.  
  241. bool BBoard::loadUsers(const string &str) {
  242. ifstream fileIn;
  243. string uname, pass;
  244. fileIn.open(str.c_str());
  245.  
  246. if(!fileIn.is_open()) {
  247. return false;
  248. }
  249. else {
  250. while (fileIn >> uname) {
  251. fileIn >> pass;
  252. User currentUser(uname, pass);
  253. userList.push_back(currentUser);
  254. }
  255. fileIn.close();
  256.  
  257. return true;
  258. }
  259. }
  260.  
  261. bool BBoard::login() {
  262. string uname, pass;
  263. int size = userList.size();
  264.  
  265. cout << "Welcome to " << title << endl;
  266.  
  267. while(true) {
  268. cout << "Enter your username ('Q' or 'q' to quit): ";
  269. cin >> uname;
  270. cout << endl;
  271.  
  272. if ((uname == "q") || (uname == "Q")) {
  273. cout << "Bye!" << endl;
  274. return false;
  275. }
  276.  
  277. cout << "Enter your password: ";
  278. cin >> pass;
  279. cout << endl;
  280.  
  281. for(int i = 0; i < size; i++) {
  282.  
  283. currentUser = userList.at(i);
  284.  
  285. if(currentUser.check(uname, pass)) {
  286. cout << "Welcome back " << currentUser.getUsername() << "!" << endl;
  287. return true;
  288. }
  289. }
  290.  
  291. cout << "Invalid Username or Password!" << endl << endl;
  292. }
  293. }
  294.  
  295. void BBoard::run() {
  296. bool flag = false;
  297. int size = userList.size();
  298.  
  299. for (int i = 0; i < size; i++) {
  300. if (currentUser.getUsername() == userList.at(i).getUsername()) {
  301. flag = true;
  302. }
  303. }
  304.  
  305. if (!flag) {
  306. return;
  307. }
  308. else {
  309. displayMess();
  310. }
  311. }
  312.  
  313. void BBoard::displayMess() {
  314. char choice = ' ';
  315. bool flag = true;
  316. int size = messageList.size();
  317. string sbjct;
  318. string body;
  319. string athr = currentUser.getUsername();
  320.  
  321. while (flag) {
  322. cout << endl << "Menu" << endl;
  323. cout << "- Display Messages ('D' or 'd')" << endl;
  324. cout << "- Add New Message ('N' or 'n')" << endl;
  325. cout << "- Quit ('Q' or 'q')" << endl;
  326. cout << "Choose an action: ";
  327. cin >> choice;
  328. cout << endl;
  329.  
  330. if ((choice == 'q') || (choice == 'Q')) {
  331. cout << "Bye!" << endl;
  332. flag = false;
  333. }
  334. else if ((choice == 'd') || (choice == 'D')) {
  335. if (size == 0) {
  336. cout << "Nothing to Display." << endl;
  337. }
  338. else {
  339. cout << "---------------------------------------------------------" << endl;
  340. for (int i = 0; i < size; i++) {
  341. Message currentMessage = messageList.at(i);
  342. cout << "Message #" << i + 1 << ": ";
  343. currentMessage.display();
  344. cout << "---------------------------------------------------------" << endl;
  345. }
  346. }
  347. }
  348. else {
  349. getline(cin,sbjct);
  350.  
  351. cout << "Enter Subject: ";
  352. getline(cin,sbjct);
  353. cout << endl;
  354.  
  355. cout << "Enter Body: ";
  356. getline(cin, body);
  357. cout << endl;
  358.  
  359. Message currentMessage(athr, sbjct, body);
  360. messageList.push_back(currentMessage);
  361.  
  362. cout << "Message Recorded!" << endl;
  363. size = messageList.size();
  364. }
  365. }
  366. }
  367.  
  368. ****************************************************************************************************
  369. //Main.cpp
  370. #include <iostream>
  371. #include <vector>
  372. #include <string>
  373. #include <fstream>
  374.  
  375. using namespace std;
  376.  
  377. #include "Message.h"
  378. #include "User.h"
  379. #include "BBoard.h"
  380.  
  381. int main() {
  382. string userfile;
  383. cout << "User file?" << endl;
  384. cin >> userfile;
  385. cout << endl;
  386.  
  387. BBoard bb("CS12 Bulletin Board");
  388.  
  389. // load users from file
  390. if (!bb.loadUsers(userfile)) {
  391. cout << "Error loading users from file " << userfile << endl;
  392. return 1;
  393. }
  394.  
  395. if (!bb.login()) {
  396. cout << "Login not successful" << endl;
  397. return 1;
  398. }
  399. bb.run();
  400.  
  401. return 0;
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement