Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *Progamer's Name: Dillon Bong Zi Hang
- *Program Description: Library Management System Login Menu
- *Last Updated: 7 - February - 2016
- */
- // Header File
- #include"LMS_Header.h"
- #include"Functions.h"
- /* Library Management System Login Menu */
- int main()
- {
- // Get username and password and ignore whitespace
- printf("***********************************************\n");
- printf("* Library Management System *\n");
- printf("***********************************************\n\n");
- printf("Username: ");
- scanf("%s", &u);
- fflush(stdin);
- printf("\nPassword: ");
- scanf("%s", &p);
- fflush(stdin);
- // Entered username and password is equal to Admin
- if ((strcmp(username, u)) == 0 && (strcmp(password, p)) == 0)
- {
- // Login success from login menu. Go to Main Menu
- {
- printf("\nLogin Successful\n\n");
- system("pause");
- system("cls");
- Main_Menu();
- }
- }
- else
- {
- // Login failed from login menu. Re-enter username and password
- printf("\nLogin Failed\n\n");
- system("pause");
- system("cls");
- main();
- }
- system("pause>nul");
- return 0;
- getch();
- }
- /* Library Management System Main Menu */
- int Main_Menu()
- {
- printf("***********************************************\n");
- printf("* Main Menu *\n");
- printf("***********************************************\n\n");
- printf("1. Add New Book\n");
- printf("2. Modify Book Details\n");
- printf("3. Remove Book\n");
- printf("4. View Book List\n");
- printf("5. Book Check In\n");
- printf("6. Book Check Out\n");
- printf("7. Search Menu\n");
- printf("8. View Logs\n");
- printf("9. Exit\n\n");
- printf("Please select an option: ");
- {
- // Gets user's option
- gets(main_menu_option);
- // if user confirmation's string length is equal to 1
- if (strlen(main_menu_option) == 1)
- {
- switch (main_menu_option[0])
- {
- // Goto Add New Book Menu
- case '1':
- fflush(stdin);
- system("cls");
- Books = Add_New_Book(file_pointer);
- break;
- // Goto Modify Book Details
- case '2':
- fflush(stdin);
- system("cls");
- Modify_Book_Details_1();
- break;
- case '3':
- fflush(stdin);
- system("cls");
- Remove_Book(file_pointer, Books);
- break;
- case '4':
- printf("");
- break;
- case '5':
- printf("");
- break;
- case '6':
- printf("");
- break;
- case '7':
- printf("");
- break;
- case '8':
- printf("");
- break;
- case '9':
- // Terminating the Program
- printf("\nProgram will now terminate. Press any key to continue");
- system("pause>nul");
- exit(0);
- break;
- default:
- // Invalid Input from the user
- printf("\nInvalid input please re-enter an option");
- system("pause>nul");
- system("cls");
- Main_Menu();
- break;
- }
- }
- else
- {
- printf("\nInvalid input please re-enter an option");
- system("pause>nul");
- system("cls");
- Main_Menu();
- fflush(stdin);
- }
- }
- system("pause>nul");
- return 0;
- getch();
- }
- /* Add New Book Menu */
- int Add_New_Book(FILE *file_pointer)
- {
- // Open Books Data Text File in append or create binary mode
- file_pointer = fopen(filename, "ab+");
- // If file is unable to open, then display error
- if (file_pointer == NULL)
- {
- printf("File cannot be opened.");
- system("pause>nul");
- system("cls");
- Main_Menu();
- }
- else
- {
- printf("***********************************************\n");
- printf("* Add New Book *\n");
- printf("***********************************************\n\n");
- memset(&t, 0, sizeof(t));
- // Accept Book ID as input
- printf("1. Book ID: ");
- scanf("%99[^\n]s", &t.Book_ID);
- fflush(stdin);
- // Accept Book Title as input
- printf("\n2. Book Title: ");
- scanf("%199[^\n]s", &t.Book_Title);
- fflush(stdin);
- // Accept Book Edition as input
- printf("\n3. Book Edition: ");
- scanf("%49[^\n]s", &t.Book_Edition);
- fflush(stdin);
- // Accept Year of Publication as input
- printf("\n4. Year of Publication: ");
- scanf("%49[^\n]s", &t.Year_of_Publication);
- fflush(stdin);
- // Accept Shelf Location as input
- printf("\n5. Shelf Location: ");
- scanf("%49[^\n]s", &t.Shelf_Location);
- fflush(stdin);
- // Accept Price as input
- printf("\n5. Price(RM): ");
- scanf("%19[^\n]s", &t.Price);
- fflush(stdin);
- printf("\nConfirm (y/n)? Type -1 to return to Main Menu: ");
- // Indefinite While Loop to Confirm Data
- while (1)
- {
- gets(add_new_book_confirm);
- fflush(stdin);
- // Compare user input to escape
- if (stricmp(add_new_book_confirm, "-1") != 0)
- {
- // if user confirmation's string length is equal to 1
- if (strlen(add_new_book_confirm) == 1)
- {
- switch (add_new_book_confirm[0])
- {
- // Save all data into file
- case 'y':
- {
- Add_Record_function(file_pointer);
- fclose(file_pointer);
- fflush(stdin);
- printf("\nRecord Successfully Added. Add new book (y/n): ");
- // Indefinite While Loop to get add new book input
- while (1)
- {
- gets(add_more_book_confirm);
- fflush(stdin);
- if (strlen(add_more_book_confirm) == 1)
- {
- switch (add_more_book_confirm[0])
- {
- case 'y':
- system("cls");
- Add_New_Book(file_pointer);
- break;
- case 'n':
- system("cls");
- Main_Menu();
- break;
- default:
- fflush(stdin);
- printf("\nInvalid input. Please Re-Enter Confirmation to Add New Book: ");
- }
- }
- else
- {
- fflush(stdin);
- printf("\nInvalid input. Please Re-Enter Confirmation to Add New Book: ");
- }
- }
- break;
- }
- // Return to Add New Book Menu
- case 'n':
- {
- fflush(stdin);
- fclose(file_pointer);
- system("cls");
- Add_New_Book(file_pointer);
- break;
- }
- // Invalid Input. Re-enter user confirmation
- default:
- {
- fflush(stdin);
- printf("\nInvalid input. Please Re-Enter Confirmation: ");
- break;
- }
- }
- }
- else
- {
- // Invalid Input. Re-enter user confirmation
- printf("\nInvalid input. Please Re-Enter Confirmation: ");
- }
- }
- // User input equals to esc then return to main menu
- else if (stricmp(add_new_book_confirm, "-1") == 0)
- {
- fclose(file_pointer);
- printf("\nThe program will now return to Main Menu");
- system("pause>nul");
- system("cls");
- Main_Menu();
- }
- }
- }
- system("pause>nul");
- return 0;
- getch();
- }
- /* Modify Book Details 1 Menu */
- int Modify_Book_Details_1()
- {
- // Open Books Data Text File in read mode
- file_pointer = fopen(filename, "r");
- // If file is unable to open, then display error
- if (file_pointer == NULL)
- {
- printf("The file cannot be opened.");
- system("pause>nul");
- system("cls");
- Main_Menu();
- }
- else{
- printf("***********************************************\n");
- printf("* Modify Book Details *\n");
- printf("***********************************************\n\n");
- printf("No. Book ID Book Title\n");
- // while loop to display file data until end of file
- num2 = 1;
- // Displaying Book ID and Book Title
- while (fread(&t, sizeof(struct Books_Data), 1, file_pointer))
- {
- // Aligning Text
- printf(" %d. %-10s %s\n", num2, t.Book_ID, t.Book_Title);
- num2++;
- }
- printf("\nType -1 to return to Main Menu.\nEnter the Book Number to update the book details: ");
- // while loop to get user option
- while (1)
- {
- //gets user option
- scanf("%d", &Books);
- Books -= 1;
- fflush(stdin);
- if (Books == -2) // Return to Main Menu
- {
- printf("\nThe program will now return to Main Menu");
- system("pause>nul");
- system("cls");
- Main_Menu();
- }
- else if (Books < -1) // Invalid Input
- {
- printf("\nInvalid Option. Please Re-Enter: ");
- }
- else if (Books >= 0)
- {
- // Check for Valid Book Number
- if (!View_Book_List_function(file_pointer, Books))
- {
- printf("\nError. Book Number Not Found.\n");
- fclose(file_pointer);
- system("pause>nul");
- system("cls");
- Modify_Book_Details_1();
- }
- else
- {
- system("cls");
- fclose(file_pointer);
- Modify_Book_Details_2(file_pointer, Books);
- }
- }
- else
- {
- printf("\nInvalid Input. Please Re-Enter: ");
- }
- }
- }
- system("pause>nul");
- return 0;
- getch();
- }
- /* Modify Book Details 2 Menu */
- int Modify_Book_Details_2(FILE *file_pointer, int BooksNum)
- {
- // Open file in read and binary mode
- file_pointer = fopen(filename, "rb+");
- // If file is unable to open, then display error
- if (file_pointer == NULL)
- {
- printf("The file cannot be opened.");
- system("pause>nul");
- system("cls");
- Main_Menu();
- }
- else{
- // Display Book Details
- printf("***********************************************\n");
- printf("* Modify Book Details *\n");
- printf("***********************************************\n\n");
- printf("Book Number : %d\n\n", BooksNum + 1);
- printf("1. Book ID : %s\n", t.Book_ID);
- printf("2. Book Title : %s\n", t.Book_Title);
- printf("3. Book Edition : %s\n", t.Book_Edition);
- printf("4. Year of Publication: %s\n", t.Year_of_Publication);
- printf("5. Shelf Location : %s\n", t.Shelf_Location);
- printf("6. Price(RM) : %s\n", t.Price);
- printf("7. Save Book Details.\n\n");
- printf("8. Please select an option. Type -1 to return to previous menu: ");
- // indefinite while loop to get user option
- while (1)
- {
- scanf("%d", &option);
- fflush(stdin);
- switch (option)
- {
- // Modify Book ID
- case 1:
- {
- system("cls");
- printf("1. Current Book ID: %s\n\n", t.Book_ID);
- printf("2. New Book ID: ");
- scanf("%99[^\n]s", t.Book_ID);
- fflush(stdin);
- system("cls");
- Modify_Book_Details_2(file_pointer, BooksNum);
- break;
- }
- // Modify Book Title
- case 2:
- {
- system("cls");
- printf("1. Current Book Title: %s\n\n", t.Book_Title);
- printf("2. New Book Title: ");
- scanf("%199[^\n]s", t.Book_Title);
- fflush(stdin);
- system("cls");
- Modify_Book_Details_2(file_pointer, BooksNum);
- break;
- }
- // Modify Book Edition
- case 3:
- {
- system("cls");
- printf("1. Current Book Edition: %s\n\n", t.Book_Edition);
- printf("2. New Book Edition: ");
- scanf("%49[^\n]s", t.Book_Edition);
- fflush(stdin);
- system("cls");
- Modify_Book_Details_2(file_pointer, BooksNum);
- break;
- }
- // Modify Year of Publication
- case 4:
- {
- system("cls");
- printf("1. Current Year of Publication: %s\n\n", t.Year_of_Publication);
- printf("2. New Year of Publication: ");
- scanf("%49[^\n]s", t.Year_of_Publication);
- fflush(stdin);
- system("cls");
- Modify_Book_Details_2(file_pointer, BooksNum);
- break;
- }
- // Modify Shelf Location
- case 5:
- {
- system("cls");
- printf("1. Current Shelf Locatin: %s\n\n", t.Shelf_Location);
- printf("2. New Shelf Location: ");
- scanf("%49[^\n]s", t.Shelf_Location);
- fflush(stdin);
- system("cls");
- Modify_Book_Details_2(file_pointer, BooksNum);
- break;
- }
- // Modify Price
- case 6:
- {
- system("cls");
- printf("1. Current Price(RM): %s\n\n", t.Price);
- printf("2. New Price(RM): ");
- scanf("%49[^\n]s", t.Price);
- fflush(stdin);
- system("cls");
- Modify_Book_Details_2(file_pointer, BooksNum);
- break;
- }
- // Save all details into file
- case 7:
- {
- if (Add_New_Book_function(file_pointer, BooksNum))
- {
- printf("\nBook Details Updated.\n");
- }
- fclose(file_pointer);
- system("pause>nul");
- system("cls");
- Modify_Book_Details_1();
- break;
- }
- // Return to Previous Menu
- case -1:
- {
- printf("\nThe program will now return to Main Menu");
- system("pause>nul");
- system("cls");
- Modify_Book_Details_1();
- break;
- }
- // User option is invalid
- default:
- {
- printf("\nInvalid Input. Please Re-Enter: ");
- break;
- }
- }
- }
- }
- system("pause>nul");
- return 0;
- getch();
- }
- int Remove_Book(FILE *file_pointer, int BooksNum)
- {
- // Open Books Data Text File in read and binary mode
- file_pointer = fopen(filename, "rb");
- // If file canont be opened then display error
- if (!file_pointer) {
- printf("The file cannot be opened.");
- system("pause>nul");
- system("cls");
- Main_Menu();
- }
- // Open Temp Books Data in write and binary mode
- file_pointer_temp = fopen("temp.dat", "a");
- // If file cannot be opened then display error
- if (!file_pointer_temp) {
- printf("The temp file cannot be opened.");
- system("pause>nul");
- system("cls");
- Main_Menu();
- }
- else
- {
- printf("***********************************************\n");
- printf("* Remove Book *\n");
- printf("***********************************************\n\n");
- printf("No. Book ID Book Title\n");
- num2 = 1;
- // Displaying Book ID and Book Title
- while (fread(&t, sizeof(struct Books_Data), 1, file_pointer))
- {
- // Aligning Text
- printf(" %d. %-10s %s\n", num2, t.Book_ID, t.Book_Title);
- num2++;
- }
- printf("\nType -1 to return to Main Menu.\nEnter the booknumber to be removed: ");
- // while loop to get user option
- while (1)
- {
- //gets user option
- scanf("%d", &Books);
- Books -= 1;
- fflush(stdin);
- if (Books == -2) // Return to Main Menu
- {
- printf("\nThe program will now return to Main Menu");
- system("pause>nul");
- system("cls");
- Main_Menu();
- }
- else if (Books < -1) // Invalid Input
- {
- printf("\nInvalid Option. Please Re-Enter: ");
- }
- else if (Books >= 0)
- {
- // Check for Valid Book Number
- if (!View_Book_List_function(file_pointer, Books))
- {
- printf("\nError. Book Number Not Found.\n");
- fclose(file_pointer);
- fclose(file_pointer_temp);
- system("pause>nul");
- system("cls");
- Remove_Book(file_pointer, BooksNum);
- }
- else
- {
- // Search the file for book number
- while (fread(&t, sizeof(struct Books_Data), 1, file_pointer) != NULL) {
- if (BooksNum == Books) {
- printf("\nThe Book Number was found and deleted.\n\n");
- found = 1;
- }
- // Write the unmatched book number into the temporary file
- else {
- fwrite(&t, sizeof(struct Books_Data), 1, file_pointer_temp);
- }
- }
- // No Book Number found
- if (!found) {
- printf("\nNo Book Number found");
- system("pause>nul");
- system("cls");
- Remove_Book(file_pointer, BooksNum);
- }
- else
- {
- // Close the file pointers
- fclose(file_pointer);
- fclose(file_pointer_temp);
- // Remove the current file and rename the temporary file
- remove("Books Data.dat");
- rename("temp.dat", "Books Data.dat");
- system("pause>nul");
- system("cls");
- Main_Menu();
- }
- }
- }
- else
- {
- printf("\nInvalid Input. Please Re-Enter: ");
- }
- }
- }
- system("pause>nul");
- return 0;
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement