Guest User

Untitled

a guest
Aug 1st, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. /*
  2. * user.cpp
  3. * PROJECTO #1
  4. *
  5. * Created by Miguel Oliveira & Rui Couto on 22/03/2011.
  6. *
  7. */
  8.  
  9. #include "user.h"
  10. #include <string>
  11. #include <iostream>
  12. #include <vector>
  13.  
  14. using namespace std;
  15.  
  16. IdentNum User::numUsers = 0; //number of users begins with 0
  17.  
  18. /*
  19. ########################################
  20. ## CONSTRUCTORS ##
  21. ########################################
  22. */
  23.  
  24. User::User () //used to with the temp objects
  25. {
  26. setID (0);
  27. name = "UNKNOWN NAME USER";
  28. }
  29.  
  30. User::User (string name, string pass) //when adding a user, receives a name and password
  31. {
  32. numUsers++; //the number of users, increases with each new user
  33. setID (numUsers); //the ID of the new user will be the same as the number of users, since number of users is always different
  34. setName (name); //set the name as the name received
  35. setPass (pass); //set the password as the string received as password
  36. setActive(true); //automatically set the status to true
  37. requestedBooks.clear(); //SAFETY MEASURE - making sure the vector is cleared
  38. }
  39.  
  40.  
  41. /*
  42. ########################################
  43. ## GET FUNCTIONS ##
  44. ########################################
  45. */
  46.  
  47. IdentNum User::getID () const //returns id of the user;
  48. {
  49. return ID;
  50. }
  51.  
  52. string User::getName () const //return the name of the user;
  53. {
  54. return name;
  55. }
  56.  
  57. string User::getPass () const //returns the password of the user;
  58. {
  59. return pass;
  60. }
  61.  
  62. bool User::isActive () const //returns the status of the user;
  63. {
  64. return active;
  65. }
  66.  
  67. vector<IdentNum> User::getRequestedBooks () const //returns the vector with the user's requested books
  68. {
  69. return requestedBooks;
  70. }
  71.  
  72. bool User::hasBooksRequested () const //returns true or false if the user has books requested or not
  73. {
  74. if ( requestedBooks.empty() )
  75. return false;
  76. else
  77. return true;
  78. }
  79.  
  80.  
  81. /*
  82. ########################################
  83. ## SET FUNCTIONS ##
  84. ########################################
  85. */
  86.  
  87. void User::setID (IdentNum userID) //define user ID
  88. {
  89. ID = userID;
  90. }
  91.  
  92. void User::setName (string userName) //define user name
  93. {
  94. name = userName;
  95. }
  96.  
  97. void User::setPass (string userPass) //define user password
  98. {
  99. pass = userPass;
  100. }
  101.  
  102. void User::setActive (bool status) //define user status
  103. {
  104. active = status;
  105. }
  106.  
  107. void User::setRequestedBooks (const vector<IdentNum> &booksRequestedByUser) //define requested books based on the vector that contains that information
  108. {
  109. requestedBooks = booksRequestedByUser;
  110.  
  111. if ( requestedBooks != booksRequestedByUser) //SECURITY MEASURE - making sure the vectors were equalized
  112. cerr << "Error defining requestedBooks";
  113. }
  114.  
  115. void User::setNumUsers (IdentNum num) //define number of users
  116. {
  117. numUsers = num;
  118. }
  119.  
  120. void User::borrowBook (IdentNum bookID) //method to borrow the books, receives book ID and adds it to the vector of requestedBooks
  121. {
  122. requestedBooks.push_back(bookID);
  123. }
  124.  
  125. void User::returnBook (IdentNum bookID) //method to return the books, receives book ID
  126. {
  127. for (int i = 0; i < (int)requestedBooks.size(); i++) //searches book ID on the requested books vector, and, if found, deletes it
  128. {
  129. if (requestedBooks[i] == bookID)
  130. requestedBooks.erase(requestedBooks.begin()+i);
  131. }
  132. }
Add Comment
Please, Sign In to add comment