Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. enum Gender{MALE=1, FEMALE=2};
  6.  
  7. struct Date{
  8. int day;
  9. int month;
  10. int year;
  11. Date(){
  12. day=1;
  13. month=1;
  14. year=1990;
  15. }
  16. Date(int day, int month, int year){
  17. this->day = day;
  18. this->month=month;
  19. this->year=year;
  20. }
  21. };
  22.  
  23. struct User{
  24. int u_id;
  25. string u_name;
  26. string username;
  27. string password;
  28. string email;
  29. Date birthday;
  30. Gender gender;
  31. string phone;
  32. User(){}
  33. User(int _u_id, string _u_name, string _username, string _password, string _email, Date _birthday, Gender _gender, string _phone){
  34. u_id = _u_id;
  35. u_name = _u_name;
  36. username = _username;
  37. password = _password;
  38. email = _email;
  39. birthday = _birthday;
  40. gender = _gender;
  41. phone = _phone;
  42. }
  43. };
  44.  
  45. void printUser(const User& u){
  46. cout <<"\tUsername: " <<u.username << " - " <<"Email: "<< u.email << endl;
  47. }
  48.  
  49. void printAllUsers(const User us[], int size){
  50. for(int i=0; i<size; i++){
  51. printUser(us[i]);
  52. }
  53. }
  54.  
  55. void insert(User us[], int& size, User u){
  56. us[size++] = u;
  57. }
  58.  
  59. void deleteUser(User us[], int& size, int u_id){
  60. int findIndex = -1;
  61. for(int i=0; i<size; i++){
  62. if(us[i].u_id==u_id){
  63. findIndex = i;
  64. break;
  65. }
  66. }
  67. if(findIndex==-1){
  68. cout << "Couldn't find your id ";
  69. }else{
  70. for(int i=findIndex; i<size-1; i++){
  71. us[i] = us[i+1];
  72. }
  73. }
  74. size--;
  75. }
  76.  
  77. User enterInfo(){
  78. User temp;
  79. cout<<"Enter your ID: ";
  80. cin>>temp.u_id;
  81. cin.ignore();
  82. cout<<"Enter your name: ";
  83. getline(cin,temp.u_name);
  84. cout<<"Enter username: ";
  85. cin>>temp.username;
  86. cin.ignore();
  87. cout<<"Enter password: ";
  88. cin>>temp.password;
  89. cin.ignore();
  90. cout<<"Enter email: ";
  91. cin>>temp.email;
  92. cin.ignore();
  93. cout<<"Enter phone number: ";
  94. cin>>temp.phone;
  95. cout<<"Enter birtday (dd/mm/yy): ";
  96. char c;
  97. cin>>temp.birthday.day>>c>>temp.birthday.month>>c>>temp.birthday.year;
  98. cin.ignore();
  99. int gender;
  100. cout<<"Enter gender(MALE=1/FEMALE=2): ";
  101. cin>>gender;
  102. if(gender==1)
  103. temp.gender=MALE;
  104. else
  105. temp.gender=FEMALE;
  106. return temp;
  107. }
  108.  
  109. struct Book{
  110. int book_id;
  111. string name;
  112. int current_u;
  113. User user[30];
  114. Book(){};
  115. Book(int id,string name){
  116. current_u=0;
  117. this->name=name;
  118. book_id=id;
  119. }
  120. };
  121.  
  122. bool checkBookValid(int id,const Book book[],int current_book){
  123. for(int i=0;i<current_book;i++)
  124. if(book[i].book_id==id){
  125. return 0;
  126. cout<<"OK";
  127. }
  128. return 1;
  129. }
  130.  
  131.  
  132. int main(){
  133. int current_book=3,id;
  134. Book book[100];
  135. book[0]=Book(182852,"Co Gai Den Tu Hom Qua");
  136. book[1]=Book(257874,"Toi Thay Hoa Vang Tren Co Xanh");
  137. book[2]=Book(125571,"Mat Biec");
  138. char respond;
  139. while(1){
  140. cout<<"Current book available in the library:"<<endl;
  141. for(int i=0;i<current_book;i++){
  142. cout<<"\tID: "<<book[i].book_id<<" - Name: "<<book[i].name<<endl;
  143. }
  144. cout<<endl;
  145. cout<<"'b' to borrow a book"<<endl;
  146. cout<<"'r' to return a book"<<endl;
  147. cout<<"'s' to show all user's info borrowed book"<<endl;
  148. cout<<"'i' to insert new book to library"<<endl;
  149. cout<<"'q' to quit"<<endl;
  150. cout<<"\n Enter key: ";
  151. cin>>respond;
  152. cout<<endl;
  153. switch(respond){
  154. case 'i':{
  155. int b_id;
  156. string name;
  157. do{
  158. cout<<"Enter new book ID: ";
  159. cin>>b_id;
  160. }while(!checkBookValid(b_id,book,current_book));
  161. cout<<"Enter new book name: ";
  162. cin.ignore();
  163. getline(cin,name);
  164. book[current_book].book_id=b_id;
  165. book[current_book].name=name;
  166. current_book++;
  167. break;
  168. }
  169. case 'b': {
  170. cout<<"Enter book ID: ";
  171. cin>>id;
  172. int i=0;
  173. for(;book[i].book_id!=id && i<=current_book;i++);
  174. if(i==3){
  175. cout<<"Couldn't find book ID " <<id<<endl;
  176. break;
  177. }
  178. else
  179. cout<<"BOOK NAME: "<<"\t"<<book[i].name<<endl;
  180. User temp=enterInfo();
  181. insert(book[i].user,book[i].current_u,temp);
  182. break;
  183. }
  184. case 's':{
  185. for(int i=0;i<current_book;i++){
  186. cout<<"BOOK: "<<book[i].name<<endl;
  187. printAllUsers(book[i].user,book[i].current_u);
  188. cout << "---------------------" << endl;
  189. }
  190. break;
  191. }
  192. case 'r':{
  193. int b_id,u_id;
  194. cout<<"Enter book ID to return :";
  195. cin>>b_id;
  196. int i=0;
  197. for(;book[i].book_id!=id && i<=current_book;i++);
  198. if(i==3){
  199. cout<<"Couldn't find book ID " <<id<<endl;
  200. break;
  201. }
  202. else{
  203. cout<<"BOOK NAME: "<<"\t"<<book[i].name<<endl;
  204. cout<<"Enter your user ID: ";
  205. cin>>u_id;
  206. deleteUser(book[i].user,book[i].current_u,u_id);
  207. break;
  208. }
  209.  
  210. }
  211. case 'q':
  212. return 0;
  213. default:
  214. cout<<"Invalid key"<<endl;
  215. }
  216. }
  217. cout<<endl<<endl;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement