Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.13 KB | None | 0 0
  1. /*
  2. *Progamer's Name: Dillon Bong Zi Hang
  3. *Program Description: Library Management System Login Menu
  4. *Last Updated: 7 - February - 2016
  5. */
  6.  
  7. // Header File
  8. #include"LMS_Header.h"
  9. #include"Functions.h"
  10.  
  11.  
  12. /* Library Management System Login Menu */
  13. int main()
  14. {
  15. // Get username and password and ignore whitespace
  16. printf("***********************************************\n");
  17. printf("* Library Management System *\n");
  18. printf("***********************************************\n\n");
  19. printf("Username: ");
  20. scanf("%s", &u);
  21. fflush(stdin);
  22. printf("\nPassword: ");
  23. scanf("%s", &p);
  24. fflush(stdin);
  25. // Entered username and password is equal to Admin
  26. if ((strcmp(username, u)) == 0 && (strcmp(password, p)) == 0)
  27. {
  28. // Login success from login menu. Go to Main Menu
  29. {
  30. printf("\nLogin Successful\n\n");
  31. system("pause");
  32. system("cls");
  33. Main_Menu();
  34. }
  35. }
  36. else
  37. {
  38. // Login failed from login menu. Re-enter username and password
  39. printf("\nLogin Failed\n\n");
  40. system("pause");
  41. system("cls");
  42. main();
  43. }
  44. system("pause>nul");
  45. return 0;
  46. getch();
  47. }
  48.  
  49. /* Library Management System Main Menu */
  50. int Main_Menu()
  51. {
  52. printf("***********************************************\n");
  53. printf("* Main Menu *\n");
  54. printf("***********************************************\n\n");
  55. printf("1. Add New Book\n");
  56. printf("2. Modify Book Details\n");
  57. printf("3. Remove Book\n");
  58. printf("4. View Book List\n");
  59. printf("5. Book Check In\n");
  60. printf("6. Book Check Out\n");
  61. printf("7. Search Menu\n");
  62. printf("8. View Logs\n");
  63. printf("9. Exit\n\n");
  64. printf("Please select an option: ");
  65. {
  66. // Gets user's option
  67. gets(main_menu_option);
  68. // if user confirmation's string length is equal to 1
  69. if (strlen(main_menu_option) == 1)
  70. {
  71. switch (main_menu_option[0])
  72. {
  73. // Goto Add New Book Menu
  74. case '1':
  75. fflush(stdin);
  76. system("cls");
  77. Books = Add_New_Book(file_pointer);
  78. break;
  79. // Goto Modify Book Details
  80. case '2':
  81. fflush(stdin);
  82. system("cls");
  83. Modify_Book_Details_1();
  84. break;
  85. case '3':
  86. fflush(stdin);
  87. system("cls");
  88. Remove_Book(file_pointer, Books);
  89. break;
  90. case '4':
  91. printf("");
  92. break;
  93. case '5':
  94. printf("");
  95. break;
  96. case '6':
  97. printf("");
  98. break;
  99. case '7':
  100. printf("");
  101. break;
  102. case '8':
  103. printf("");
  104. break;
  105. case '9':
  106. // Terminating the Program
  107. printf("\nProgram will now terminate. Press any key to continue");
  108. system("pause>nul");
  109. exit(0);
  110. break;
  111. default:
  112. // Invalid Input from the user
  113. printf("\nInvalid input please re-enter an option");
  114. system("pause>nul");
  115. system("cls");
  116. Main_Menu();
  117. break;
  118. }
  119. }
  120. else
  121. {
  122. printf("\nInvalid input please re-enter an option");
  123. system("pause>nul");
  124. system("cls");
  125. Main_Menu();
  126. fflush(stdin);
  127. }
  128.  
  129. }
  130. system("pause>nul");
  131. return 0;
  132. getch();
  133. }
  134.  
  135.  
  136. /* Add New Book Menu */
  137. int Add_New_Book(FILE *file_pointer)
  138. {
  139. // Open Books Data Text File in append or create binary mode
  140. file_pointer = fopen(filename, "ab+");
  141. // If file is unable to open, then display error
  142. if (file_pointer == NULL)
  143. {
  144. printf("File cannot be opened.");
  145. system("pause>nul");
  146. system("cls");
  147. Main_Menu();
  148. }
  149. else
  150. {
  151. printf("***********************************************\n");
  152. printf("* Add New Book *\n");
  153. printf("***********************************************\n\n");
  154.  
  155. memset(&t, 0, sizeof(t));
  156. // Accept Book ID as input
  157. printf("1. Book ID: ");
  158. scanf("%99[^\n]s", &t.Book_ID);
  159. fflush(stdin);
  160.  
  161. // Accept Book Title as input
  162. printf("\n2. Book Title: ");
  163. scanf("%199[^\n]s", &t.Book_Title);
  164. fflush(stdin);
  165.  
  166. // Accept Book Edition as input
  167. printf("\n3. Book Edition: ");
  168. scanf("%49[^\n]s", &t.Book_Edition);
  169. fflush(stdin);
  170.  
  171. // Accept Year of Publication as input
  172. printf("\n4. Year of Publication: ");
  173. scanf("%49[^\n]s", &t.Year_of_Publication);
  174. fflush(stdin);
  175.  
  176. // Accept Shelf Location as input
  177. printf("\n5. Shelf Location: ");
  178. scanf("%49[^\n]s", &t.Shelf_Location);
  179. fflush(stdin);
  180.  
  181. // Accept Price as input
  182. printf("\n5. Price(RM): ");
  183. scanf("%19[^\n]s", &t.Price);
  184. fflush(stdin);
  185.  
  186. printf("\nConfirm (y/n)? Type -1 to return to Main Menu: ");
  187. // Indefinite While Loop to Confirm Data
  188. while (1)
  189. {
  190. gets(add_new_book_confirm);
  191. fflush(stdin);
  192. // Compare user input to escape
  193. if (stricmp(add_new_book_confirm, "-1") != 0)
  194. {
  195. // if user confirmation's string length is equal to 1
  196. if (strlen(add_new_book_confirm) == 1)
  197. {
  198. switch (add_new_book_confirm[0])
  199. {
  200. // Save all data into file
  201. case 'y':
  202. {
  203. Add_Record_function(file_pointer);
  204. fclose(file_pointer);
  205. fflush(stdin);
  206. printf("\nRecord Successfully Added. Add new book (y/n): ");
  207. // Indefinite While Loop to get add new book input
  208. while (1)
  209. {
  210. gets(add_more_book_confirm);
  211. fflush(stdin);
  212. if (strlen(add_more_book_confirm) == 1)
  213. {
  214. switch (add_more_book_confirm[0])
  215. {
  216. case 'y':
  217. system("cls");
  218. Add_New_Book(file_pointer);
  219. break;
  220. case 'n':
  221. system("cls");
  222. Main_Menu();
  223. break;
  224. default:
  225. fflush(stdin);
  226. printf("\nInvalid input. Please Re-Enter Confirmation to Add New Book: ");
  227. }
  228. }
  229. else
  230. {
  231. fflush(stdin);
  232. printf("\nInvalid input. Please Re-Enter Confirmation to Add New Book: ");
  233. }
  234. }
  235. break;
  236. }
  237. // Return to Add New Book Menu
  238. case 'n':
  239. {
  240. fflush(stdin);
  241. fclose(file_pointer);
  242. system("cls");
  243. Add_New_Book(file_pointer);
  244. break;
  245. }
  246. // Invalid Input. Re-enter user confirmation
  247. default:
  248. {
  249. fflush(stdin);
  250. printf("\nInvalid input. Please Re-Enter Confirmation: ");
  251. break;
  252. }
  253. }
  254. }
  255. else
  256. {
  257. // Invalid Input. Re-enter user confirmation
  258. printf("\nInvalid input. Please Re-Enter Confirmation: ");
  259. }
  260.  
  261. }
  262. // User input equals to esc then return to main menu
  263. else if (stricmp(add_new_book_confirm, "-1") == 0)
  264. {
  265. fclose(file_pointer);
  266. printf("\nThe program will now return to Main Menu");
  267. system("pause>nul");
  268. system("cls");
  269. Main_Menu();
  270. }
  271. }
  272. }
  273. system("pause>nul");
  274. return 0;
  275. getch();
  276. }
  277.  
  278. /* Modify Book Details 1 Menu */
  279. int Modify_Book_Details_1()
  280. {
  281. // Open Books Data Text File in read mode
  282. file_pointer = fopen(filename, "r");
  283. // If file is unable to open, then display error
  284. if (file_pointer == NULL)
  285. {
  286. printf("The file cannot be opened.");
  287. system("pause>nul");
  288. system("cls");
  289. Main_Menu();
  290. }
  291. else{
  292. printf("***********************************************\n");
  293. printf("* Modify Book Details *\n");
  294. printf("***********************************************\n\n");
  295. printf("No. Book ID Book Title\n");
  296. // while loop to display file data until end of file
  297. num2 = 1;
  298. // Displaying Book ID and Book Title
  299. while (fread(&t, sizeof(struct Books_Data), 1, file_pointer))
  300. {
  301. // Aligning Text
  302. printf(" %d. %-10s %s\n", num2, t.Book_ID, t.Book_Title);
  303. num2++;
  304. }
  305. printf("\nType -1 to return to Main Menu.\nEnter the Book Number to update the book details: ");
  306. // while loop to get user option
  307. while (1)
  308. {
  309. //gets user option
  310. scanf("%d", &Books);
  311. Books -= 1;
  312. fflush(stdin);
  313. if (Books == -2) // Return to Main Menu
  314. {
  315. printf("\nThe program will now return to Main Menu");
  316. system("pause>nul");
  317. system("cls");
  318. Main_Menu();
  319. }
  320. else if (Books < -1) // Invalid Input
  321. {
  322. printf("\nInvalid Option. Please Re-Enter: ");
  323. }
  324. else if (Books >= 0)
  325. {
  326. // Check for Valid Book Number
  327. if (!View_Book_List_function(file_pointer, Books))
  328. {
  329. printf("\nError. Book Number Not Found.\n");
  330. fclose(file_pointer);
  331. system("pause>nul");
  332. system("cls");
  333. Modify_Book_Details_1();
  334. }
  335. else
  336. {
  337. system("cls");
  338. fclose(file_pointer);
  339. Modify_Book_Details_2(file_pointer, Books);
  340. }
  341. }
  342. else
  343. {
  344. printf("\nInvalid Input. Please Re-Enter: ");
  345. }
  346. }
  347. }
  348. system("pause>nul");
  349. return 0;
  350. getch();
  351. }
  352.  
  353. /* Modify Book Details 2 Menu */
  354. int Modify_Book_Details_2(FILE *file_pointer, int BooksNum)
  355. {
  356. // Open file in read and binary mode
  357. file_pointer = fopen(filename, "rb+");
  358. // If file is unable to open, then display error
  359. if (file_pointer == NULL)
  360. {
  361. printf("The file cannot be opened.");
  362. system("pause>nul");
  363. system("cls");
  364. Main_Menu();
  365. }
  366. else{
  367. // Display Book Details
  368. printf("***********************************************\n");
  369. printf("* Modify Book Details *\n");
  370. printf("***********************************************\n\n");
  371. printf("Book Number : %d\n\n", BooksNum + 1);
  372. printf("1. Book ID : %s\n", t.Book_ID);
  373. printf("2. Book Title : %s\n", t.Book_Title);
  374. printf("3. Book Edition : %s\n", t.Book_Edition);
  375. printf("4. Year of Publication: %s\n", t.Year_of_Publication);
  376. printf("5. Shelf Location : %s\n", t.Shelf_Location);
  377. printf("6. Price(RM) : %s\n", t.Price);
  378. printf("7. Save Book Details.\n\n");
  379. printf("8. Please select an option. Type -1 to return to previous menu: ");
  380. // indefinite while loop to get user option
  381. while (1)
  382. {
  383. scanf("%d", &option);
  384. fflush(stdin);
  385. switch (option)
  386. {
  387. // Modify Book ID
  388. case 1:
  389. {
  390. system("cls");
  391. printf("1. Current Book ID: %s\n\n", t.Book_ID);
  392. printf("2. New Book ID: ");
  393. scanf("%99[^\n]s", t.Book_ID);
  394. fflush(stdin);
  395. system("cls");
  396. Modify_Book_Details_2(file_pointer, BooksNum);
  397. break;
  398. }
  399. // Modify Book Title
  400. case 2:
  401. {
  402. system("cls");
  403. printf("1. Current Book Title: %s\n\n", t.Book_Title);
  404. printf("2. New Book Title: ");
  405. scanf("%199[^\n]s", t.Book_Title);
  406. fflush(stdin);
  407. system("cls");
  408. Modify_Book_Details_2(file_pointer, BooksNum);
  409. break;
  410. }
  411. // Modify Book Edition
  412. case 3:
  413. {
  414. system("cls");
  415. printf("1. Current Book Edition: %s\n\n", t.Book_Edition);
  416. printf("2. New Book Edition: ");
  417. scanf("%49[^\n]s", t.Book_Edition);
  418. fflush(stdin);
  419. system("cls");
  420. Modify_Book_Details_2(file_pointer, BooksNum);
  421. break;
  422. }
  423. // Modify Year of Publication
  424. case 4:
  425. {
  426. system("cls");
  427. printf("1. Current Year of Publication: %s\n\n", t.Year_of_Publication);
  428. printf("2. New Year of Publication: ");
  429. scanf("%49[^\n]s", t.Year_of_Publication);
  430. fflush(stdin);
  431. system("cls");
  432. Modify_Book_Details_2(file_pointer, BooksNum);
  433. break;
  434. }
  435. // Modify Shelf Location
  436. case 5:
  437. {
  438. system("cls");
  439. printf("1. Current Shelf Locatin: %s\n\n", t.Shelf_Location);
  440. printf("2. New Shelf Location: ");
  441. scanf("%49[^\n]s", t.Shelf_Location);
  442. fflush(stdin);
  443. system("cls");
  444. Modify_Book_Details_2(file_pointer, BooksNum);
  445. break;
  446. }
  447. // Modify Price
  448. case 6:
  449. {
  450. system("cls");
  451. printf("1. Current Price(RM): %s\n\n", t.Price);
  452. printf("2. New Price(RM): ");
  453. scanf("%49[^\n]s", t.Price);
  454. fflush(stdin);
  455. system("cls");
  456. Modify_Book_Details_2(file_pointer, BooksNum);
  457. break;
  458. }
  459. // Save all details into file
  460. case 7:
  461. {
  462. if (Add_New_Book_function(file_pointer, BooksNum))
  463. {
  464. printf("\nBook Details Updated.\n");
  465. }
  466. fclose(file_pointer);
  467. system("pause>nul");
  468. system("cls");
  469. Modify_Book_Details_1();
  470. break;
  471. }
  472. // Return to Previous Menu
  473. case -1:
  474. {
  475. printf("\nThe program will now return to Main Menu");
  476. system("pause>nul");
  477. system("cls");
  478. Modify_Book_Details_1();
  479. break;
  480. }
  481. // User option is invalid
  482. default:
  483. {
  484. printf("\nInvalid Input. Please Re-Enter: ");
  485. break;
  486. }
  487. }
  488. }
  489. }
  490. system("pause>nul");
  491. return 0;
  492. getch();
  493. }
  494.  
  495. int Remove_Book(FILE *file_pointer, int BooksNum)
  496. {
  497. // Open Books Data Text File in read and binary mode
  498. file_pointer = fopen(filename, "rb");
  499. // If file canont be opened then display error
  500. if (!file_pointer) {
  501. printf("The file cannot be opened.");
  502. system("pause>nul");
  503. system("cls");
  504. Main_Menu();
  505. }
  506. // Open Temp Books Data in write and binary mode
  507. file_pointer_temp = fopen("temp.dat", "a");
  508.  
  509. // If file cannot be opened then display error
  510. if (!file_pointer_temp) {
  511. printf("The temp file cannot be opened.");
  512. system("pause>nul");
  513. system("cls");
  514. Main_Menu();
  515. }
  516. else
  517. {
  518. printf("***********************************************\n");
  519. printf("* Remove Book *\n");
  520. printf("***********************************************\n\n");
  521. printf("No. Book ID Book Title\n");
  522. num2 = 1;
  523. // Displaying Book ID and Book Title
  524. while (fread(&t, sizeof(struct Books_Data), 1, file_pointer))
  525. {
  526. // Aligning Text
  527. printf(" %d. %-10s %s\n", num2, t.Book_ID, t.Book_Title);
  528. num2++;
  529. }
  530. printf("\nType -1 to return to Main Menu.\nEnter the booknumber to be removed: ");
  531. // while loop to get user option
  532. while (1)
  533. {
  534. //gets user option
  535. scanf("%d", &Books);
  536. Books -= 1;
  537. fflush(stdin);
  538. if (Books == -2) // Return to Main Menu
  539. {
  540. printf("\nThe program will now return to Main Menu");
  541. system("pause>nul");
  542. system("cls");
  543. Main_Menu();
  544. }
  545. else if (Books < -1) // Invalid Input
  546. {
  547. printf("\nInvalid Option. Please Re-Enter: ");
  548. }
  549. else if (Books >= 0)
  550. {
  551. // Check for Valid Book Number
  552. if (!View_Book_List_function(file_pointer, Books))
  553. {
  554. printf("\nError. Book Number Not Found.\n");
  555. fclose(file_pointer);
  556. fclose(file_pointer_temp);
  557. system("pause>nul");
  558. system("cls");
  559. Remove_Book(file_pointer, BooksNum);
  560. }
  561. else
  562. {
  563. // Search the file for book number
  564. while (fread(&t, sizeof(struct Books_Data), 1, file_pointer) != NULL) {
  565. if (BooksNum == Books) {
  566. printf("\nThe Book Number was found and deleted.\n\n");
  567. found = 1;
  568. }
  569. // Write the unmatched book number into the temporary file
  570. else {
  571. fwrite(&t, sizeof(struct Books_Data), 1, file_pointer_temp);
  572. }
  573. }
  574. // No Book Number found
  575. if (!found) {
  576. printf("\nNo Book Number found");
  577. system("pause>nul");
  578. system("cls");
  579. Remove_Book(file_pointer, BooksNum);
  580. }
  581. else
  582. {
  583. // Close the file pointers
  584. fclose(file_pointer);
  585. fclose(file_pointer_temp);
  586. // Remove the current file and rename the temporary file
  587. remove("Books Data.dat");
  588. rename("temp.dat", "Books Data.dat");
  589. system("pause>nul");
  590. system("cls");
  591. Main_Menu();
  592. }
  593. }
  594. }
  595. else
  596. {
  597. printf("\nInvalid Input. Please Re-Enter: ");
  598. }
  599. }
  600. }
  601. system("pause>nul");
  602. return 0;
  603. getch();
  604. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement