assignment 8 group 2 - final group project report
TRANSCRIPT
1
Assignment 8
Project B: Library Information System
Written by: Group 2
Software Development Process (SDP):
1. Problem Statement:
This program is an electronic rental system of a library in a town called ABC. In this system, there are some features that were included, such as:
• To store the set of books including their author(s), title, ISBN, subject, loan type (normal, short loan, no-‐take-‐out), shelf-‐mark, loan status, number of copies, etc
• To provide options for altering the details of a book, mark it as lost / damaged / ordered, etc.
• To create different privileges, which are the guest, student, teacher, and admin. For example, the teacher and admin can borrow a book for a longer time compared with the students. Admin can see the borrower list, etc.
• To alter the borrowing time • To search for a particular book and allocate a book to a specific borrower for a
specific time period • To implement return and renewal • To print the list of available books and borrowed books • To provide borrower statistics (e.g. about the number of books) to a particular
account
2. Analysis
1. int main() function:
l Inputs: No input in this function
l Outputs: No output in this function
2
2. void mainmenu(void) function:
l Inputs: A number between 1 to 4, 1 leads to registration menu, 2 leads to login menu, 3 leads to search books, and 4 leads to exit this program
l Outputs: The registration(), login(), booksearch(), and exit_program() function according to the number input
3. void wrong_option_sentence(void) function:
l Inputs: No input for this function
l Outputs: The sentence: Please try again, the data you input was wrong.
4. void registration (void) function:
l Inputs: l User’s ID number l User’s first name l Surname l Password l Privilege : 0=guest, 1=student, 2=teacher, 3=administrator
l Outputs:
The sentence: Registration success! Now you have been registered in this library. The data of users are saved in the userlist.txt file
5. void show_userdata (void) function:
l Inputs: There is no input in this function.
l Outputs: This function read the file userlist.txt and store in filenm file variable. It will process the data of users such as user’s ID, surname, given name, password, and privilege.
6. int line_number (FILE *filenm) function:
l Inputs: There is no input in this function.
l Outputs: There is no output in this function.
3
7. void showupdate_userdata (void) function:
l Inputs: This function has no input
l Outputs: This function only open and then write the existing file userlist.txt, which contains the user’s ID, given name, family name, password, and privilege.
8. FILE* file_read (char*) function:
l Inputs: No input in this function, however this function is called when an existing file is needed to be opened and read
l Outputs: The file is opened and saved into the variable filenm, if the file is not exist and cannot be opened, the sentence” The file cannot be opened: (filenm)” will be displayed.
9. FILE* file_write (char*) function:
l Inputs: No input in this function, however this function is called when an existing file is needed to be written and updated
l Outputs: The file is opened and ready to be written, then it is saved into the variable filenm, if the file is not exist and cannot be opened, the sentence” The file cannot be opened: (filenm)” will be displayed.
10. FILE* file_append (char*) function:
l Inputs: No input in this function, however this function is called when an existing file is needed to be appended
l Outputs: The file is opened and ready to be appended, then it is saved into the variable filenm, if the file is not exist and cannot be opened, the sentence” The file cannot be opened: (filenm)” will be displayed.
11. void file_close (FILE *filenm) function:
l Inputs: No input in this function, however this function is called when an existing file is needed to be closed
l Outputs: The file is closed, and if it fail to do so, a sentence “The file cannot be closed: (filenm)” will be displayed.
4
12. void login(void) function:
l Inputs: User needs to input his first name, surname, ID number and password which all are already registered. If all these are correct, user will get into the borrower function and it should obtain other input, or else these input have any mistakes, user will get to main menu and it should obtain some input as well.
l Outputs: Some sentences will be provided on screen to prompt user input corresponding information, and if the password is wrong will also get some prompt. Or else call other functions.
l Additional requirements: Using the strcmp statement to check the alphabetic string with surname and password, only both of surname and password are correct the login in process is successful.
13. void borrower_function(int id, char* name) function:
l Inputs: User should input the order to program according to the sentences which printed on screen.
l Outputs: the menu which is using to prompt user input order should be printed on screen, and other prompt which is using to warm user the happen of mistakes.
l Additional requirements: Different order will call another functions so it must break out after the function.
14. void loan_list(int id, char* name) function:
l Inputs: User needs to choose does him want to renew his books, if he has already borrowed some books.
l Outputs: Some of information about the books which have been borrowed by the user will print on screen. In addition, some prompts and the menu for choosing orders will also be provided on screen.
15. FILE* open_singlebook (int number) function:
l Inputs: There is no input in this function.
l Outputs: The value of filemn will be assigned to this function, and the address of source of the file is decided by the 'book_no', the value filemn is determined by the file_read() function with parameter file_name
l All of these operations is doing in the File, and return a value to the function.
5
16. FILE* write_singlebook (int number) function:
l Inputs: The value of the pointer filemn is determined by the file_write() function with parameter file_name.
l Outputs: The value of filemn will be assigned to this function, and the address of source of the file is decided by the 'book_no'.
l All of these operations is doing in the File, and return a value to the function.
17. char *integerstring_conversion(char *string, int n) function:
l Inputs: This function has no input, however the data that used to be the parameters is come from the other function.
l Outputs: After this function the value will assign to the char type function finally.
18. void show_singlebook(int no) function:
l Inputs: No input from keyboard in this function, but the data all comes from other functions or what already stored in file.
l Outputs: Has no directly output on screen in this function. The data is assign to corresponding character, the data contains last borrowed date, and due date, who borrowed this book, the data is stored in file, and it could display by calling this function in other statement.
l Other requirements: The new data should write at the end of this file, which means the data that already existed cannot be cancelled.
19. void renew(void) function:
l Inputs: User needs to input the number of book that is renew, and the barcode of this book.
l Some of prompts should be display on the screen, prompt user input corresponding data.
20. void admin (int id, char *name) function:
l Inputs: User need to input the order in this function according to the prompts. From 1-‐10.
l Outputs: The menu for each order should be displayed firstly, after input the order other displayof other function would be displayed.
6
l Other requirements: After choose orders must exit this switch function, which program would not automatically back to this function.
21. void borrowmenu(int id, char*name) function:
l Inputs: User needs to choose the order, just either 1 or 2.
l Outputs: The menu for each order should be displayed first, or else user input the order which is neither 1 nor 2 therefore it should prompt user input the correct order. And then the function connects to other functions.
22. void borrow_function (void) function :
l Input: l enter student ID correctly, if user has the privilege, enter the ISBN and the barcode of
the book. l enter user’s ID number, book’s number and book’s barcode.
l Output: l if users input an inexistent ID, it will show “sorry, you have not been registered”; if a
correct ID have been store, it will show “hi guest, you can just search books here”. “The book number is %d” if ISBN has been store. “You could not borrow this book, the book was marked as %s” if the book has been marked by someone
l “you have succeeded borrowing a book.” Or if user entered wrong information, system will display “you could not borrow this book” and “the book’s due date is at %d-‐%d-‐%d” then you can borrow it after this date. “you have succeeded borrowing a book” if succeed.
23. void bookreturn(int, char*) function:
l Input: “please enter your ID number, book’s number and book’s barcode” when return a book.
l Output: “thanks for returning the book” when succeeded and “error occurred” when error.
24. void display_available (int,char*) function:
l Input: No input for this function
l Output: The list of available books
7
25. void display_borrowed (int,char*) function:
l Input: No input for this function
l Output: The list of books that are borrowed currently
26. void display_borrower (int,char*) function:
l Input: No input in this function
l Output: the borrower and the book they borrowed will be displayed
27. void display_users (int,char*) function:
l Input: the member’s ID number that want to be displayed
l Output: Member’s fullname, ID number, and their privilege / status
28. void privilege_changing (int,char*) function:
l Input: the user’s ID whose privilege you want to change, and enter the user’s new privilege.
l Output: “the user’s privilege has been changed” if succeed; “error” if error.
29. void mark_details (int,char*) function:
l Input: enter the number of the book that you want to mark and mark the book’s detail. l Output:
“the book’s detail has been marked” if succeed and “error occurred” if error.
30. void booksearch (void) function:
l Input: here are three menus you can choose if search a book: by subject. By author and by title, choose and enter 1-‐3. Then input the book’s subject, book’s author or book’s title as requirement.
l Output: search by subject and enter the book’s subject: “the subject you entered is too short (it has to be more than 3 letters)” and “sorry, the book cannot be found” if error; “there are %d
8
results and %d pages found” if succeed, then you can choose to see a book’s details, go and search in another page or return to main menu, just choose 1-‐3 and enter a book label or a particular page. Search by author’s name and enter author name:”the author’s name you entered is too short (it has to be more than 3 letters)” and “please enter the book’s author again” if error. Search by book’s title and enter the title: “the title you entered is too short (it has to be more than 3 letters)” and “please enter the book’s title again” if error. After the process, “you have renewed the book” if succeed.
31. void subjectsearch (void) function:
l Inputs: Enter a book subject, which can be fiction or non-‐fiction
l Outputs: Display the list of the books according to the subject input
32. void booklist(int) function:
l Inputs: Enter a number 1-‐3 to see a book details, move to the next page, or return to the main menu
l Outputs: The book details contains label, title, author, and subject, or a list of books on a page, or it can display main menu again according to the number input
33. void search_singlebook(int) function:
l Inputs: No input for this function
l Outputs: Show the information of a single book including book barcode, loan type, loan type, loan
status, the time last borrowed ( day, month and year), due date( day, month and year), the
details of the book and its borrower
34. void authorsearch (void) function:
l Inputs:
An author’s name l Outputs:
Display the list of the books according to the author’s name
35. void titlesearch (void) function:
l Inputs: The title of the book
9
l Outputs: Display the list of the books according to the input title
36. void alteringtime(int, int) function:
l Inputs: There is no input in this function.
l Outputs: There is no output in this function, but the time of the book borrowing will be updated and renewed
37. void update_singlebook (int) function:
l Inputs: This function has no input
l Outputs: Update the information of a single book include the loan type, barcode, due day, due month
and so on.
38. void show_bookinfo (void) function:
l Inputs:
No input in this function, but a database file which is booklist.txt is necessary l Outputs:
It has no specific output but is show the data of a book that could be updated in the next function
39. void update_bookinfo (void) function:
l Inputs: No input in this function, but a database file which is booklist.txt is necessary
l Outputs: Update the information of a collection of books including the book number, title, author, shelf mark, ISBN and so on
40. void exit_program(void) function:
l Inputs: No input in this function
l Outputs: Display the sentence: “Thanks for your visit, please come again next time!”
10
3. Design Algorithm: 1. int main() function:
l Call the show_bookinfo function l Call the show_userdata function l Call the mainmenu function l Return 0 for this main function
2. void mainmenu(void) function:
l Declare one variable of the type of integer namely a l Display the menu and the options l Ask user to input a number between 1-‐4 l Store and save the number input into variable a l Using switch function, call the function according to the number input l If it is 1, call the registration function l If it is 2, call the login function l If it is 3, call the booksearch function l If it is 4, call the exit_program function l Otherwise, the wrong_option_sentence function will be called
3. void wrong_option_sentence(void) function:
l Display the sentence “Please try again, the data you input was wrong.”
4. void registration (void) function:
l Declare two variables of the type of character namely pw and passw l Declare one variable of the type of integer namely a, and initialize it equal to 0 l Declare one variable of the type of integer namely b, and initialize it equal to 0 l Cal the show_userdata function l While a is zero, the registration menu will be appeared l Ask user to input his/her ID number l Read the value and store it into users[number_of_users].ID l If the ID input is more than 8 digits, the sentence “Your ID number should be within 8 digits.” Is
displayed l Ask user to input his/her given name l Read the value and store it into users[number_of_users].givenname l Ask user to input his/her last name l Read the value and store it into users[number_of_users].surname l Ask user to input his/her password l Read the value and store it into users[number_of_users].password
11
l Ask user to input his/her privilege between 0 to 3 l Read the value and store it into users[number_of_users].userprivilege l Display the sentence ”Registration success! Now you have been registered in this library.” l Go back to the main menu by calling mainmenu function
5. void show_userdata (void) function:
l declare one pointer of the type of file namely filenm l declare one variable of the type of integer namely line l declare one variable of the type of integer namely a, and initialize it equal to 0 l declare one variable of the type of integer namely b l declare one variable of the type of character namely c l declare one variable of the type of character namely strarray l assign file_read to filenm l assign line_number (filenm) to line l output the value of users[a].ID into the file which is pointed by filenm l fix the position for filenm renewedly l using fseek to move the any file position in filenm by the offset -‐1L from the current position l assign c to users[a].surname[b] l assign '\0' to users[a].surname[b] l fix the position for filenm renewedly l using fseek to move the any file position in filenm by the offset -‐1L from the current position l assign c to users[a].givenname[b] l assign '\0' to users[a].givenname[b] l assign users[a].surname and blank space to strarray, and then assign it to users[a].fullname l fix the position for filenm renewedly l using fseek to move the any file position in filenm by the offset -‐1L from the current position l assign c to users[a].password[b] l assign '\0' to users[a].password[b] l output the value of users[a].userprivilege into the file which is pointed by filenm l assign line to number_of_users l close the file
6. int line_number (FILE *filenm) function:
l declare one variable of the type of integer namely a l declare one variable of the type of integer namely l, and initialize it equal to 0* l when the a ='\n', the value of l plus one l let filenm point to the outset again l this function return the value of 1
12
7. void showupdate_userdata (void) function:
l declare one pointer of the type of file namely filenm l declare one variable of the type of integer namely a l assign file_read to filenm l input the data of user to the file l close the file
8. FILE* file_read (char*) function:
l declare one pointer of the type of file namely filenm l assign afile to filename l open and read the file of filename and assign it to filenm l when the file is not exist, display the sentence “The file cannot be opened: (filenm)” l this function return the value as filenm
9. FILE* file_write (char*) function:
l declare one pointer of the type of file namely filenm l assign afile to filename l open and write on the file of filename and assign it to filenm l when the file is not exist, display the sentence “The file cannot be opened: (filenm)” l this function return the value as filenm
10. FILE* file_append (char*) function:
l declare one pointer of the type of file namely filenm l assign afile to filename l open and append the file of filename and assign it to filenm l when the file is not exist, display the sentence “The file cannot be opened: (filenm)” l this function return the value as filenm
11. void file_close (FILE *filenm) function:
l Close the file l When the file is not exist, display the sentence “The file cannot be closed: (filenm)”
12. void login(void) function:
l Declare the function into no return value and no parameters
l Declare necessary characters into int type and initial the value is zero, and three char type of characters.
l Use printf statement to prompt user input the information of user which contains first name, surname, ID number and the self-‐defined password.
13
l If the serial number of user less than the defined number of users call the while loop, and inset the if statement to the while loop and determine following implementation.
l First if statement is using to check do the input surname and password coincide with the given surname and password which have registered already. After that using a nested if statement to the last statement which is using to check the privilege of user who are login in.
l If the privilege of user is 1 or 2 call the borrower_function() function; or if the privilege of user is 3, call the admin() function, or else prompt user who is just guest, and calling the mainmenu() function to back main menu in which the privilege of user just guest.
l And after the fist if statement finished make b plus 1 to itself, and prompt user as well, and then call the mainmenu() function to back main menu.
13. void borrower_function(int id, char* name) function:
l Declare the function into has no return value but has two parameters, which are int type id, and char type pointer name respectively.
l Declare b into char type and has eight letter, and declare a and i into int type, initial i with zero.
l Use printf statements to display the menu on screen to prompt user to switch.
l Use switch statement to judge the input value and determine the following statement that will be executed.
l For case 1, call the loan_list() function and then break this switch statement.
l For case 2, call the renew() function and then break this switch statement.
l For case 3, use a while loop with condition in serial number less than defined number of users. After that, use a if statement to check the ID of user, if the ID is correct we using printf statement to prompt user input his new password and using scanf statement to store the input number into b, and then use strcpy to copy this number to replace the original password, what is more, call showupdate_userdata() and borrower_function(). In addition, if i is equal to or bigger than the defined number of users, prompt user his original password is wrong.
l For case 4, call the mainmenu() function and then break this switch statement.
14. void loan_list(int id, char* name) function:
l Declare the function into has no return value but has two parameters, which are int type id, and char type pointer name respectively.
l Declare necessary letter into int type, and a FILE type pointer filemn.
l Use a for loop with initial a is equal to zero and if a less than the defined number of books the a will plus 1 to itself.
l Add a nested while loop to this for loop with condition of x is less than the defined number of users, and print a blank space.
14
l Use the if statement to check the ID if so, program will execute the following statements, displays a 2, and the defined 'no' plus 1 to itself, and connect the pointer filemn to open_singlebook() function, and then execute the open_singlebook() function.
l Add a while loop to the last while loop with condition b is less than the defined singlebook_amount, and use a if statement to check the name in the struct does it is equal to zero ,if so, print the title, due_year, due_month, and due_day on screen respectively, or else let b plus 1 to itself, and then finish the last while loop.
l Let x plus 1 to itself, in which the second while loop is still going on.
l In the other conditions of the first if statement, x also plus 1 to itself. After that, finish the second nested while loop and first for loop.
l Use if statement to check the 'no', if it is equal to zero, print some sentences to prompt user who have not borrowed any book.
l If the value of 'no' is not equal to zero, print the menu to user and gain the order from keyboard by scanf statement and store it in y.
l Use if statement to check the value of y, if it is equal to 1, call the renew() function, or else, call the borrower_function() function. After that, finish the if statement.
l Finally, call the borrower_function() function.
15. FILE* open_singlebook (int number) function:
l Declare the function into has FILE type return value and with parameter number in int type.
l Declare a FILE type of pointer filemn, and book_no into char type with 15 letters, additionally, declare the first and last with not limited, and equal to 'book' and '.txt' respectively, finally, declare a pointer named file_name.
l Call the integerstring_conversion() function.
l Give the file name beginning with 'first' and 'book_no' in series, and the file name end with 'file_name' and 'last'.
l Connect the pointer filemn to file_read() function with parameter of file_name.
l Return the value of filemn to the function.
16. FILE* write_singlebook (int number) function:
l Declare the function into has FILE type return value and with parameter number in int type.
l Declare a FILE type of pointer filemn, and book_no into char type with 15 letters, additionally, declare the first and last with not limited, and equal to 'book' and '.txt' respectively, finally, declare a pointer named file_name.
l Call the integerstring_conversion() function.
15
l Give the file name beginning with 'first' and 'book_no' in series, and the file name end with 'file_name' and 'last'.
l Connect the pointer filemn to file_write() function with parameter of file_name.
l Return the value of filemn to the function.
17. char *integerstring_conversion(char *string, int n) function:
l Declare this function into char type return value and has two parameter which are char type pointer string and int type n.
l Declare necessary letter into int type.
l Use if statement to judge whether n could be divide by 10 with no remainder and has result 0, if not, make the string is equal to this function with parameters string and the divided result. If the if statement is false do not execute this statement.
l Make the value which points to the next letter in this string equals to the remainder that gain from n divide by 10.
l Make value of pointer string is equal to \0, which means to initialize the string.
l Return the value of string to the function.
18. void show_singlebook(int no) function:
l Declare necessary elements in this function, which consist of a FILE type pointer, three int type in that one has initial value 0, and two char type.
l Make the pointer filemn connecting to open_singlebook() function with parameter 'no'.
l Define the 'line' is equal to the line_number function with parameter 'filemn' and minus 2.
l Make the singlebook is equal to the data that come from struct type borrowingdata, and then distribute the memory to line according to the size of borrowing data.
l Gain the 1499 bytes of data from the file that point by filemn and store in str.
l Use for loop to circularly operate each line.
l Use fscanf to assign the value that write in the filemn to singlebook[]barcode term that is No. a line in singlebook .
l Set a while loop that with condition if read a space letter in filemn. If so, use fseek to locate the input location, move to the last one cursor from the end.
l Use the for loop to repeatedly gain the value of character that is in the filemn, with condition when the reading process get a character that is not space character.
l After that set a \0 to the loan type for 'b' in the end.
l Locating the cursor to the last one character from the end again.
16
l Set a while loop that with condition if read a space letter in filemn. If so, use fseek to locate the input location, move to the last one cursor from the end.
l Use the for loop to repeatedly gain the value of character that is in the filemn, with condition when the reading process get a character that is not space character.
l After that set a \0 to the loan type for 'b' in the end.
l Locating the cursor to the last one character from the end again.
l Use a while loop to check the character that is not space character, if so locate the cursor to the last one character from the end.
l Gain the data from filemn and assign the data to lastborrowed_day, lastborrowed_month and lastborrowed_year of No.a term in singlebook respectively.
l Use a while loop to check the space character, if so locate the cursor to the last one character from the end.
l Gain the data from filemn and assign the data to due_day, due_month and due_year of No.a term in singlebook respectively.
l Use a while loop to check the space character, if so locate the cursor the last one character from the end.
l Set a for loop to gain the value of 'details' with condition that if there is not a '\n'.
l Set a '\0' at the end of the 'details' string. And finish the first for loop here.
l Assign the value of line to singlebook_amout.
l Close the corresponding file.
19. void renew(void) function:
l Declare this function into no return value and has no parameters.
l Declare necessary elements in this function, it consist of a file type pointer filemn, and two int type elements.
l Use printf statement to prompt user input the book's number and book's barcode.
l After that use two scanf statements to gain the value from keyboard and store it in corresponding element respectively.
l Call the alteringtime() function with the two input data as parameters.
20. void admin (int id, char *name) function:
l Declare this function into void type which means it has not return value and has two parameters int id and char *name. To connect each user.
17
l Declare one int type parameter in this function, which would be used in the following statements.
l Use some printf statements to display the menu to user, which could prompt choose the corresponding choice.
l Use a scanf statement to gain the input from keyboard and store it in the declared parameter.
l Use a switch statement to the parameter a.
l For case 1, call the borrowmenu() function with parameters id and name, and then break out of this switch statement.
l For case 2, call the booketurn() function with parameters id and name, and then break out of this switch statement.
l For case 3, call the display_available() function with parameters id and name, and then break out of this switch statement.
l For case 4, call the display_borrowed() function with parameters id and name, and then break out of this switch statement.
l For case 5, call the display_borrower() function with parameters id and name, and then break out of this switch statement.
l For case 6, call the display_users() function with parameters id and name, and then break out of this switch statement.
l For case 7, call the privilege_changing() function with parameters id and name, and then break out of this switch statement.
l For case 8, call the mark_details() function with parameters id and name, and then break out of this switch statement.
l For case 9, call the borrower_function() function with parameters id and name, and then break out of this switch statement.
l For case 10, call the mainmenu() function and then break out of this switch statement.
l For the default case, call the wrong_option_sentence() function and then break out of this statement.
l Finish this function.
21. void borrowmenu(int id, char*name) function:
l Declare this function into void type which means it has no return value and has two parameters which are int type id and char type pointer name.
l Use some printf statement to display the menu and prompts on screen.
l Use scanf statement to gain the input order from keyboard, and store it in declared parameter.
18
l Use if statement to judge the input, if the input is not 1 or 2 prompt user to input the correct order, and use scanf statement again.
l Use switch statement to judge the value of a.
l For case 1, call the borrow_function() function and then exit this switch statement.
l For case 2, execute the printf statement to prompt use already quit this borrow system, and then quit this switch statement.
l For default case, call the borrower_function() function with parameter id and name.
22. void borrow_function (void) function :
l -‐1. Declare five variables of the type of integer namely a, b, c, d and e. l -‐2. Declare four variables of the type of integer namely ID, barcode, no and tot. l -‐3. Declare two variables of the type of character namely name and ISBN. l -‐4. Declare 12 variables of the type of short integer namely year, month and day; year1,
month1 and day1; year2, month2 and day2: year3, month3 and day3. l -‐5. Ask user to enter ID, read and store the address of ID. And write a if-‐else statement to
determine whether the use has been registered. l -‐6. Write an if-‐else statement to determine whether the user has the privilege to borrow
book there or just search books. l -‐7. If user can borrow books, user is asked to enter the ISBN, book number and barcode of
the book. l -‐8. Write an if-‐else statement to determine the book which user wants to borrow is
available. l -‐9. Available copies of the book-‐1, and assign “on-‐loan” to collection. l -‐10. Store the update-‐ information of the user to the collection. l -‐11. Use pointers to divide users to have two different due time of the book. l -‐12. If the user has borrowed the book successfully, the system will display “you have
succeeded borrowing a book”.
23. void bookreturn(int, char*) function:
l -‐1. Declare one pointer of the type of file namely filenm. l -‐2. Declare five variables of the type of integer namely a, b, book_no (book number), bc
(book barcode) and ID. l -‐3. Ask users to enter their ID, book number and book barcode and store all of them. l -‐4. For the borrower, the number of the book he or she borrowed should be zero again. l -‐5. Store the return time. The due time to this specific book come back to zero all again. l -‐6. The number of copies of this book increases 1. l -‐7. The borrower ID[b] + 1, thus the user can borrow other books. l -‐8. In other cases, the system will display “Error occurred”.
24. void display_available (int,char*) function:
l -‐1. Declare a variable of the type of integer namely a.
19
l -‐2. If the book is available, add the title of the book to the list. l -‐3. Use a for loop to list all the available books.
25. void display_borrowed (int,char*) function:
l -‐1. Declare a variable of the type of integer namely a. l -‐2. If the available copy of the book is not zero, add the title of the book to the borrowed
list. l -‐3. Use a for loop to list all the borrowed books.
26. void display_borrower (int,char*) function:
l -‐1. Declare a pointer of the type of file namely filenm. l -‐2. Declare three variables of the type of integer namely a, b, and c, and initialize a, b, and c
to 0 at first. l -‐3. Assign the number of book that borrower borrowed to c. l -‐4. If the borrower borrowed books, list the borrower and the titles of the books. l -‐5. Use for loop to list all the books that borrower borrowed.
27. void display_users (int,char*) function:
l -‐1. Declare two variable of the type of integer namely a and b. l -‐2. Ask user to enter ID and read and store the value of it into the address of b. l -‐3. Display the information of the user: full name, ID and member’s privilege. l -‐4. Use a for loop to list all the members’ information .
28. void privilege_changing (int,char*) function:
l -‐1. Declare two variable of the type of integer namely a and b, and set the initial value of a to be 0.
l -‐2. Declare two variable of the type of integer namely new_prv and line. l -‐3. Ask user which use’s privilege does him or her wants to change and input their ID. l -‐4. Read and store the value of the input into the address of b. l -‐5. Assign new privilege to user. l -‐6. If all the cases do not occur, the system will point out error.
29. void mark_details (int,char*) function:
l -‐1. Declare a pointer of the type of file namely filenm. l -‐2. Declare two variables of the type of integer namely a and b, and initialize a to 0 first. l -‐3. Declare a variable of the type of integer namely no (number). l -‐4. Declare two strings of the type of character namely title and markbook and control the
length of the characters to 50-‐byte. l -‐5. Ask user to enter the number, barcode and other detail of the book he or she wants to
mark. l -‐6. Read and store the value of the input into the address of no (number), b and maekbook
individually.
20
l -‐7. Assign markbook to singlebook[a].details, and update the detail information one by one by using an if-‐else statement.
l -‐8. If the input is not variable to the function, then system will display “Error occurred”.
30. void booksearch (void) function:
l -‐1. Declare a variable of type of integer namely a. l -‐2. Ask user to choose 1-‐3: 1.Subject; 2.Author; 3.Title to search a certain book. l -‐3. Read and store the value of input to into the address of a. l -‐4. Use switch statement to choose the operation depending on the value of a: Case1: search by entering the subject Case2: search by the author’s name Case3: search by the title. l -‐5. If all the cases are not available, then the default will be wrong_option_sentence.
31. void subjectsearch (void) function:
l Declare an array sub of char type containing 50 elements l Declare the variables a and b of int type l Ask user to enter the book’s subject and read and store the subject l Using the while loop to check whether the input subject is too short l Using for loop to display the books of the subject
32. void booklist(int) function:
l Declare the variables a, b, booklabel and page of int type l Using if and else statement to check the whether users can find the book l Using while loop to display the details of book found including book label, title, author and
subject l Using switch and cases to provide options for users, the first one is to see the details of a
particular book and enter a book label, the second one is to move to another page and enter a particular book page, the third choice is to go back to the main menu, and then the wrong options
33. void search_singlebook(int) function:
l Declare the variables number, a of int type l Using for loop to show the information of a single book including book barcode, loan type,
loan type, loan status, the time last borrowed( day, month and year), due date( day, month and year), the details of the book and its borrower
l Go back to the main menu
34. void authorsearch (void) function:
l Declare an array aut of char type contains 50 elements l Declare the variable a and b of int type, and initialize b=0
21
l Ask user to enter the book’s author l Using while loop check whether the input name is correct, and if it is wrong type it again l Using for loop to show the findings of the author
35. void titlesearch (void) function:
l Declare an array tit of char type contains 100 elements l Declare the variables a and b of int type l Ask user to enter the book’s title l Using while loop to check whether the input title is correct l Using for loop to display the findings of the title
36. void alteringtime(int, int) function:
l Declare the variable book_no, barcode of the type of int l Declare the filenm of the type of FILE using pointer l Declare the variable a of int type and initialize a=0 l Declare the pointer time_tand give the values to b l Using strcut to declare the the variables tm, time1 and time 2 l Declare the variable sec of in type l Open the file book_no l Using if and else statement and while loop to state the privileges l Using pointers to get the values of time1 and time2 in tm_mday, tm_mon and tm_year l State the due date l Update the the information of a single book l Close the file
37. void update_singlebook (int) function:
l Declare the variable number and a of type int l Declare the filename is write_singlebook l Print the data to the file write_singlebook in a fixed form l Using for loop to print the data to the file write_singlebook l Close the file
38. void show_bookinfo (void) function:
l Declare the filenm of the type of FILE using pointer l Declare the variables line, a, b, of the int type and initialize a=0 l Declare the variable c and an array str contains 150 elements of char type l Read the file booklist.txt l Create a space of size for collection and displaybook, and then convert root to
structbookdetails using pointer l Read 1500-‐1 elements from booklist.txt and store the elements
22
l Using for loop to output the file to collection[a] l Using while loop and fseek function to change the pointer to one byte before the beginning l Using for loop to read characters from file title l Place the pointer at one byte before the beginning l Using while loop and fseek function to change the pointer to one byte before the beginning l Using for loop to read characters from file author and fseek function to place the pointer to
one byte before the beginning l Using while loop and fseek function to change the pointer to one byte before the beginning l Using for loop to read characters from file shelfmark and fseek function to place the
pointer to one byte before the beginning l Using while loop and fseek function to change the pointer to one byte before the beginning l Using for loop to read characters from file ISBN and fseek function to place the pointer to
one byte before the beginning l Using while loop and fseek function to change the pointer to one byte before the beginning l Using for loop to read characters from file subject and fseek function to place the pointer
to one byte before the beginning l Using while loop and fseek, fscanf functions to output available copies l Using while loop and fseek, fscanf functions to output total copies l Using while loop and fseek function to change the pointer to one byte before the beginning l Using if and else statement to check the place of the pointer l Using for while loop and if statement to place the pointer to one byte before the
beginning, and output the data to borrowerID l Close the file
39. void update_bookinfo (void) function: l Declare the filenm of the type of FILE using pointer l Declare the variables a and b of int type l Write in the file booklist.txt l Output the data book number, title, author, shelf mark, ISBN, subject, available copies,
total copies, availability and borrower ID l Using for loop to output the data l Output booknumber l Output title l Output author l Output shelfmark l Output ISBN l Output subject l Output available_copies l Output total_copies l Output availability l Using for loop and if statement to output borrower ID l Output change a new line l Close the file
40. void exit_program(void) function:
l Display the expression Thanks for your visit, please come again next time
23
l Exit the program
4. Implementation
See the C code with comments in the Appendix at the end of the report
5. Testing
If the program is run:
Main menu
Continued on the next page ..
24
1. Register
The information of users saved in userlist.txt
Try again until
25
2. Log in
Student account log in:
26
Guests account log in:
Teacher account log in:
Admin account log in:
27
If you input wrong password:
Test function in Admin account:
Borrow and return a book. There is a problem here. The program will stop here.
28
Return a book.
See the available book list
See the borrowed book list
See the borrower list
(Have problem)
See the members’ information
(Have problem)
29
Change users privilege
Mark book detail
Go back to my account
30
See books you have borrowed
Renew a book (has a bug)
Change password
(password was changed)
Continued on the next page . .
31
3. Search books
By subject
32
Function 1 doesn’t work
Function 2:
33
By author
By title
34
4. Exit the program:
35
Appendix
1. C Source code (the file can be found in the zipped file which is the Assignment8.c )
/*
Name: A Program for Creating a Library Rental System.
File Name: Assignment8.c
Copyright: Free
Author: Group 2
Description: This program is the ABC Town's library rental system which its user can log in, register, borrow, and search books
*/
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
#include<time.h>
#define DATABASE_PAGES 5
/* declare a number of books per page, which is 5 */
struct bookdetails /* declare a structure type namely bookdetails, which contains some information about books*/
{
int booknumber;
char author[100];
char title[500];
char ISBN[100];
char subject[100];
char shelfmark[50];
int total_copies;
int available_copies;
char availability[50];
int borrowerID[50];
};
36
struct borrowers /* declare a structure type namely borrowers, which contains some information about borrowers*/
{
char surname[50];
char givenname[50];
char fullname[100];
int ID;
char password[8];
int userprivilege;
};
struct borrowingdata /* declare a structure type namely borrowingdata, which contains some data about borrowing*/
{
int barcode;
char loantype[50];
char loanstatus[50];
int lastborrowed_day;
int lastborrowed_month;
int lastborrowed_year;
int due_day;
int due_month;
int due_year;
char details[20];
char borrower[20];
};
char filename[100]; /* declare a char-type variable filename (array), and four integer-type variables as can be seen on the left */
int number_of_books;
int number_of_users;
int singlebook_amount;
int privilege;
void mainmenu(void); /* declare the 40 functions (including the main function) written in this program */
void wrong_option_sentence(void);
37
void registration (void);
void show_userdata (void);
int line_number (FILE *filenm);
void showupdate_userdata (void);
FILE* file_read (char*);
FILE* file_write (char*);
FILE* file_append (char*);
void file_close (FILE *filenm);
void login (void);
void borrower_function (int, char*);
void loan_list (int, char*);
FILE* open_singlebook (int);
FILE* write_singlebook (int);
char* integerstring_conversion (char*, int);
void show_singlebook (int);
void renew (void);
void admin (int,char*);
void borrowmenu (int, char*);
void borrow_function (void);
void bookreturn(int, char*);
void display_available (int,char*);
void display_borrowed (int,char*);
void display_borrower (int,char*);
void display_users (int,char*);
void privilege_changing (int,char*);
void mark_details (int,char*);
void booksearch (void);
void subjectsearch (void);
void booklist(int);
void search_singlebook(int);
void authorsearch (void);
void titlesearch (void);
void alteringtime(int, int) ;
void update_singlebook (int);
38
void show_bookinfo (void);
void update_bookinfo (void);
void exit_program(void);
struct bookdetails *collection; /*Declare a pointer of the type of structure bookdetails namely collection*/
struct bookdetails **displaybook; /*Declare a pointer of the type of structure bookdetails namely displaybook, which point to the pointer variable that point to struct bookdetails */
struct borrowers *users; /*Declare a pointer of the type of structure users namely collection*/
struct borrowingdata *singlebook; /*Declare a pointer of the type of structure singlebook namely collection*/
int main() /*this is the main program to perform the functions*/
{
show_bookinfo();/* implement the function show_bookinfo()*/
show_userdata();/* implement the function show_userdata()*/
mainmenu();/* implement the function mainmenu()*/
return 0;
}
void mainmenu(void) /* a function to display the main menu */
{
int a; /* declare one variable of the type of integer namely a*/
printf("\n*********************************************\n");
printf("\nThis is the ABC Town's Library Rental System.\nPlease register first before you log in.\n");
while(1)
{ /* display the main menu */
printf("\n*********************************************\n");
printf(" Main Menu ");
printf("\n*********************************************\n\n");
printf("1. Register \n");
printf("2. Log in to My Account\n");
39
printf("3. Search books you want to find \n");
printf("4. Exit the program \n");
printf("\n*********************************************\n\n");
printf("To choose one of the options above, please choose 1 - 4: ");/* ask user to choose*/
scanf("%d",&a); /* read the value and store it into variable a */
switch (a) /* use switch statement to choose the operation depending on the value of a */
{
case 1 : registration(); /* case=1, implement the function registration() */
break;
case 2 : login(); /* case=2, implement the function login() */
break;
case 3 : booksearch(); /* case=3, implement the function booksearch() */
break;
case 4 : exit_program(); /* case=4, implement the function exit_program() */
default : wrong_option_sentence(); /* the above cases do not occur, implement the function exit_program() */
}
}
}
void wrong_option_sentence(void) /* a function is be implemented when the wrong option occur */
{
printf("\n\nPlease try again, the data you input was wrong.\n");/* ask users to try again */
getchar();
}
void registration(void) /* a function to register */
{
char pw , passw[8];/* declare two variables of the type of character namely pw and passw*/
int a = 0;/* declare one variable of the type of integer namely a, and initialize it equal to 0*/
40
int b = 0;/* declare one variable of the type of integer namely b, and initialize it equal to 0*/
show_userdata();/* implement the function show_userdata()*/
while( a == 0) /* while the user has not ever registered before */
{
printf("\n\n*********************************************\n");
printf(" Registration");
printf("\n*********************************************\n\n");
printf("Please enter your ID number (8 digits):\n");/* ask user to enter ID number */
scanf("%d", &users[number_of_users].ID);/* read the value and store it into users[number_of_users].ID*/
if((users[number_of_users].ID > 99999999))/* when the inputted number is more than 99999999, ask user to input again*/
{
printf("Your ID number should be within 8 digits.");/*ask user*/
a = 0;
}
else a = 1;
}
printf("\nPlease enter your first (given) name:\n");/* input the first name*/
scanf("%s", users[number_of_users].givenname);/* read the value and store it into users[number_of_users].givenname*/
printf("\nPlease enter your last name:\n");/* input the last name*/
scanf("%s", users[number_of_users].surname);/* read the value and store it into users[number_of_users].surname*/
printf("\nCreate your own password:(less than 8 characters)\n");/* input the new password*/
b = 0;
while ((pw = getch())!='\r') /* use getch to get a character and assign it to pw */
{
if ( b <8 && isprint(pw)) /* insert a if-else statement to judge operation isprint(pw) and b<8
41
as the test condition.*/
{
passw[b] = pw; /* assign pw to passw */
putchar('*'); /* use * to dispaly on the screen */
b++;
}
else if ( b > 0 && pw == '\b')/* insert a if-else statement to judge operation pw == '\b' and b>0 */
{
--b;
putchar('\b');/* back a space*/
putchar(' ');
putchar('\b');/* back a space*/
}
}
passw[b]='\0';/* assign NULL to passw */
for( b = 0; b<8; b++)
{
users[number_of_users].password[b] = passw[b];/* assign passw to users[number_of_users].password */
}
printf("\n\nYour status is: \n0. Guest\n1. Student\n2. Teacher\n3. Admin\n");/* ask user to choose their status */
printf("\nPlease enter a number between 0 - 3 from the options above: ");
scanf("%d", &users[number_of_users].userprivilege); /* read the value and store it into the addres of users[number_of_users].userprivilege*/
strcpy(users[number_of_users].fullname, users[number_of_users].surname);/* assign users[number_of_users].surname to number_of_users].fullname*/
strcat(users[number_of_users].fullname, users[number_of_users].givenname);/* assign users[number_of_users].givenname to number_of_users].fullname*/
number_of_users += 1;
showupdate_userdata();/* implement the function showupdate_userdata()*/
printf("\nRegistration success! Now you have been registered in this library.\n");
mainmenu();/* implement the function mainmenu()*/
42
}
void show_userdata(void)/* a function to store the data of user */
{
FILE *filenm;/* declare one pointer of the type of file namely filenm */
int line;/* declare one variable of the type of integer namely line*/
int a = 0;/* declare one variable of the type of integer namely a, and initialize it equal to 0*/
int b;/* declare one variable of the type of integer namely b*/
char c;/* declare one variable of the type of character namely c*/
char strarray[1500];/* declare one variable of the type of character namely strarray*/
filenm = file_read ("userlist.txt");/* assign file_read to filenm */
line = line_number (filenm);/* assign line_number (filenm) to line */
users = (struct borrowers *) malloc( (line+1) * sizeof(struct borrowers)); /* assign space to users */
for (a = 0; a < line; a++)
{
fscanf(filenm,"%d",&users[a].ID);/*output the value of users[a].ID into the file which is pointed by filenm*/
while( isspace(fgetc(filenm)) );/* fix the position for filenm renewedly */
fseek(filenm,-1L,SEEK_CUR); /* using fseek to move the any file position in filenm by the offset -1L from the current position */
for(b = 0; (c=fgetc(filenm))!='\t' ; b++)
users[a].surname[b] = c;/* assign c to users[a].surname[b] */
users[a].surname[b] = '\0';/* assign '\0' to users[a].surname[b]*/
while( isspace(fgetc(filenm)) );/* fix the position for filenm renewedly */
fseek(filenm,-1L,SEEK_CUR); /* using fseek to move the any file position in filenm by the offset -1L from the current position */
for(b = 0; (c = fgetc(filenm))!='\t' ;b++)
users[a].givenname[b] = c;/* assign c to users[a].givenname[b] */
users[a].givenname[b] = '\0';/* assign '\0' to users[a].givenname[b] */
strcpy(strarray,users[a].surname);/* assign users[a].surname to strarray*/
43
strcat(strarray," ");/* assign " " to strarray*/
strcpy(users[a].fullname ,strcat(strarray,users[a].givenname));/* assign strarray to users[a].fullname */
while( isspace(fgetc(filenm)) );/* fix the position for filenm renewedly */
fseek(filenm,-1L,SEEK_CUR); /* using fseek to move the any file position in filenm by the offset -1L from the current position */
for(b = 0; (c=fgetc(filenm))!='\t' ; b++)
users[a].password[b] = c;/* assign c to users[a].password[b] */
users[a].password[b] = '\0';/* assign '\0' to users[a].password[b] */
while( isspace(fgetc(filenm)) ); /* fix the position for filenm renewedly */
fseek(filenm,-1L,SEEK_CUR); /* using fseek to move the any file position in filenm by the offset -1L from the current position */
fscanf(filenm,"%d",&users[a].userprivilege);
/*output the value of users[a].userprivilege into the file which is pointed by filenm*/
}
number_of_users = line;/* assign line to number_of_users */
file_close(filenm); /* close the file*/
return;
}
int line_number(FILE *filenm) /* a function to add the number of line*/
{
char a;/* declare one variable of the type of integer namely a*/
int l = 0;/* declare one variable of the type of integer namely l, and initialize it equal to 0*/
while( (a = fgetc(filenm)) != EOF)
if (a == '\n') l++; /* when the a ='\n', the value of l plus one.*/
rewind(filenm);/* let filenm point to the outset again*/
return l;
}
void showupdate_userdata (void)/* a function to store the data of user */
{
44
FILE *filenm;/* declare one pointer of the type of file namely filenm */
int a;/* declare one variable of the type of integer namely a*/
filenm = file_write ("userlist.txt");/* assign file_read to filenm */
for (a = 0; a < number_of_users; a++)
{ /*input the data of user to the file */
fprintf(filenm,"%d\t%s\t%s\t%s\t%d\n",users[a].ID,users[a].givenname,users[a].surname,users[a].password,users[a].userprivilege);
}
file_close(filenm);/* close the file*/
}
FILE* file_read ( char *afile)/* a function to read the file */
{
FILE *filenm;/* declare one pointer of the type of file namely filenm */
strcpy(filename,afile);/* assign afile to filename */
filenm = fopen(filename,"r");/*open and read the file of filename and assign it to filenm*/
if(filenm == NULL) /* when the file is empty, ask user */
{
printf("\nThe file cannot be opened: %s !\n", filename);
exit(1);
}
return filenm;
}
FILE* file_write (char *afile)/* a function to write the file */
{
FILE *filenm;/* declare one pointer of the type of file namely filenm */
strcpy(filename,afile);/* assign afile to filename */
filenm = fopen(filename,"w");/*open and write the file of filename and assign it to filenm*/
if(filenm == NULL)/* when the file is empty, ask user */
{
printf("\nThe file cannot be opened: %s !\n", filename);
45
system("pause");
exit(1);
}
return filenm;
}
FILE* file_append (char *afile)/* a function to append the file */
{
FILE *filenm;/* declare one pointer of the type of file namely filenm */
strcpy(filename,afile);/* assign afile to filename */
filenm = fopen(filename,"a");/*open and append the file of filename and assign it to filenm*/
if(filenm == NULL)/* when the file is empty, ask user */
{
printf("\nThe file cannot be opened: %s !\n", filename);
system("pause");
exit(1);
}
return filenm;
}
void file_close(FILE *filenm)/* a function to close the file */
{
if (fclose(filenm)!=0)/* when the file that has been clodsed does not return 0, ask user */
printf("The file cannot be closed: %s!\n",filename);
}
void login (void)/* a function to log in */
{
int a , b = 0;/* declare two variables of the type of integer namely a and b,and initialize b equal to 0*/
char y[100];/* declare one variable of the type of character namely y*/
char x[100];/* declare one variable of the type of character namely z*/
char z[100];/* declare one variable of the type of character namely x*/
46
printf("\n\n*********************************************\n");
printf(" Log In");
printf("\n*********************************************\n\n");
printf("\nPlease enter your first (given) name:\n");/* input the first name*/
scanf("%s",y); /*read and store the value into y*/
printf("\nPlease enter your surname:\n");/* input the surname name*/
scanf("%s",x); /*read and store the value into x*/
printf("\nPlease enter your ID number:\n");/* input the ID number*/
scanf("%d",&a); /*read and store the value into the address of a*/
printf("\nPlease enter your password:\n");/* input the password*/
scanf("%s",z); /*read and store the value into z*/
while(b < number_of_users)
{
if(strcmp(users[b].surname,x) == 0 && strcmp(users[b].givenname,y) == 0 && a == users[b].ID && strcmp(users[b].password,z) == 0)
{/*compare the inputted data with the data in the struct*/
if((users[b].userprivilege == 1)||(users[b].userprivilege == 2))/* when the user is student or teacher*/
{
borrower_function(a,x);/* implement the function borrower_function(a,x)*/
}
else
{
if(users[b].userprivilege == 3)/* when the user is admin*/
{
admin(a,users[b].fullname);/* implement the function admin(a,users[b].fullname)*/
}
else /* when the user is admin*/
{
printf("\nYou can only search the book because you are guest only.\n");
mainmenu();/* implement the function mainmenu()*/
exit_program();/* implement the function exit_program()*/
}
47
}
}
else{b++;}
}
printf("\nPerhaps the data you entered was wrong or you haven't registered yet. Please try again.\n");
mainmenu();/* implement the function mainmenu()*/
}
void borrower_function (int id , char* name)/* a function to display the information of borrower */
{
char b[8];/* declare one variable of the type of character namely b*/
int a;/* declare one variable of the type of integer namely a*/
int i = 0;/* declare one variable of the type of integer namely i, and initialize it equal to 0 */
printf("\n\n*********************************************\n");/*menu*/
printf(" My Account");
printf("\n*********************************************\n\n");
printf("\n1. See books you have borrowed\n2. Renew a book \n3. Change your password\n4. Return to the main menu\n");
printf("\nTo choose one of the options above, please choose 1 - 4: ");/*ask user to choose*/
scanf("%d",&a);/* read and store the inputted value into the address of a*/
switch(a) /* use switch statement to choose the operation depending on the value of a */
{
case 1: loan_list(id , name); break;/* case=1, implement the function loan_list(id , name) */
case 2: renew(); break;/* case=2, implement the function renew()*/
case 3: while(i < number_of_users)
{
if(id == users[i].ID)/* the original password is ritht*/
{
printf("Please enter your new password:");/*ask user to input password*/
scanf("%s", b); /* read and store the inputted value into b*/
48
strcpy(users[i].password, b);/* assign b to users[i].password*/
printf("\nYour password has been changed!\n");
showupdate_userdata();/*implement the function showupdate_userdata()*/
borrower_function(id , name);/*implement the function borrower_function(id , name)*/
}
else{i++;}
}
printf("\nYour original password is wrong.\n");
break;
case 4: mainmenu(); break;/* case=4, implement the function mainmenu()*/
}
}
void loan_list(int id,char* name)/* a function to display the list of loan */
{
FILE *filenm;/* declare one pointer of the type of file namely filenm */
int a = 0;/* declare one variable of the type of integer namely a, and initialize it equal to 0 */
int b = 0;/* declare one variable of the type of integer namely b, and initialize it equal to 0 */
int x = 0;/* declare one variable of the type of integer namely x, and initialize it equal to 0 */
int y;/* declare one variable of the type of integer namely y*/
int no = 0;/* declare one variable of the type of integer namely no, and initialize it equal to 0 */
for (a = 0; a < number_of_books; a++)
{
while (x < number_of_users)
{
printf(" "); /*output 2*/
if (collection[a].borrowerID[x] == id)/* the id is coincident*/
{
printf("2"); /*output 2*/
49
no++;
filenm = open_singlebook(collection[a].booknumber);/* assign open_singlebook(collection[a].booknumber) to filenm*/
show_singlebook(collection[a].booknumber);/*implement the function show_singlebook(collection[a].booknumber)*/
while (b < singlebook_amount)
{
if (strcmp(singlebook[b].borrower,name) == 0)/* the name is coincident*/
{
printf("%s\t%d\t %d-%d-%d\n",collection[a].title,singlebook[b].due_year,singlebook[b].due_month,singlebook[b].due_day);
}/*display the information of loan*/
else{b++;}
}
x++;
}
else{x++;}
}
}
if (no == 0)/* the user nevers borrow books*/
{
printf("\nYou haven't borrowed any book!\n");
}
else
{ /* the menu of renewing*/
printf("Do you want to renew your book?\n");
printf("\n1. Yes\n2. No\n\n");
printf("To choose the option above, please enter 1 or 2: ");/*ask user to choose*/
scanf("%d",&y);/* read and store the inputted value into y*/
if(y == 1)
{
renew();/*implement the function showupdate_userdata()*/
}
else
50
{
borrower_function(id,name);/*implement the function borrower_function(id,name)*/
}
}
borrower_function(id,name);/*implement the function borrower_function(id,name)*/
}
FILE* open_singlebook (int number)/* a function to open the file of single book*/
{
FILE *filenm;/* declare one pointer of the type of file namely filenm */
char book_no[15];/* declare one variable of the type of character namely book_no*/
char first[]="book";/* declare one variable of the type of character namely first,assign "book" to it*/
char last[]=".txt";/* declare one variable of the type of character namely last,assign ".txt" to it*/
char *file_name;/* declare one pointer of the type of character namely file_name*/
integerstring_conversion(book_no,number);/* implement the function integerstring_conversion(book_no,number)*/
file_name = strcat(first,book_no);/*assign book_no to first, then assign it to file_name*/
file_name = strcat(file_name,last);/*assign book_no to last, then assign it to file_name*/
filenm = file_read (file_name);/*assign file_read (file_name) to filenm*/
return filenm;
}
FILE* write_singlebook (int number)/* a function to write the file of single book*/
{
FILE *filenm;/* declare one pointer of the type of file namely filenm */
char book_no[15];/* declare one variable of the type of character namely book_no*/
char first[]="book";/* declare one variable of the type of character namely first, assign "book" to it*/
char last[]=".txt";/* declare one variable of the type of character namely last, assign ".txt" to it*/
51
char *file_name;/* declare one pointer of the type of character namely file_name*/
integerstring_conversion(book_no,number);/* implement the function integerstring_conversion(book_no,number)*/
file_name = strcat(first,book_no);/*assign book_no to first, then assign it to file_name*/
file_name = strcat(file_name,last);/*assign book_no to last, then assign it to file_name*/
filenm = file_write (file_name);/*assign file_read (file_name) to filenm*/
return filenm;
}
char * integerstring_conversion(char *string, int n)/* a function to convert the string*/
{
int a;/* declare one variable of the type of integer namely a*/
if((a = n/10) != 0) string = integerstring_conversion(string,a);
*(string++) = (n%10+'0'); /*convert the number into integer*/
*string = '\0';
return string;
}
void show_singlebook (int no)/* a function to store the data of single book*/
{
FILE *filenm;/* declare one pointer of the type of file namely filenm */
int line;/* declare one variable of the type of integer namely line*/
int a = 0;/* declare one variable of the type of integer namely a, and initialize it equal to 0*/
int b;/* declare one variable of the type of integer namely b*/
char str[1500];/* declare one string of the type of character namely str*/
char w;/* declare one variable of the type of character namely w*/
filenm = open_singlebook (no);/* assign open_singlebook (no) to filenm */
line = line_number (filenm);/* assign line_number (filenm) to line */
line = line-2;/* assign line-2 to line */
singlebook = (struct borrowingdata *) malloc( line * sizeof(struct borrowingdata));/* assign space to singlebook */
fgets(str,1500,filenm);/*read filenm and store into str*/
52
for (a = 0; a < line; a++)
{
fscanf(filenm,"%d",&singlebook[a].barcode);/*output the value of singlebook[a].barcode into the file which is pointed by filenm*/
while( isspace(fgetc(filenm)) );
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
for(b = 0; !isspace(w = fgetc(filenm)) ; b++)
singlebook[a].loantype[b] = w;/* assign w to singlebook[a].loantype[b] */
singlebook[a].loantype[b] = '\0';/* assign '\0' to singlebook[a].loantype[b] */
fseek(filenm,-1L,SEEK_CUR); /* fix the position for filenm renewedly */
while( isspace(fgetc(filenm)) );
fseek(filenm,-1L,SEEK_CUR); /* fix the position for filenm renewedly */
for(b=0; !isspace(w = fgetc(filenm)) ;b++)
singlebook[a].loanstatus[b] = w;/* assign w to singlebook[a].loanstatus[b] */
singlebook[a].loanstatus[b] = '\0';/* assign '\0' to singlebook[a].loanstatus[b] */
fseek(filenm,-1L,SEEK_CUR); /* fix the position for filenm renewedly */
while( isspace(fgetc(filenm)) );
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
fscanf(filenm,"%d/",&singlebook[a].lastborrowed_day);
/*output the value of singlebook[a].lastborrowed_day into the file which is pointed by filenm*/
fscanf(filenm,"%d/",&singlebook[a].lastborrowed_month);
/*output the value of singlebook[a].lastborrowed_month into the file which is pointed by filenm*/
fscanf(filenm,"%d",&singlebook[a].lastborrowed_year);
/*output the value of singlebook[a].lastborrowed_year into the file which is pointed by filenm*/
while( isspace(fgetc(filenm)) );
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
fscanf(filenm,"%d/",&singlebook[a].due_day);
/*output the value of singlebook[a].due_day into the file which is pointed by filenm*/
fscanf(filenm,"%d/",&singlebook[a].due_month);
53
/*output the value of singlebook[a].due_month into the file which is pointed by filenm*/
fscanf(filenm,"%d",&singlebook[a].due_year);
/*output the value of singlebook[a].due_year into the file which is pointed by filenm*/
while( isspace(fgetc(filenm)) );
fseek(filenm,-1L,SEEK_CUR); /* fix the position for filenm renewedly */
for(b = 0; (w = fgetc(filenm))!='\t' ; b++)
singlebook[a].borrower[b] = w;/* assign w to singlebook[a].borrower[b] */
singlebook[a].borrower[b] = '\0';/* assign'\0' to singlebook[a].borrower[b] */
while( isspace(fgetc(filenm)) );
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
for(b=0; (w = fgetc(filenm))!='\n' ;b++)
singlebook[a].details[b] = w;/* assign w to singlebook[a].details[b] */
singlebook[a].details[b] = '\0';/* assign'\0' to singlebook[a].details[b] */
}
singlebook_amount = line;/* assign line to singlebook_amount */
file_close(filenm);/* close the file*/
return;
}
void renew (void) /* a function to renew a book */
{
FILE *filenm;/* declare one pointer of the type of file namely filenm */
int barcode; /* declare one variable of the type of integer namely barcode*/
int book_no; /* declare one variable of the type of integer namely book_no*/
printf("\n\n*********************************************\n");/*menu*/
printf(" Renew a book");
printf("\n*********************************************\n\n");
printf("\nPlease enter the book's number: ");
scanf("%d",&book_no);/*read and store the value into the address of book_no*/
54
printf("\nPlease enter the book's barcode: ");
scanf("%d",&barcode);/*read and store the value into the address of barcode*/
alteringtime(book_no,barcode); /* implement the function alteringtime(book_no,barcode)*/
}
void admin (int id, char* name)/* a function for the operating of admin */
{
int a;/* declare one variable of the type of integer namely a*/
printf("\n\n*********************************************\n");/*menu*/
printf(" Admin Account");
printf("\n*********************************************\n\n");
printf("1. Borrow a book\n");
printf("2. Return a book\n");
printf("3. See the available book list\n");
printf("4. See the borrowed book list\n");
printf("5. See the borrower list\n");
printf("6. See member's information\n");
printf("7. Change user's privilege\n");
printf("8. Mark a book's detail.\n");
printf("9. Go back to my account\n");
printf("10.Return to main menu\n");
printf("\nTo choose one of the options above, please choose 1 - 10: ");
scanf("%d",&a);/*read and store the value into the address of a*/
switch(a)/* use switch statement to choose the operation depending on the value of a */
{
case 1 :borrowmenu(id,name);break;/* case=1, implement the function borrowmenu(id,name) */
case 2 :bookreturn(id,name);break;/* case=2, implement the function bookreturn(id,name) */
case 3 :display_available (id,name);break;/* case=3, implement the function display_available */
case 4 :display_borrowed (id,name);break;/* case=4, implement the function display_borrowed */
case 5 :display_borrower(id,name);break;/* case=5, implement the function display_borrower(id,name) */
case 6 :display_users(id,name);break;/* case=6, implement the function display_users(id,name) */
55
case 7 :privilege_changing (id,name);break;/* case=7, implement the function privilege_changing */
case 8 :mark_details(id,name);break;/* case=8, implement the function mark_details(id,name) */
case 9 :borrower_function(id,name);break;/* case=9, implement the function borrower_function(id,name) */
case 10 :mainmenu();break;/* case=10, implement the function mainmenu()*/
default :wrong_option_sentence();break;/* the above cases do not occur, implement the function wrong_option_sentence() */
}
}
void borrowmenu(int id,char* name)/* a function to display the borrow menu */
{
int a;/* declare one variable of the type of integer namely a*/
printf("\n\n*********************************************\n");/*menu*/
printf(" Borrow Menu");
printf("\n*********************************************\n\n");
printf("1. Borrow a book\n\n");
printf("2. Exit borrow menu\n");
printf("\nTo choose the option above, please enter 1-2:\n\n");
scanf("\n%d",&a);/*read and store the value into the address of a*/
if (a != 1 && a != 2)/* ask user must input 1 or 2*/
{
printf("You must input 1 or 2!");
scanf("\n%d",&a);/*read and store the value into the address of a*/
}
switch(a)
{
case 1:borrow_function();break;/* case=1, implement the function borrow_function() */
case 2:printf("Quit the borrow system");break;/* case=2, quit */
default :borrower_function(id,name);/* the above cases do not occur, implement the function borrower_function(id,name) */
}
}
56
void borrow_function(void)/* a function to borrow*/
{
time_t sec;/* the function to obtain time now*/
int a,b,c,d,e; /* declare five variables of the type of integer namely a,b,c,d and e*/
int ID;/* declare one variable of the type of integer namely ID*/
int barcode, no, tot;/* declare three variables of the type of integer namely barcode, no and tot*/
char name[50],ISBN[50]; /* declare two variables of the type of character namely name and ISBN*/
struct tm * ttime;
short year,month,day;/* declare three variables of the type of short integer namely year,month and day*/
short year1,month1,day1;/* declare three variables of the type of short integer namely year1,month1 and day1*/
short year2,month2,day2;/* declare three variables of the type of short integer namely year2,month2 and day2*/
short year3,month3,day3; /* declare three variables of the type of short integer namely year3,month3 and day3*/
tot = 0;/*initialize it equal to 0*/
printf("\n\n*********************************************\n");/*menu*/
printf(" Borrow Your Book");
printf("\n*********************************************\n\n");
printf("Please enter your ID number:\n");/*ask user to enter ID*/
scanf("%d",&ID);/*read and store the value into the address of ID*/
for(a = 0; a < number_of_users; a++)
{
if(ID!=users[a].ID)/* insert a if-else statement to judge operation ID!=users[a].ID as the test condition */
{tot++;/* assign tot+1 to tot */
if(tot==number_of_users)/* insert a if-else statement to judge operation tot==number_of_users as the test condition */
printf("\nSorry, you have not been registered.\n");}/*ask user*/
else if(ID==users[a].ID)/* insert a if-else statement to judge operation ID==users[a].ID as the test condition */
{strcpy(name,users[a].fullname);/* assign users[a].fullname to name */
if(users[a].userprivilege==0)/* insert a if-else statement to judge operation users[a].userprivilege==0 as the test condition */
57
printf("\nHi guest, you can just search books here.\n");
else if(users[a].userprivilege==1)/* insert a if-else statement to judge operation users[a].userprivilege==1 as the test condition */
{
printf("\nPlease enter the book's ISBN:\n");/*ask user to enter ISBN*/
scanf("%s",ISBN);/*read and store the value into the ISBN*/
for(e = 0; e < number_of_books; e++)
{
if(strcmp(ISBN,collection[e].ISBN)==0)/*insert a if-else statement to judge operation
strcmp(ISBN,collection[e].ISBN)==0 as the test condition*/
{
printf("\nThe book number is %d\n",collection[e].booknumber);/*output the book number*/
no = collection[e].booknumber;/* assign collection[e].booknumber to no */
b = e;/* assign e to b */
}
}
show_singlebook(no);/* implement the function show_singlebook(no)*/
printf("\nPlease enter the book's barcode:\n");/*ask user to enter barcode*/
scanf("%d",&barcode);/*read and store the value into the adress of barcode*/
for(c = 0; c < singlebook_amount; c++)
{
if(singlebook[c].barcode==barcode)/* insert a if-else statement to judge operation
singlebook[c].barcode==barcode as the test condition */
{
if(strstr(singlebook[c].loanstatus,"non-loan")!=NULL||strcmp(singlebook[c].loantype,"non-loan")==0)
/* insert a if-else statement to judge operation strstr(singlebook[c].loanstatus,"non-loan")!=NULL or
strcmp(singlebook[c].loantype,"non-loan")==0 as the test condition */
58
printf("\nYou couldn't borrow this book. The book was marked as %s",singlebook[c].details);
/*ask user that he can not borrow this book*/
else if( strstr(singlebook[c].loanstatus,"Available")!=NULL&&strcmp(singlebook[c].loantype,"short")==0)
{/* insert a if-else statement to judge operation strstr(singlebook[c].loanstatus,"Available")!=NULL and
strcmp(singlebook[c].loantype,"short")==0 as the test condition */
strcpy(singlebook[c].loanstatus,"on_loan");/* assign "on_loan" to singlebook[c].loanstatus */
time( &sec ); /* implement the function time( &sec )*/
sec+=1209600;/* assign 1209600 to sec+ */
ttime = localtime ( &sec );/* assign localtime ( &sec ) to ttime */
year= ttime->tm_year; /* assign ttime->tm_year to year */
year = year +1900;/* assign year +1900 to year */
month= ttime->tm_mon; /* assign ttime->tm_mon to month */
month=month+1;/* assign month+1 to month */
day= ttime->tm_mday;/* assign ttime->tm_mday to day */
singlebook[c].due_day=day;/* assign day to singlebook[c].due_day */
singlebook[c].due_month=month;/* assign month to singlebook[c].due_month*/
singlebook[c].due_year=year;/* assign year to singlebook[c].due_year*/
printf("\nThe book's due date is at:%d-%d-%d\n",day,month,year);/*output the due date*/
strcpy(singlebook[c].borrower,name);/* assign name to singlebook[c].borrower*/
for(d = 0; d < 15; d++)
{
if(collection[b].borrowerID[d]==-1)/*insert a if-else statement to judge operation
collection[b].borrowerID[d]==-1 as the test condition*/
{
59
collection[b].borrowerID[d]=ID;/* assign ID to collection[b].borrowerID[d]*/
collection[b].borrowerID[d+1]=-1;/* assign -1 to collection[b].borrowerID[d+1]*/
break;
}
}
collection[b].available_copies--;/*assign collection[b].available_copies-1 to collection[b].available_copies*/
if(collection[b].available_copies==0)
/*insert a if-else statement to judge operation collection[b].available_copies==0 as the test condition*/
strcpy(collection[b].availability,"on_loan");/*assign "on_loan" to collection[b].availability*/
update_bookinfo();/* implement the function update_bookinfo()*/
update_singlebook(no);/* implement the function update_singlebook(no)*/
printf("\nYou have succeedd borrowing a book.\n");
}
else
if ( strstr(singlebook[c].loanstatus,"Available")!=NULL && !strcmp(singlebook[c].loantype,"normal") )
{/*insert a if-else statement to judge operation strstr(singlebook[c].loanstatus,"Available")!=NULL and
!strcmp(singlebook[c].loantype,"normal") as the test condition*/
strcpy(singlebook[c].loanstatus,"on_loan");/*assign "on_loan" to singlebook[c].loanstatus*/
time( &sec );/* implement the function time( &sec )*/
sec+=2592000;/*assign 2592000 to sec+*/
ttime = localtime ( &sec );/* assign localtime ( &sec ) to ttime */
year1= ttime->tm_year;/* assign ttime->tm_year to year1 */
year1 = year1 +1900;/* assign year1 +1900 to year1 */
60
month1= ttime->tm_mon; /* assign ttime->tm_mon to month1 */
month1=month1+1;/* assign month1+1 to month1 */
day1= ttime->tm_mday; /* assign ttime->tm_mday to day1 */
singlebook[c].due_day=day1;/* assign day1 to singlebook[c].due_day */
singlebook[c].due_month=month1;/* assign month1 to singlebook[c].due_month*/
singlebook[c].due_year=year1;/* assign year1 to singlebook[c].due_year*/
printf("\nThe book's due date is at:%d-%d-%d\n",day1,month1,year1);/*output the due date*/
strcpy(singlebook[c].borrower,name);/* assign name to singlebook[c].borrower*/
for(d = 0; d<15; d++)
{
if(collection[b].borrowerID[d]==-1)/*insert a if-else statement to judge
operation collection[b].borrowerID[d]==-1 as the test condition*/
{
collection[b].borrowerID[d]=ID;/* assign ID to collection[b].borrowerID[d]*/
collection[b].borrowerID[d+1]=-1;/* assign -1 to collection[b].borrowerID[d+1]*/
break;
}
}
collection[b].available_copies--;/*assign collection[b].available_copies-1 to collection[b].available_copies*/
if(collection[b].available_copies==0)
/*insert a if-else statement to judge operation collection[b].available_copies==0 as the test condition*/
strcpy(collection[b].availability,"on_loan");/*assign "on_loan" to collection[b].availability*/
update_bookinfo();/* implement the function update_bookinfo()*/
update_singlebook(no);/* implement the function update_singlebook(no)*/
61
printf("\nYou have succeedd borrowing a book.\n");
}
}
}
}
else if(users[a].userprivilege==2||users[a].userprivilege==3)
{/*insert a if-else statement to judge operation users[a].userprivilege==2 or users[a].userprivilege==3 as the test condition*/
printf("\nPlease enter the book's ISBN:\n");/*ask user to enter the ISBN*/
scanf("%s",ISBN);/*read and store the value into the ISBN*/
for(e=0;e<number_of_books;e++)
{
if(strcmp(ISBN,collection[e].ISBN)==0)
{/* insert a if-else statement to judge operation strcmp(ISBN,collection[e].ISBN)==0 as the test condition */
printf("\nThe book number is %d\n",collection[e].booknumber);/*output the book number*/
no = collection[e].booknumber;/* assign collection[e].booknumber to no */
b = e;/* assign e to b */
}
}
show_singlebook(no);/* implement the function show_singlebook(no)*/
printf("\nPlease enter the book's barcode:\n");/*ask user to enter barcode*/
scanf("%d",&barcode);/*read and store the value into the adress of barcode*/
for(c = 0; c < singlebook_amount; c++)
{
if(singlebook[c].barcode==barcode)/* insert a if-else statement to judge operation
singlebook[c].barcode==barcode as the test condition */
{
62
if(strstr(singlebook[c].loanstatus,"non-loan")!=NULL||strcmp(singlebook[c].loantype,"non-loan")==0)
/* insert a if-else statement to judge operation strstr(singlebook[c].loanstatus,"non-loan")!=NULL or
strcmp(singlebook[c].loantype,"non-loan")==0 as the test condition */
printf("\nYou couldn't borrow this book. The book was marked as %s\n",singlebook[c].details);
/*ask user that he can not borrow this book*/
else if( strstr(singlebook[c].loanstatus,"Available")!=NULL&&strcmp(singlebook[c].loantype,"short")==0)
{/* insert a if-else statement to judge operation strstr(singlebook[c].loanstatus,"Available")!=NULL and
strcmp(singlebook[c].loantype,"short")==0 as the test condition */
strcpy(singlebook[c].loanstatus,"on_loan");/* assign "on_loan" to singlebook[c].loanstatus */
time( &sec );/* implement the function time( &sec )*/
sec+=1209600;/* assign 1209600 to sec+ */
ttime = localtime ( &sec );/* assign localtime ( &sec ) to ttime */
year2= ttime->tm_year;/* assign ttime->tm_year to year2 */
year2 = year2 +1900;/* assign year2 +1900 to year2 */
month2= ttime->tm_mon;/* assign ttime->tm_mon to month2 */
month2=month2+1;/* assign month2+1 to month2 */
day2= ttime->tm_mday; /* assign ttime->tm_mday to day2 */
singlebook[c].due_day=day2;/* assign day2 to singlebook[c].due_day */
singlebook[c].due_month=month2;/* assign month2 to singlebook[c].due_month*/
singlebook[c].due_year=year2;/* assign year2 to singlebook[c].due_year*/
printf("\nThe book's due date is at:%d-%d-%d\n",day2,month2,year2);/*output the due date*/
63
strcpy(singlebook[c].borrower,name);/* assign name to singlebook[c].borrower*/
for(d = 0; d < 15;d++)
{
if(collection[b].borrowerID[d]==-1)/*insert a if-else statement to judge
operation collection[b].borrowerID[d]==-1 as the test condition*/
{
collection[b].borrowerID[d]=ID;/* assign ID to collection[b].borrowerID[d]*/
collection[b].borrowerID[d+1]=-1;/* assign -1 to collection[b].borrowerID[d+1]*/
break;
}
}
collection[b].available_copies--;/*assign collection[b].available_copies-1 to collection[b].available_copies*/
if(collection[b].available_copies==0)
/*insert a if-else statement to judge operation collection[b].available_copies==0 as the test condition*/
strcpy(collection[b].availability,"on_loan");/*assign "on_loan" to collection[b].availability*/
update_bookinfo();/* implement the function update_bookinfo()*/
update_singlebook(no);/* implement the function update_singlebook(no)*/
printf("\nYou have succeedd borrowing a book.\n");
}
else if ( strstr(singlebook[c].loanstatus,"Available")!=NULL && !strcmp(singlebook[c].loantype,"normal") )
{/* insert a if-else statement to judge operation strstr(singlebook[c].loanstatus,"Available")!=NULL and
!strcmp(singlebook[c].loantype,"normal") as the test condition */
strcpy(singlebook[c].loanstatus,"on_loan");/* assign "on_loan" to singlebook[c].loanstatus */
64
time( &sec ); /* implement the function time( &sec )*/
sec+=7776000;/* assign 7776000 to sec+ */
ttime = localtime ( &sec );/* assign localtime ( &sec ) to ttime */
year3= ttime->tm_year;/* assign ttime->tm_year to year3 */
year3 = year3 +1900;/* assign year3 +1900 to year3 */
month3= ttime->tm_mon;/* assign ttime->tm_mon to month3 */
month3=month3+1;/* assign month3+1 to month3 */
day3= ttime->tm_mday;/* assign ttime->tm_mday to day3 */
singlebook[c].due_day=day3;/* assign day3 to singlebook[c].due_day */
singlebook[c].due_month=month3;/* assign month3 to singlebook[c].due_month*/
singlebook[c].due_year=year3;/* assign year3 to singlebook[c].due_year*/
printf("\nThe book's due date is at:%d-%d-%d\n",day3,month3,year3);/*output the due date*/
strcpy(singlebook[c].borrower,name);/* assign name to singlebook[c].borrower*/
for(d = 0; d < 15; d++)
{
if(collection[b].borrowerID[d]==-1)/*insert a if-else statement to judge
operation collection[b].borrowerID[d]==-1 as the test condition*/
{
collection[b].borrowerID[d]=ID;/* assign ID to collection[b].borrowerID[d]*/
collection[b].borrowerID[d+1]=-1;/* assign -1 to collection[b].borrowerID[d+1]*/
break;
}
}
collection[b].available_copies--;/*assign collection[b].available_copies-1 to collection[b].available_copies*/
65
if(collection[b].available_copies==0)
/*insert a if-else statement to judge operation collection[b].available_copies==0 as the test condition*/
strcpy(collection[b].availability,"on_loan");/*assign "on_loan" to collection[b].availability*/
update_bookinfo();/* implement the function update_bookinfo()*/
update_singlebook(no);/* implement the function update_singlebook(no)*/
printf("\nYou have succeedd borrowing a book.\n");
}
}
}
}
}
}
}
void bookreturn(int id, char*name)/* a function to return */
{
FILE *filenm;/* declare one pointer of the type of file namely filenm */
int a = 0, b = 0;/* declare two variables of the type of integer namely a,b, and initialize them equal to 0*/
int book_no, bc, ID;/* declare three variables of the type of integer namely book_no,bc and ID*/
printf("\n\n*********************************************\n");/*menu*/
printf(" Return a Book");
printf("\n*********************************************\n\n");
printf("Please enter your ID number:\n");/*enter ID*/
scanf("%d",&ID);/* read and store the inputted value into the address of ID*/
printf("\nPlease enter your book's number:\n");/*enter book's number*/
scanf("%d",&book_no);/* read and store the inputted value into the address of book_no*/
printf("\nPlease enter your book's barcode:\n");/*enter book's barcode*/
scanf("%d",&bc);/* read and store the inputted value into the address of bc*/
filenm = open_singlebook(book_no);/* assign open_singlebook(book_no) to filename */
show_singlebook(book_no);/* implement the function show_singlebook(book_no)*/
66
while(a < singlebook_amount)
{
if(singlebook[a].barcode == bc)/* insert a if-else statement to judge operation singlebook[a].barcode == bc as the test condition */
{
strcpy(singlebook[a].borrower,"None");/* assign "None" to singlebook[a].borrower */
strcpy(singlebook[a].loanstatus,"Available");/* assign "Available" to singlebook[a].loanstatus */
singlebook[a].lastborrowed_day=singlebook[a].due_day;/* assign singlebook[a].due_day to singlebook[a].lastborrowed_day*/
singlebook[a].lastborrowed_month=singlebook[a].due_month;/* assign singlebook[a].due_month to singlebook[a].lastborrowed_month */
singlebook[a].lastborrowed_year=singlebook[a].due_year;/* assign singlebook[a].due_year to singlebook[a].lastborrowed_year */
singlebook[a].due_day=0;/* assign 0 to singlebook[a].due_day */
singlebook[a].due_month=0;/* assign 0 to singlebook[a].due_month */
singlebook[a].due_year=0;/* assign 0 to singlebook[a].due_year */
update_singlebook(book_no);/* implement the function update_singlebook(book_no)*/
a++;/* assign a+1 to a*/
}
else
{
a++;/* assign a+1 to a*/
}
}
file_close(filenm);/*close the file*/
while(b < number_of_users)
{
if(collection[book_no].borrowerID[b]==ID)
/* insert a if-else statement to judge operation ollection[book_no].borrowerID[b]==ID as the test condition */
{
collection[book_no].borrowerID[b]=-1;/* assign -1 to collection[book_no].borrowerID[b] */
collection[book_no].available_copies++;/* assign collection[book_no].available_copies+1 to collection[book_no].available_copies */
update_bookinfo();/* implement the function update_bookinfo()*/
67
printf("\nThanks for returning the book!\n");
admin(id,name);/* implement the function admin(id,name)*/
b++;/* assign b+1 to b*/
}
else
{
b++;/* assign b+1 to b*/
}
}
printf("\nError occured.\n");
admin(id,name);/* implement the function admin(id,name)*/
}
void display_available (int id, char* name)/* a function to display the available book*/
{
int a;/* declare one variable of the type of integer namely a*/
printf("\n\n*********************************************\n");/*menu*/
printf(" The Available Books List");
printf("\n*********************************************\n\n");
for(a = 0; a < 20; a++)
{
if(strcmp(collection[a].availability,"Available")==0)
/* insert a if-else statement to judge operation strcmp(collection[a].availability,"Available")==0 as the test condition */
{
printf("%s\n",collection[a].title);
}
}
admin(id,name);/* implement the function admin(id,name)*/
}
void display_borrowed(int id, char* name)/* a function to display the book that has been borrowed*/
{
int a;/* declare one variable of the type of integer namely a*/
68
printf("\n\n*********************************************\n");/*menu*/
printf(" The Borrowed Books List");
printf("\n*********************************************\n\n");
for(a = 0; a < number_of_books; a++)
{
if((collection[a].total_copies - collection[a].available_copies)!=0)
/*insert a if-else statement to judge operation (collection[a].total_copies-collection[a].available_copies)!=0 as the test condition*/
{
printf("%s\n",collection[a].title);
}
}
admin(id,name);/* implement the function admin(id,name)*/
}
void display_borrower(int id, char* name)/* a function to display the borrower */
{
FILE *filenm;/* declare one pointer of the type of file namely filenm */
int a = 0;/* declare one variable of the type of integer namely a and initialize it equal to 0*/
int b = 0;/* declare one variable of the type of integer namely b and initialize it equal to 0*/
int c = 0;/* declare one variable of the type of integer namely c and initialize it equal to 0*/
printf("\n\n*********************************************\n");/*menu*/
printf(" The Borrower's List");
printf("\n*********************************************\n\n");
for(a = 0; a < number_of_books; a++)
{
if((collection[a].total_copies - collection[a].available_copies)!=0)
/*insert a if-else statement to judge operation (collection[a].total_copies-collection[a].available_copies)!=0 as the test condition*/
{
c = collection[a].booknumber;/* assign collection[a].booknumber to c*/
show_singlebook(c);/* implement the function show_singlebook(c)*/
for(b = 0; b < singlebook_amount; b++)
{
69
if (strcmp(singlebook[b].borrower,"None")!=0)
/*insert a if-else statement to judge operation strcmp(singlebook[b].borrower,"None")!=0 as the test condition*/
{
printf("%s\t%s\t\n",singlebook[b].borrower,collection[a].title);
}
}
}
}
admin(id,name);/* implement the function admin(id,name)*/
}
void display_users(int id, char* name)/* a function to display the user */
{
int a;/* declare one variable of the type of integer namely a*/
int b;/* declare one variable of the type of integer namely b*/
printf("\n\n*********************************************\n");/*menu*/
printf(" Display Member's Data");
printf("\n*********************************************\n\n");
printf("\nPlease enter the ID number of a member that you want to display:\n");/*enter the ID*/
scanf("%d",&b);/*read and store the value into the address of b*/
for(a = 0; a < number_of_users; a++)
{
if(b == users[a].ID)
/*insert a if-else statement to judge operation b == users[a].ID as the test condition*/
printf("Member's fullname: %d\t ID number: %s\t Member's privilege: %d\n\n",users[a].fullname,users[a].ID, users[a].userprivilege);
/*display the information of the member*/
}
admin(id,name);/* implement the function admin(id,name)*/
}
void privilege_changing(int id, char* name)/* a function to change the privilege */
{
70
int a = 0;/* declare one variable of the type of integer namely a and initialize it equal to 0*/
int b;/* declare one variable of the type of integer namely b*/
int new_prv;/* declare one variable of the type of integer namely new_prv*/
int line;/* declare one variable of the type of integer namely line*/
printf("\n\n*********************************************\n");/*menu*/
printf(" Change Privilege");
printf("\n*********************************************\n\n");
printf("Which user's privilege that do you want to change?\nPlease enter that user's ID number: \n");
scanf("%d",&b);/*read and store the value into the address of b*/
printf("\nPlease enter the user's new privilege: \n");
scanf("%d",&new_prv);/*read and store the value into the address of new_prv*/
while(a < number_of_users)
{
if(users[a].ID==b) /*insert a if-else statement to judge operation users[a].ID==b as the test condition*/
{
users[a].userprivilege=new_prv;/* assign new_prv to users[a].userprivilege*/
showupdate_userdata();/* implement the function showupdate_userdata()*/
printf("The user's privilege has been changed!\n\n");
admin(id,name);/* implement the function admin(id,name)*/
}
else
{a++;}/* assign a+1 to a*/
}
printf("error!");
admin(id,name);/* implement the function admin(id,name)*/
}
void mark_details(int id, char* name)/* a function to mark the book's detail */
{
FILE *filenm;/* declare one pointer of the type of file namely filenm */
int a = 0, b;/* declare two variables of the type of integer namely a,b and initialize a equal to 0*/
int no;/* declare one variable of the type of integer namely no*/
71
char title[50];/* declare one string of the type of character namely title*/
char markbook[50];/* declare one string of the type of character namely markbook*/
printf("\n\n*********************************************\n");/*menu*/
printf(" Mark Book Detail");
printf("\n*********************************************\n\n");
printf("Please enter the number of the book that you want to mark: "); /*enter the number of the book */
scanf("%d",&no);/*read and store the value into the address of no*/
printf("\nPlease enter the barcode of the book that you want to mark: ");/*enter the barcode of the book*/
scanf("%d",&b);/*read and store the value into the address of b*/
printf("\nPlease mark the book's detail (damaged, etc.):\n");/*mark the book's detail*/
scanf("%s",markbook);/*read and store the value into the address of markbook*/
filenm = open_singlebook(no);/* assign open_singlebook(no) to filenm */
show_singlebook(no);/* implement the function show_singlebook(no)*/
while(a < singlebook_amount)
{
if(singlebook[a].barcode==b)
/*insert a if-else statement to judge operation singlebook[a].barcode==b as the test condition*/
{
strcpy(singlebook[a].details,markbook);/* assign markbook to singlebook[a].details */
update_singlebook(no);/* implement the function update_singlebook(no)*/
printf("The book's detail has been marked!\n");
admin(id,name);/* implement the function admin(id,name)*/
}
else{a++;}/* assign a+1 to a*/
}
printf("Error occured.");
admin(id,name);/* implement the function admin(id,name)*/
}
void booksearch(void)/* a function to search a book*/
{
int a;/* declare one variable of the type of integer namely a*/
72
while(1)
{ printf("\n\n*********************************************\n");/*menu*/
printf(" Search a book");
printf("\n*********************************************\n\n");
printf("1. By subject\n2. By author\n3. By title\n\n");
printf("To choose the option above, please enter 1-3: ");
scanf("%d",&a);/*read and store the value into the address of a*/
switch (a) /* use switch statement to choose the operation depending on the value of a */
{
case 1 : subjectsearch();/* case=1, implement the function subjectsearch() */
return;
case 2 : authorsearch(); /* case=2, implement the function authorsearch() */
return;
case 3 : titlesearch(); /* case=3, implement the function titlesearch() */
return;
default : wrong_option_sentence(); /* implement the function wrong_option_sentence()*/
}
}
}
void subjectsearch (void) /* a function to search a book by subject*/
{
char sub[50];/* declare one string of the type of character namely sub*/
int a,b = 0;/* declare two variables of the type of integer namely a,b and initialize b equal to 0*/
printf("\nPlease enter the book's subject:\n");/*enter the book's subject*/
scanf("%s",sub); /*read and store the value into sub*/
while(strlen(sub)<3)
{
printf("The subject you entered is too short (It has to be more than 3 letters).\n");
printf("\nPlease enter the book's subject again:\n");/*enter the book's subject again*/
73
scanf("%s",sub);/*read and store the value into the address of markbook*/
}
for(a = 0; a < number_of_books; a++)
if ( strstr(collection[a].subject, sub)!=NULL )
/*insert a if-else statement to judge operation strstr(collection[a].subject, sub)!=NULL as the test condition*/
displaybook[b++] = &collection[a]; /* assign &collection[a]to displaybook[b++]*/
booklist(b);/* implement the function booklist(b)*/
}
void booklist(int j)/* a function to display the information of books*/
{
int a, b;/* declare two variables of the type of integer namely a,b*/
int booklabel,page;/* declare two variables of the type of integer namely booklabel,page*/
if (j==0)/*insert a if-else statement to judge operation j==0 as the test condition*/
{
printf("Sorry, the book cannot be found.\n");
mainmenu();/* implement the function mainmenu()*/
}
printf("There are %d results and %d pages found. \n",j,(j/DATABASE_PAGES+1) );
page = 1;/* assign 1 to page*/
while(1)
{
printf("Page%d\n",page);
for (a = (page-1)*DATABASE_PAGES ;a<(page*DATABASE_PAGES)&&a<j; a++)
{
printf("Book label: %d\n\n",a+1);
printf("Book title: %s\n",displaybook[a]->title);
printf("Book author: %s\n",displaybook[a]->author);
printf("Book subject: %s\n",displaybook[a]->subject);
printf("\n\n\n");
74
}
printf("1. See a book's details\n2. Go and search in another page\n3. Return to main menu\n"); /*menu*/
printf("\nTo choose the option above, please enter 1-3: ");
scanf("%d",&b);/*read and store the value into the address of b*/
switch(b) /* use switch statement to choose the operation depending on the value of b */
{
case 1 : printf("\nTo see a particular book details, please enter a book label:\n");
scanf("%d",&booklabel); /*read and store the value into the address of booklabel*/
search_singlebook(displaybook[booklabel-1]->booknumber);
/* implement the function search_singlebook(displaybook[booklabel-1]->booknumber)*/
getchar(); getchar();
break;
case 2 : printf("\nTo move to another page, please enter a particular page:\n");
scanf("%d",&page);/*read and store the value into the address of page*/
break;
case 3 : mainmenu(); break;/* implement the function mainmenu()*/
default : wrong_option_sentence();/* implement the function wrong_option_sentence()*/
}
}
}
void search_singlebook(int number) /* a function to display the information of single book*/
{
int a;/* declare one variable of the type of integer namely a*/
show_singlebook(number);/* implement the function show_singlebook(number)*/
for (a = 0; a < singlebook_amount; a++)
{
75
printf("Book Barcode: %d\n",singlebook[a].barcode);
printf("Loan Type: %s\n",singlebook[a].loantype);
printf("Loan Status: %s\n",singlebook[a].loanstatus);
printf("Last borrowed: %d/",singlebook[a].lastborrowed_day);
printf("%d/",singlebook[a].lastborrowed_month);
printf("%d\n",singlebook[a].lastborrowed_year);
printf("Due date: %d/",singlebook[a].due_day);
printf("%d/",singlebook[a].due_month);
printf("%d\n",singlebook[a].due_year);
printf("Details: %s\n\n",singlebook[a].details);
printf("Borrowers: %s\n",singlebook[a].borrower);
}
mainmenu(); /* implement the function admin(id,name)*/
}
void authorsearch(void)/* a function to search a book by author*/
{
char aut[50];/* declare one string of the type of character namely aut*/
int a,b = 0;/* declare two variables of the type of integer namely a,b and initialize b equal to 0*/
printf("\nPlease enter the book's author:\n"); /*enter the book's author*/
scanf("%s",aut);/*read and store the value into aut*/
while(strlen(aut)<3)
{
printf("The author's name you entered is too short (It has to be more than 3 letters).\n");
printf("\nPlease enter the book's author again:\n");/*enter the book's author again*/
scanf("%s",aut);/*read and store the value into aut*/
}
for(a = 0;a < number_of_books; a++)
if ( strstr(collection[a].author, aut)!=NULL )
/*insert a if-else statement to judge operation strstr(collection[a].author, aut)!=NULL as the test condition*/
displaybook[b++] = &collection[a];/* assign &collection[a]to displaybook[b++]*/
76
booklist(b); /* implement the function booklist(b)*/
}
void titlesearch(void)/* a function to search a book by title*/
{
char tit[100];/* declare one string of the type of character namely tit*/
int a,b = 0;/* declare two variables of the type of integer namely a,b and initialize b equal to 0*/
printf("\nPlease enter the book's title:\n");/*enter the book's title*/
scanf("%s",tit);/*read and store the value into tit*/
while(strlen(tit)<3)
{
printf("The title you entered is too short (It has to be more than 3 letters).\n");
printf("\nPlease enter the book's title again:\n");/*enter the book's title again*/
scanf("%s",tit);/*read and store the value into tit*/
}
for(a = 0;a < number_of_books; a++)
if ( strstr(collection[a].title, tit)!=NULL )
/*insert a if-else statement to judge operation strstr(collection[a].title, tit)!=NULL as the test condition*/
displaybook[b++] = &collection[a];/* assign &collection[a]to displaybook[b++]*/
booklist(b);/* implement the function booklist(b)*/
}
void alteringtime(int book_no, int barcode) /* a function to alter time*/
{
FILE *filenm;/* declare one pointer of the type of file namely filenm */
int a = 0;/* declare one variable of the type of integer namely a and initialize it equal to 0*/
time_t b;
short year1,month1,day1;/* declare three variables of the type of short integer namely year1,month1 and day1*/
77
short year2,month2,day2;/* declare three variables of the type of short integer namely year2,month2 and day2*/
struct tm *time1;/*Declare a pointer of the type of structure tm namely time1*/
struct tm *time2;/*Declare a pointer of the type of structure tm namely time2*/
int sec;/* declare one variable of the type of integer namely sec */
time(&b); /* the function to obtain time now*/
filenm = open_singlebook(book_no);/* assign open_singlebook(book_no) to filenm*/
show_singlebook(book_no);/* implement the function show_singlebook(book_no)*/
if (privilege == 1)/*insert a if-else statement to judge operation privilege == 1 as the test condition*/
{
while (a < singlebook_amount)
{
if(singlebook[a].barcode== barcode)
/*insert a if-else statement to judge operation singlebook[a].barcode== barcode as the test condition*/
{
if (strcmp(singlebook[a].loantype,"short") == 0)
/*insert a if-else statement to judge operation strcmp(singlebook[a].loantype,"short") == 0 as the test condition*/
sec = 1209600; /* 2 weeks = 14 days*/
if (strcmp(singlebook[a].loantype,"normal") == 0)
/*insert a if-else statement to judge operation strcmp(singlebook[a].loantype,"normal") == 0 as the test condition*/
sec = 2592000; /* 30 days */
}
else{a++;}/* assign a+1 to a*/
}
}
if (privilege==2 || privilege==3)
/*insert a if-else statement to judge operation privilege==2 or privilege==3 as the test condition*/
{
while (a < singlebook_amount)
78
{
if(singlebook[a].barcode== barcode)
/*insert a if-else statement to judge operation singlebook[a].barcode== barcode as the test condition*/
{
sec = 7776000; /*90 days*/
}
else{a++;}/* assign a+1 to a*/
}
}
time1 = localtime(&b);/* assign localtime(&b) to time1*/
b += sec; /* assign sec to b +*/
day1 = time1->tm_mday; /* assign time1->tm_mday to day1*/
month1 = time1->tm_mon; /* assign time1->tm_mon to month1*/
year1 = time1->tm_year; /* assign time1->tm_year to year1*/
time2 = localtime(&b);/* assign localtime(&b) to time2*/
day2 = time2->tm_mday; /* assign time2->tm_mday to day2*/
month2 = time2->tm_mon; /* assign time2->tm_mon to month2*/
year2 = time2->tm_year;/* assign time2->tm_year to year2 */
singlebook[a].due_day= day2;/* assign day2 to singlebook[a].due_day*/
singlebook[a].due_month = month2 + 1; /* assign month2 + 1 to singlebook[a].due_month*/
singlebook[a].due_year= year2 + 1900; /* assign year2 + 1900 to singlebook[a].due_year*/
update_singlebook (book_no);/* implement the function update_singlebook (book_no)*/
printf("You have renewed the book!\n");
printf("The renewal time is %d/%d/%d. The due date is %d/%d/%d.\n",year1+1900, month1+1,day1,year2+1900,month2+1,day2);
file_close(filenm);/*close the file*/
}
void update_singlebook(int number) /* a function to update the data of single book*/
{
FILE *filenm;/* declare one pointer of the type of file namely filenm */
79
int a;/* declare one variable of the type of integer namely a */
filenm = write_singlebook(number);/* assign write_singlebook(number) to filenm*/
fprintf(filenm,"Title:%s\t Author:%s\t\n",collection[number-1].title,collection[number-1].author);
/*input the value of collection[number-1].title,collection[number-1].author into the file which is pointed by filenm*/
fprintf(filenm,"Barcode\t Loan type\t Loan status\t Due date\t Last borrowed\t Current borrower\t Details\n");
/*input the value into the file which is pointed by filenm*/
for (a=0;a<singlebook_amount;a++)
{
fprintf(filenm,"%d\t\t",singlebook[a].barcode);
/*input the value of singlebook[a].barcode into the file which is pointed by filenm*/
fprintf(filenm,"%s\t\t",singlebook[a].loantype);
/*input the value of singlebook[a].loantype into the file which is pointed by filenm*/
fprintf(filenm,"%s\t\t",singlebook[a].loanstatus);
/*input the value of singlebook[a].loanstatus into the file which is pointed by filenm*/
fprintf(filenm,"%2d/",singlebook[a].due_day);
/*input the value of singlebook[a].due_day into the file which is pointed by filenm*/
fprintf(filenm,"%2d/",singlebook[a].due_month);
/*input the value of singlebook[a].due_month into the file which is pointed by filenm*/
fprintf(filenm,"%2d\t\t",singlebook[a].due_year);
/*input the value of singlebook[a].due_year into the file which is pointed by filenm*/
fprintf(filenm,"%2d/",singlebook[a].lastborrowed_day);
/*input the value of singlebook[a].lastborrowed_day into the file which is pointed by filenm*/
fprintf(filenm,"%2d/",singlebook[a].lastborrowed_month);
/*input the value of singlebook[a].lastborrowed_month into the file which is pointed by filenm*/
fprintf(filenm,"%2d\t\t",singlebook[a].lastborrowed_year);
/*input the value of singlebook[a].lastborrowed_year into the file which is pointed by filenm*/
fprintf(filenm,"%10s\t\t",singlebook[a].borrower);
/*input the value of singlebook[a].borrower into the file which is pointed by filenm*/
80
fprintf(filenm,"%5s\n",singlebook[a].details);
/*input the value of singlebook[a].details into the file which is pointed by filenm*/
}
file_close(filenm);/*close the file*/
}
void show_bookinfo(void)/* a function to store the data of book into file*/
{
FILE *filenm;/* declare one pointer of the type of file namely filenm */
int line;/* declare one variable of the type of integer namely line */
int a = 0;/* declare one variable of the type of integer namely a and initialize it equal to 0*/
int b;/* declare one variable of the type of integer namely b */
char c;/* declare one variable of the type of integer namely c */
char str[1500];/* declare one string of the type of character namely str */
filenm = file_read ("booklist.txt");/* assign file_read ("booklist.txt") to filenm*/
line = line_number (filenm);/* assign line_number (filenm) to line*/
line = line-1;/* assign line-1 to line*/
collection = (struct bookdetails *) malloc( line * sizeof(struct bookdetails));/* assign space to collection*/
displaybook = (struct bookdetails **) malloc( line * sizeof(struct bookdetails *));/* assign space to displaybook */
fgets(str,1500,filenm); /* read the file and assign filenm to str*/
for (a = 0; a < line; a++)
{
fscanf(filenm,"%d",&collection[a].booknumber);
/*output the value of collection[a].booknumber into the file which is pointed by filenm*/
while( isspace(fgetc(filenm)) );
fseek(filenm,-1L,SEEK_CUR); /* fix the position for filenm renewedly */
for(b = 0; (c=fgetc(filenm))!='\t' ;b++)
collection[a].title[b] = c;/* assign c to collection[a].title[b] */
collection[a].title[b] = '\0';/* assign c to collection[a].title[b] */
81
fseek(filenm,-1L,SEEK_CUR); /* fix the position for filenm renewedly */
while( isspace(fgetc(filenm)) );
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
for(b=0; (c=fgetc(filenm))!='\t' ;b++)
collection[a].author[b] = c;/* assign c to collection[a].author[b] */
collection[a].author[b] = '\0';/* assign NULL to collection[a].author[b] */
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
while( isspace(fgetc(filenm)) );
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
for(b=0; (c=fgetc(filenm))!='\t' ;b++)
collection[a].shelfmark[b] = c;/* assign c to collection[a].shelfmark[b] */
collection[a].shelfmark[b] = '\0';/* assign NULL to collection[a].shelfmark[b] */
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
while( isspace(fgetc(filenm)) );
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
for(b=0; (c=fgetc(filenm))!='\t' ;b++)
collection[a].ISBN[b] = c;/* assign c to collection[a].ISBN[b] */
collection[a].ISBN[b] = '\0';/* assign NULL to collection[a].ISBN[b] */
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
while( isspace(fgetc(filenm)) );
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
for(b = 0; (c=fgetc(filenm))!='\t' ;b++)
82
collection[a].subject[b] = c;/* assign c to collection[a].subject[b] */
collection[a].subject[b] = '\0';/* assign NULL to collection[a].subject[b] */
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
while( isspace(fgetc(filenm)) );
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
fscanf(filenm,"%d",&collection[a].available_copies);
while( isspace(fgetc(filenm)) );
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
fscanf(filenm,"%d",&collection[a].total_copies);
/*output the value of collection[a].total_copies into the file which is pointed by filenm*/
while( isspace(fgetc(filenm)) );
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
for(b=0; (c=fgetc(filenm))!='\t' ;b++)
collection[a].availability[b] = c;/* assign c to collection[a].availability[b] */
collection[a].availability[b] = '\0';/* assign NULL to collection[a].availability[b] */
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
while( isspace( c=fgetc(filenm) ) && c!='\n');
if(c=='\n') /*insert a if-else statement to judge operation c=='\n' as the test condition*/
collection[a].borrowerID[0] = -1;/* assign -1 to collection[a].borrowerID[0] */
else
{
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
for(b=0; b<15; b++)
{
while( isspace( c=fgetc(filenm) ) && c!='\n');
if (c=='\n')/*insert a if-else statement to judge operation c=='\n' as the test condition*/
83
{b++; /* assign b+1 to b */
break;}
fseek(filenm,-1L,SEEK_CUR);/* fix the position for filenm renewedly */
fscanf(filenm,"%d",&collection[a].borrowerID[b]);
/*output the value of collection[a].borrowerID[b] into the file which is pointed by filenm*/
}
collection[a].borrowerID[b] = -1;/* assign -1 to collection[a].borrowerID[b] */
}
}
number_of_books = line;/* assign line to number_of_books */
file_close(filenm);/*close the file*/
return;
}
void update_bookinfo (void)/* a function to update the information of books */
{
FILE *filenm;/* declare one pointer of the type of file namely filenm */
int a, b;/* declare two variables of the type of integer namely a,b */
filenm = file_write ("booklist.txt");/* assign file_write ("booklist.txt") to filenm*/
fprintf(filenm,"Book number\t\t\t\t\t\t\t\t\t\t\t Title\t\t\t\tAuthor\t\t\tShelf mark\t\t\tISBN\t\t\tSubject\t\tAvailable copies\tTotalcopies\t\tAvailability\t\tBorrower ID\n");
for (a=0;a<number_of_books;a++)
{
fprintf(filenm,"%d\t",collection[a].booknumber);
/*input the value of collection[a].booknumber into the file which is pointed by filenm*/
fprintf(filenm,"%100s\t",collection[a].title);
/*input the value of collection[a].title into the file which is pointed by filenm*/
fprintf(filenm,"%20s\t",collection[a].author);
/*input the value of collection[a].author into the file which is pointed by filenm*/
fprintf(filenm,"%20s\t",collection[a].shelfmark);
/*input the value of collection[a].shelfmark into the file which is pointed by filenm*/
84
fprintf(filenm,"%30s\t",collection[a].ISBN);
/*input the value of collection[a].ISBNinto the file which is pointed by filenm*/
fprintf(filenm,"%20s\t\t",collection[a].subject);
/*input the value of collection[a].subject into the file which is pointed by filenm*/
fprintf(filenm,"%d\t\t",collection[a].available_copies);
/*input the value of collection[a].available_copies into the file which is pointed by filenm*/
fprintf(filenm,"%d\t\t",collection[a].total_copies);
/*input the value of collection[a].total_copies into the file which is pointed by filenm*/
fprintf(filenm,"%s\t",collection[a].availability);
/*input the value of collection[a].availability into the file which is pointed by filenm*/
for (b=0;b<15;b++)
{
if(collection[a].borrowerID[b]==-1) break;/*insert a if-else statement to judge operation collection[a].borrowerID[b]==-1 as the test condition*/
fprintf(filenm,"%d\t",collection[a].borrowerID[b]);
/*input the value of collection[a].borrowerID[b] into the file which is pointed by filenm*/
}
fprintf(filenm,"\n");/*input line feed into the file which is pointed by filenm*/
}
file_close(filenm);/*close the file*/
}
void exit_program(void)/* a function to exit */
{
printf("Thanks for your visit, please come again next time!\n");
exit(0);
}
85
2. Simple Manual Program (this also can be found in the zipped file which is the Program Instruction Manual.docx file )
Program Instruction Manual
l Introduction
This program is a management system of a library, it has basic and essential functions such as user
registering and logging in, searching a wanted book, checking the booklist, borrowing, returning and
renewing books and etc.
New users should firstly register and choose a status among guest, student, teacher and
administrator. Different status has varying privileges. Guest can only search books. Students and
teachers can search, borrow, return and renew books, they can also check there own account status
to see which books they have borrowed and what is the loan time, however, the operation of
borrowing and returning must be completed under the admin account, that means a borrower must
go to the librarian when he or she want to borrow or return a book.
The administrator can see all the information of books, users and borrowers, check the whole book
list and available book list, change the user’s privileges, mark the status of books (damage, lost, etc).
l Manual
(1) Main menu operations
When you first open the program, you will see following main menu:
86
Option 1 allows new users to register, choose option 1 and the program will ask you to input your ID
number, surname, given name and your password. After register, the user can choose option 2 to
log into his or her account. When logging in, the user need to input user name, ID and password,
only correct input would be accepted.
Option 3 can be used to search certain books. More details will be given in following section.
(2) Account operations
If the user’s status is student or teacher, they will see the same menu:
87
Option 1 “See books you have borrowed” can list your loan list.
Option 2 “Renew a book” can extend the loan time.
Option 3 can be used to change the users’ password.
The privilege difference between students and teachers is that teacher can borrow more books than
students and teacher’s loan time is longer.
When log in as administrator, the menu is as follow:
Options 1 and 2 are used for book borrowers. When someone wants to borrow or return a book, the
admin will use these two functions. For example, a student whose ID number is 12341234 wants to
borrow the book “The Kite Runner” whose ISBN is 978-‐0747-‐56653-‐3, the admin will choose option 1
and input the borrower’s ID 12341234 and book ISBN. The operation for returning books is the same
as borrowing.
88
Option 3 is used for checking available booklist, choose it to check all the books that can be
borrowed. It looks like this:
Similarly, we can also check the borrowed booklist and borrower list.
Administrator has the privilege to change other users’ privileges status. Admin can also change the
status of books, such as “damaged” “lost” etc.
(3) Search books In the main menu, there’s an option “Search books you want to find”. It allows the user to search
wanted book by title, subject or author, like this:
89
Search by title and author are easy to understand, you just need to enter the title of the book or
author of the book, or you can just input some key word, the program will list all the related books.
For example, someone want to find the book “The Kite Runner” written by Khaled Hosseini. Firstly
we search it by title, choose 1 and input keyword “Kite”, we can see the result:
Or we can also search it by author, we select 2 and input “Khaled”, we can see:
90
Here we found two books written by the same author, we can just select the one we want.
For search by subject, because we just have limited numbers of books, at present we just two
subject of books, fiction and non-‐fiction.
l Known Bugs
The program has several bugs:
(1) After register, the user can login normally, but if the program is closed and open again, only
admin can login, students and teachers can not login normally.
(2) Sometime, when admin select “borrow” or “return” options, the program crashes.
(3) When admin select “Mark the book’s detail”, the program crashes.
(4) When the user selects the option “Renew”, the program crashes.
(5) When the admin check the user’s details, sometimes the program will crash.
l Some limitations need to be mentioned
(1) The number of books is quite limited. We have just 15 books in our library now, and when we
adding new books, we cannot directly add books in the program, we have to edit the file
“booklist.txt”.
(2) Some book information such as shelf number and loan time are just written in the files attached
to the program, but we cannot see these information directly in the program, that is difficult for
users to know some important information of the books.
(3) In the program, it always needs to repeat logging in, that is quite inconvenient.