Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1.  
  2. #include<iostream>
  3. #include<iomanip>
  4. #include<fstream>
  5. #include<cmath>
  6. #include<cstdbool>
  7. #include<string>
  8. #include<conio.h>
  9. #include<vector>
  10. #include<string>
  11. #include<algorithm>
  12. #include<cstdio>
  13. #include<limits>
  14. #include<cctype>
  15.  
  16. using namespace std;
  17.  
  18. void book_issue_menu();
  19. void user_issue_menu();
  20.  
  21. class book
  22. {
  23. int ID_book;
  24. string title, author;
  25. public:
  26. void add_book();
  27. void search_book();
  28. void show_books();
  29. book();
  30. ~book();
  31. };
  32. book::book()
  33. {}
  34. book::~book()
  35. {}
  36.  
  37. void book::add_book() {
  38. cout << "Creating new book: " << endl;
  39. cout << "ID: ";
  40. while (!(cin >> ID_book))
  41. {
  42. cout << "ID has to be a number.\nID:";
  43. cin.clear();
  44. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  45. }
  46.  
  47. fflush;
  48. cin.get();
  49.  
  50. cout << "Title: " << "\t";
  51. getline(cin, title);
  52.  
  53. cout << endl << "Author: ";
  54. while (getline(cin, author), find_if(author.begin(), author.end(), isalpha) == author.end())
  55. {
  56. cout << "You have to enter letters.\nAuthor: ";
  57. }
  58.  
  59. fstream file("books.txt", ios::app);
  60. if (file.good() == true)
  61. {
  62. file << ID_book << endl;
  63. file << title << endl;
  64. file << author << endl;
  65. file.close();
  66. }
  67. cout << endl;
  68. }
  69. void book::search_book() {
  70. string title_1;
  71. string line;
  72. bool check = true;
  73.  
  74. cout << "\nEnter title of book to search: ";
  75. cin.get();
  76. getline(cin, title_1);
  77.  
  78. fstream file("books.txt");
  79.  
  80. if (file.good() == true)
  81. {
  82. while (getline(file, line))
  83. {
  84. title = line;
  85.  
  86. if ((title == title_1))
  87. {
  88. cout << "Book in stock.\n";
  89. check = false;
  90. break;
  91. }
  92. }
  93. if (check == true)
  94. cout << "Book not in stock.\n";
  95. }
  96. else
  97. {
  98. cout << "Cannot open the file.\n";
  99. }
  100. }
  101. void book::show_books() {
  102. string helper, line;
  103. int i = 0, line_counter = 0;
  104.  
  105. cout << "BOOK LIST" << endl;
  106.  
  107. fstream file("books.txt", ios::in);
  108.  
  109. if (file.good() == true)
  110. {
  111. while (getline(file, helper))
  112. {
  113. if (i == 0) cout << "ID:";
  114. else if (i == 1) cout << "Title:";
  115. else if (i == 2)
  116. {
  117. cout << "Author:";
  118. i = -1;
  119. }
  120. i++;
  121. cout << helper << endl;
  122. line_counter++;
  123. }
  124. file.close();
  125. }
  126. cout << "Number of books: " << line_counter / 3 << endl << endl;
  127. }
  128.  
  129. class user
  130. {
  131. int ID_user;
  132. string name;
  133. public:
  134. void add_user();
  135. void search_user();
  136. void show_users();
  137. user();
  138. ~user();
  139.  
  140. };
  141. user::user()
  142. {
  143. }
  144. user::~user()
  145. {
  146. }
  147.  
  148. void user::add_user() {
  149.  
  150. cout << "Creating new user: " << endl;
  151. cout << "ID: ";
  152. while (!(cin >> ID_user))
  153. {
  154. cout << "ID has to be a number.\nID:";
  155. cin.clear();
  156. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  157. }
  158.  
  159. fflush;
  160. cin.get();
  161.  
  162. cout << endl << "Name: ";
  163. while (getline(cin, name), find_if(name.begin(), name.end(), isalpha) == name.end())
  164. {
  165. cout << "You have to enter letters.\Name: ";
  166. }
  167.  
  168. fstream file("users.txt", ios::app);
  169. if (file.good() == true)
  170. {
  171. file << ID_user << endl;
  172. file << name << endl;
  173.  
  174. file.close();
  175. }
  176. cout << endl;
  177. }
  178. void user::search_user() {
  179. string name_1;
  180. string line;
  181. bool check = true;
  182.  
  183. cout << "\nEnter name of user to search: ";
  184. cin.get();
  185. getline(cin, name_1);
  186.  
  187. fstream file("books.txt");
  188.  
  189. if (file.good() == true)
  190. {
  191. while (getline(file, line))
  192. {
  193. name = line;
  194.  
  195. if ((name == name_1))
  196. {
  197. cout << "User in base.\n";
  198. check = false;
  199. break;
  200. }
  201. }
  202. if (check == true)
  203. cout << "User not in base.\n";
  204. }
  205. }
  206. void user::show_users() {
  207. string helper;
  208. int i = 0;
  209.  
  210. cout << "USER LIST" << endl;
  211.  
  212. fstream file("books.txt", ios::in);
  213.  
  214. if (file.good() == true)
  215. {
  216. while (getline(file, helper))
  217. {
  218. if (i == 0) cout << "ID:";
  219. else if (i == 1)
  220. {
  221. cout << "Name:";
  222. i = -1;
  223. }
  224. i++;
  225. cout << helper << endl;
  226. file.close();
  227. }
  228. }
  229. cout << endl;
  230. }
  231.  
  232. int main()
  233. {
  234. char choice;
  235. choice = 0;
  236.  
  237. while (choice < '1' || choice > '3')
  238. {
  239. cout << "---=== Library ==---" << endl;
  240. cout << "What do you want to do?"
  241. << endl << "1. Book Menu"
  242. << endl << "2. User Menu"
  243. << endl << "3. Exit." << endl
  244. << "Enter your choice: " << "\t";
  245. cin >> choice;
  246. switch (choice) {
  247. case '1':
  248. system("cls");
  249. book_issue_menu();
  250. break;
  251. case '2':
  252. system("cls");
  253. user_issue_menu();
  254. break;
  255. case '3':
  256. system("cls");
  257. break;
  258. }
  259. }
  260. return 0;
  261. }
  262.  
  263. void book_issue_menu() {
  264. book b1;
  265. int choice;
  266. choice = 0;
  267.  
  268. while (choice != 4)
  269. {
  270. cout << "---=== BOOK MENU ==---" << endl;
  271. cout << "What do you want to do?"
  272. << endl << "1. Add new book."
  273. << endl << "2. Search book."
  274. << endl << "3. Display all books."
  275. << endl << "4. Exit to main menu." << endl
  276. << "Enter your choice: " << "\t";
  277. cin >> choice;
  278. switch (choice) {
  279. case 1:
  280. system("cls");
  281. b1.add_book();
  282. break;
  283. case 2:
  284. system("cls");
  285. b1.search_book();
  286. break;
  287. case 3:
  288. system("cls");
  289. b1.show_books();
  290. break;
  291. case 4:
  292. system("cls");
  293. return;
  294. }
  295. }
  296. }
  297. void user_issue_menu() {
  298. user u1;
  299. int choice;
  300. choice = 0;
  301.  
  302. while (choice != 4)
  303. {
  304. cout << "---=== BOOK MENU ==---" << endl;
  305. cout << "What do you want to do?"
  306. << endl << "1. Add new user."
  307. << endl << "2. Search user."
  308. << endl << "3. Display all users."
  309. << endl << "4. Exit to main menu." << endl
  310. << "Enter your choice: " << "\t";
  311. cin >> choice;
  312. switch (choice) {
  313. case 1:
  314. system("cls");
  315. u1.add_user();
  316. break;
  317. case 2:
  318. system("cls");
  319. u1.search_user();
  320. break;
  321. case 3:
  322. system("cls");
  323. u1.show_users();
  324. break;
  325. case 4:
  326. system("cls");
  327. return;
  328. }
  329. }
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement