Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
125
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. #include <windows.h>
  3.  
  4. using namespace std;
  5.  
  6. enum Gender{MALE=1, FEMALE=2};
  7.  
  8. struct Date{
  9. int day;
  10. int month;
  11. int year;
  12. Date(){
  13. day=1;
  14. month=1;
  15. year=1990;
  16. }
  17. Date(int day, int month, int year){
  18. this->day = day;
  19. this->month=month;
  20. this->year=year;
  21. }
  22. };
  23.  
  24. struct User{
  25. int u_id;
  26. string u_name;
  27. string username;
  28. string password;
  29. string email;
  30. Date birthday;
  31. Gender gender;
  32. string phone;
  33. User(){}
  34. User(int _u_id, string _u_name, string _username, string _password, string _email, Date _birthday, Gender _gender, string _phone){
  35. u_id = _u_id;
  36. u_name = _u_name;
  37. username = _username;
  38. password = _password;
  39. email = _email;
  40. birthday = _birthday;
  41. gender = _gender;
  42. phone = _phone;
  43. }
  44. };
  45.  
  46. void printUser(const User& u){
  47. cout <<"\tUsername: " <<u.username << " - " <<"Email: "<< u.email << endl;
  48. }
  49.  
  50. void printAllUsers(const User us[], int size){
  51. for(int i=0; i<size; i++){
  52. printUser(us[i]);
  53. }
  54. }
  55.  
  56. void insert(User us[], int& size, User u){
  57. us[size++] = u;
  58. }
  59.  
  60. void deleteUser(User us[], int& size, int u_id){
  61. int findIndex = -1;
  62. for(int i=0; i<size; i++){
  63. if(us[i].u_id==u_id){
  64. findIndex = i;
  65. break;
  66. }
  67. }
  68. if(findIndex==-1){
  69. cout << "Couldn't find your id ";
  70. }else{
  71. for(int i=findIndex; i<size-1; i++){
  72. us[i] = us[i+1];
  73. }
  74. }
  75. size--;
  76. }
  77.  
  78. User enterInfo(){
  79. User temp;
  80. cout<<"Enter your ID: ";
  81. cin>>temp.u_id;
  82. cin.ignore();
  83. cout<<"Enter your name: ";
  84. getline(cin,temp.u_name);
  85. cout<<"Enter username: ";
  86. cin>>temp.username;
  87. cin.ignore();
  88. cout<<"Enter password: ";
  89. cin>>temp.password;
  90. cin.ignore();
  91. cout<<"Enter email: ";
  92. cin>>temp.email;
  93. cin.ignore();
  94. cout<<"Enter phone number: ";
  95. cin>>temp.phone;
  96. cout<<"Enter birtday (dd/mm/yy): ";
  97. char c;
  98. cin>>temp.birthday.day>>c>>temp.birthday.month>>c>>temp.birthday.year;
  99. cin.ignore();
  100. int gender;
  101. cout<<"Enter gender(MALE=1/FEMALE=2): ";
  102. cin>>gender;
  103. if(gender==1)
  104. temp.gender=MALE;
  105. else
  106. temp.gender=FEMALE;
  107. return temp;
  108. }
  109.  
  110. struct Book{
  111. int book_id;
  112. string name;
  113. int current_u;
  114. User user[30];
  115. Book(){};
  116. Book(int id,string name){
  117. current_u=0;
  118. this->name=name;
  119. book_id=id;
  120. }
  121. };
  122.  
  123. bool checkBookValid(int id,const Book book[],int current_book){
  124. for(int i=0;i<current_book;i++)
  125. if(book[i].book_id==id){
  126. return 0;
  127. cout<<"OK";
  128. }
  129. return 1;
  130. }
  131.  
  132.  
  133. int main(){
  134. int current_book=3,id;
  135. Book book[100];
  136. book[0]=Book(182852,"Co Gai Den Tu Hom Qua");
  137. book[1]=Book(257874,"Toi Thay Hoa Vang Tren Co Xanh");
  138. book[2]=Book(125571,"Mat Biec");
  139. char respond;
  140. while(1){
  141. cout<<"Current book available in the library:"<<endl;
  142. for(int i=0;i<current_book;i++){
  143. cout<<"\tID: "<<book[i].book_id<<" - Name: "<<book[i].name<<endl;
  144. }
  145. cout<<endl;
  146. cout<<"'b' to borrow a book"<<endl;
  147. cout<<"'r' to return a book"<<endl;
  148. cout<<"'s' to show all user's info borrowed book"<<endl;
  149. cout<<"'i' to insert new book to library"<<endl;
  150. cout<<"'q' to quit"<<endl;
  151. cout<<"\n Enter key: ";
  152. cin>>respond;
  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. system("cls");
  217. }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement