0% found this document useful (0 votes)
74 views17 pages

Library Management System With C++

This document describes a library management system project including data modeling, algorithms, functions, coding style, and implementation. It outlines the key data types and variables used to track users, books, and other information. Functions are defined to handle login validation, user account creation, and accessing the system menu. Coding best practices around readability and single responsibility are also discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views17 pages

Library Management System With C++

This document describes a library management system project including data modeling, algorithms, functions, coding style, and implementation. It outlines the key data types and variables used to track users, books, and other information. Functions are defined to handle login validation, user account creation, and accessing the system menu. Coding best practices around readability and single responsibility are also discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Faculty of Computer & Information Sciences Examiners: Prof.

Zaki Taha
Ain Shams University Dr. Yasmine Afify
Subject: CSW150 Dr. Salsabil Amin
Fundamentals of Structured Academic year: 2nd term 2019-2020
Programming Year: 1st undergraduate

Research Topic Version (A)


Title: Library Management System
‫ علي الطالب عدم كتابة اسمه أو كتابة اي شيء يدل علي شخصيته‬:‫تحذير هام‬

1. Data Model
(1.1)
> struct userInfo{};
We used the structure to organize the new user details and call the variables when
needed.
> string adminUsername , adminPassword , ID , name , password, email;
>string account_type, adminChoice, ID_submitted , password_submitted;
>string userChoice, userSearch, book_buy_choice;
We used the string datatype to make it possible to check if a wrong input is
entered with letters or numbers.
> int phoneNumber1, phoneNumber2, phoneNumber3;
> int book1_copies=10, book2_copies=10,book3_copies=10, book4_copies=10,
book5_copies=10;
We used the integer datatype to make sure that the entered input is only numbers,
and to make a calculation for the book copies.
> int adminLoginValidation(string, string);
> int user_account(userInfo& user_account_parameter);
> int accessMenu(userInfo& user_account_parameter);
We used the integer datatype to make the functions return zero,then go to the
main function.

Page 1 of 17
(1.2)
> int book1_copies=10, book2_copies=10,book3_copies=10, book4_copies=10,
book5_copies=10;
We used these as a variables so it can be decremented when copies is bought,
and made it global so the function can’t read it’s initialization again , so the copies
number can be updated.
> string adminChoice, ID_submitted , password_submitted;
> string userChoice, userSearch, book_buy_choice;
> userInfo user_account_variable;
We used those as a local variables to be restricted by their own function.
(1.3)
> Admin Login:
We used the if conditions and the && operator to make sure that both username
and password are correct all within the while (true) infinite loop till the correct
credentials is entered then it breaks from the loop.
> Admin Choice, Account type Choice, User Login:
We used the same while (true) infinite loop that prompts a “Invalid choice” or
“Invalid Login” every time till the correct choice/credentials is entered, then it
breaks.
> Access Menu:
We used many nested if conditions within the while (true) method.

Page 2 of 17
2. Logical Model (Algorithm)
IF userChoice = "3" THEN
IF user_account_parameter.account_type = "2" THEN
PRINT "Choose a Book to Buy(Enter Book Number) : "
GET book_buy_choice

IF book_buy_choice = "1" THEN


IF book1_copies = 0 THEN
PRINT "Can’t Buy, No Available Copies for This Book !"
CALL accessMenu with user_account_parameter
ELSE
PRINT "Done !You bought’Physics for Scientist & Engineers"
DECREMENT book1_copies

ELSE IF book_buy_choice = "2" THEN


IF book2_copies = 0 THEN
PRINT "Can’t Buy, No Available Copies for This Book !"
CALL accessMenu with user_account_parameter
ELSE
PRINT "Done !You bought 'Electrical Engineering'"
DECREMENT book2_copies
ENDIF

ELSE IF book_buy_choice = "3" THEN


IF book3_copies = 0 THEN
PRINT "Can’t Buy, No Available Copies for This Book !"
CALL accessMenu with user_account_parameter
ELSE
PRINT "Done !You bought 'Management'"
DECREMENT book3_copies
ENDIF

ELSE IF book_buy_choice = "4" THEN


IF book4_copies = 0 THEN
PRINT "Can’t Buy, No Available Copies for This Book !"
CALL accessMenu with user_account_parameter
ELSE
PRINT "Done !You bought 'Structured Programming with C++'"
DECREMENT book4_copies
ENDIF
Page 3 of 17
ELSE IF book_buy_choice = "5" THEN
IF book5_copies = 0 THEN
PRINT "Can’t Buy, No Available Copies for This Book !"
CALL accessMenu with user_account_parameter
ELSE
PRINT "Done !You bought 'Calculus for Scientists & Engineers'"
DECREMENT book5_copies
ENDIF

ELSE
PRINT " Invalid Choice !"
CALL accessMenu with user_account_parameter
ENDIF

ELSE IF user_account_parameter.account_type = "1" OR


user_account_parameter.account_type = "3"
PRINT "Only Staff Can Buy Books !"
CALL accessMenu with user_account_parameter
ENDIF

3. Process Model (Functions)

> int adminLoginValidation(string, string);


This functions is used to check if the username and password of admin is correct
or not, and if it’s correct the admin is allowed to create new user account.
> int user_account(userInfo& user_account_parameter);
This function is used by the admin to submit the new user details including
(ID – Name – Email – Password – Phone Numbers – Account Type),
after entering the details; the new user is requested to enter his ID & password
if correct; the user is transferred to the Access Menu.

Page 4 of 17
> int accessMenu(userInfo& user_account_parameter);
This function is where the user can access the features of the library;
the user can view all the books , search for a book or buy a book only if he/she
is from Staff members.

4. Coding Style

Keep It Stupid Simple. A style theory that originated in the United States Navy
which already goes back to 1960. It states that the majority of systems should be
kept as simple as possible. One can avoid needless complexity. When you're
[1]
writing code the question to ask is "can this be written in a simpler way?" .

Clean Code Guidelines:


> Use easily pronounceable names for variables and methods.
Do not use variable and method names as abbreviations. Use the full name of the
variable so it can be pronounced easily and that everyone can understand it [2]
(As in line 9 & 10 & 21 & 31 & 32 & 68 & 123 & 124 & 125).
> Better Functions.
-The tinier the better
-One function should do just one thing
-No nested control structure
-Worse are the less arguments,
[3]
Over three arguments are wrong . (As in line 23 & 61 & 114)

> Using goto, continue, etc.

This is a common debate among programmers. As with global variables, these


kinds of assumptions are generally known as poor practice. They are deemed bad as
they lead to "spaguetti code."

Page 5 of 17
If we are programming we want a linear flow. But the flow is changed by using
certain statements and contributes to a "twisted and tangled" flow.

In the past, Goto was utilized. However, when while, for, if functions were created
with the introduction of those structured programming. In general, stop using goto
unless you're confident the code would be simpler and easier to read. An example
could be to use it in nested loops.
The use of break and continue is practically identical. Use them in switches and try
making one-purpose functions so you have only one exit point [4].

> Let one function or method perform only one task.


It makes other people reading the code able to understand it or they won’t need
much time to understand, also function and method become predictable, they produce
same output with the same input [5].(As functions in line 29 & 66)

Page 6 of 17
5. Implementation
#include <iostream>
#include <string>

using namespace std;

struct userInfo
{
int phoneNumber1, phoneNumber2, phoneNumber3;
string ID, name, password, email, account_type;
};
int adminloginValidation(string, string);
int user_account(userInfo& user_account_parameter);
int accessMenu(userInfo& user_account_parameter);
int book1_copies = 10, book2_copies = 10, book3_copies = 10, book4_copies = 10,
book5_copies = 10;

int main()
{
string adminUsername, adminPassword;

adminloginValidation(adminUsername, adminPassword);

return 0;
}

int adminloginValidation(string adminUsername,string adminPassword)


{
string adminChoice;
userInfo user_account_variable;
while (true)
{

cout << " -------------------------------------------


\n";
cout << " LIBRARY MANAGEMENT SYSTEM\n";
cout << " -------------------------------------------
\n" << endl;
cout << "Enter Admin Username : ";
cin >> adminUsername;
cout << "\nEnter Admin Password : ";
cin >> adminPassword;
if (adminUsername == "lib1" && adminPassword == "librarian1") {
cout << "\n Login Successful , Welcome!\n\n"; break; }
else if (adminUsername == "lib2" && adminPassword ==
"librarian2") { cout << "\n Login Successful , Welcome!\n\n"; break; }
else if (adminUsername == "lib3" && adminPassword ==
"librarian3") { cout << "\n Login Successful , Welcome!\n\n"; break; }
else if (adminUsername == "lib4" && adminPassword ==
"librarian4") { cout << "\n Login Successful , Welcome!\n\n"; break; }
else { cout << "\n Invalid Login , Please Try Again ! \n\n"; }

Page 7 of 17
}
while (true)
{
cout << " -------------------------------------------
\n";
cout << " LIBRARY MANAGEMENT SYSTEM\n";
cout << " -------------------------------------------
\n" << endl;
cout << "1.Create User Account\n2.Exit\n\n";
cout << "Choice : "; cin >> adminChoice;
if (adminChoice == "1") break;
else if (adminChoice == "2") { cout << endl; return 0; }
else cout << "\n Invalid choice !\n";
}

user_account(user_account_variable);

return 0;
}

int user_account(userInfo& user_account_parameter)


{
string ID_submitted, password_submitted;
cout << " -------------------------------------------\n";
cout << " LIBRARY MANAGEMENT SYSTEM\n";
cout << " -------------------------------------------\n";
cout << " REGISTERATION\n";
cout << " -------------------------------------------\n";
cout << endl;
cout << " > ID : \n\n ";
cin >> user_account_parameter.ID;
cout << "\n > Name : \n\n ";
cin >> user_account_parameter.name;
cout << "\n > Email : \n\n ";
cin >> user_account_parameter.email;
cout << "\n > Password : \n\n ";
cin >> user_account_parameter.password;
cout << "\n > Phone Number : \n";
cout << "\n 1st Number : "; cin >> user_account_parameter.phoneNumber1;
cout << "\n 2nd Number : "; cin >> user_account_parameter.phoneNumber2;
cout << "\n 3rd Number : "; cin >> user_account_parameter.phoneNumber3;

while (true)
{
cout << "\n Account type ( 1.Student | 2.Staff | 3.Guest ):\n\n
";
cin >> user_account_parameter.account_type; cout << endl;
if (user_account_parameter.account_type == "1") { cout << "
Student\n\n"; break; }
else if (user_account_parameter.account_type == "2") { cout << "
Staff\n\n"; break; }
else if (user_account_parameter.account_type == "3") { cout << "
Guest\n\n"; break; }
else cout << " Invalid Choice !\n";
}

cout << " -------------------------------------------\n";

Page 8 of 17
cout << " LIBRARY MANAGEMENT SYSTEM\n";
cout << " -------------------------------------------\n";
cout << " LOGIN\n";
cout << " -------------------------------------------\n";
cout << endl;
while (true)
{
cout << "Enter User ID : ";
cin >> ID_submitted;
cout << "\nEnter User Password : ";
cin >> password_submitted;
if (ID_submitted == user_account_parameter.ID &&
password_submitted == user_account_parameter.password)
{
cout << "\n\n Login Successful !\n\n"; break;
}
else cout << "\n\n Invalid Login , Please Try Again ! \n\n";
}
accessMenu(user_account_parameter);

return 0;
}

int accessMenu(userInfo& user_account_parameter)


{
string userChoice;
string userSearch;
string book_buy_choice;

cout << " -------------------------------------------\n";


cout << " LIBRARY MANAGEMENT SYSTEM\n";
cout << " -------------------------------------------\n";
cout << " ACCESS MENU\n";
cout << " -------------------------------------------\n";
cout << endl;
cout << "1.Show all Books\n2.Search a Book\n3.Buy a Book\n4.Exit\n\n";
cout << "Choice : "; cin >> userChoice; cout << endl;
if (userChoice == "1" || userChoice == "2" || userChoice == "3" ||
userChoice == "4")
{
if (userChoice == "1")
{
cout << " -----------------------------------
--------\n";
cout << " LIBRARY MANAGEMENT
SYSTEM\n";
cout << " -----------------------------------
--------\n";
cout << " BOOKS LIST\n";
cout << " ---------------------------------
----------\n"; cout << endl;
cout << " BOOK 1 :\n\n Title : Physics for Scientists &
Engineers\n Edition : 6th edition\n Catogery : Science\n Production Year : 2004\n
Number of Copies : " << book1_copies << "\n\n";

Page 9 of 17
cout << " BOOK 2 :\n\n Title : Electrical Engineering\n
Edition : 5th edition\n Catogery : Science\n Production Year : 2011\n Number of
Copies : " << book2_copies << "\n\n";
cout << " BOOK 3 :\n\n Title : Management\n Edition : 11th
edition\n Catogery : Business\n Production Year : 2012\n Number of Copies : " <<
book3_copies << "\n\n";
cout << " BOOK 4 :\n\n Title : Structured Programming with
C++\n Edition : 1st edition\n Catogery : Computer Science\n Production Year :
2012\n Number of Copies : " << book4_copies << "\n\n";
cout << " BOOK 5 :\n\n Title : Calculus for Scientists &
Engineers\n Edition : 1st edition\n Catogery : Mathematics\n Production Year :
2013\n Number of Copies : " << book5_copies << "\n\n";

accessMenu(user_account_parameter);
}
else if (userChoice == "2")
{
while (true)
{
cout << "\nEnter Book's Title/Production year :
";
getline(cin, userSearch);
getline(cin, userSearch);
if (userSearch == "Physics for Scientists &
Engineers")
{
cout << "\n\n BOOK 1 :\n\n Title : Physics
for Scientists & Engineers\n Edition : 6th edition\n Catogery : Science\n
Production Year : 2004\n Number of Copies : " << book1_copies << "\n\n"; break;
}
else if (userSearch == "Electrical Engineering" ||
userSearch == "2011")
{
cout << "\n\n BOOK 2 :\n\n Title :
Electrical Engineering\n Edition : 5th edition\n Catogery : Science\n Production
Year : 2011\n Number of Copies : " << book2_copies << "\n\n"; break;
}
else if (userSearch == "Management")
{
cout << "\n\n BOOK 3 :\n\n Title :
Management\n Edition : 11th edition\n Catogery : Business\n Production Year :
2012\n Number of Copies : " << book3_copies << "\n\n"; break;
}
else if (userSearch == "Structured Programming
with C++")
{
cout << "\n\n BOOK 4 :\n\n Title :
Structured Programming with C++\n Edition : 1st edition\n Catogery : Computer
Science\n Production Year : 2012\n Number of Copies : " << book4_copies <<
"\n\n"; break;
}
else if (userSearch == "Calculus for Scientists &
Engineers" || userSearch == "2013")
{
cout << "\n\n BOOK 5 :\n\n Title : Calculus
for Scientists & Engineers\n Edition : 1st edition\n Catogery : Mathematics\n
Production Year : 2013\n Number of Copies : " << book5_copies << "\n\n"; break;

Page 10 of 17
}
else if(userSearch == "2012")
{
cout << "\n\n BOOK 3 :\n\n Title :
Management\n Edition : 11th edition\n Catogery : Business\n Production Year :
2012\n Number of Copies : " << book3_copies << "\n\n";
cout << " BOOK 4 :\n\n Title : Structured
Programming with C++\n Edition : 1st edition\n Catogery : Computer Science\n
Production Year : 2012\n Number of Copies : " << book4_copies << "\n\n"; break;
}
else cout << "\n Invalid Search , Please Try
Again !\n";
}
accessMenu(user_account_parameter);
}
else if (userChoice == "3")
{
if (user_account_parameter.account_type == "2")
{
cout << " BOOK 1 :\n\n Title : Physics for
Scientists & Engineers\n Edition : 6th edition\n Catogery : Science\n Production
Year : 2004\n Number of Copies : " << book1_copies << "\n\n";
cout << " BOOK 2 :\n\n Title : Electrical
Engineering\n Edition : 5th edition\n Catogery : Science\n Production Year :
2011\n Number of Copies : " << book2_copies << "\n\n";
cout << " BOOK 3 :\n\n Title : Management\n
Edition : 11th edition\n Catogery : Business\n Production Year : 2012\n Number of
Copies : " << book3_copies << "\n\n";
cout << " BOOK 4 :\n\n Title : Structured
Programming with C++\n Edition : 1st edition\n Catogery : Computer Science\n
Production Year : 2012\n Number of Copies : " << book4_copies << "\n\n";
cout << " BOOK 5 :\n\n Title : Calculus for
Scientists & Engineers\n Edition : 1st edition\n Catogery : Mathematics\n
Production Year : 2013\n Number of Copies : " << book5_copies << "\n\n";
cout << " Choose a Book to Buy (Enter Book Number)
: "; cin >> book_buy_choice; cout << "\n\n";
if (book_buy_choice == "1")
{
if(book1_copies==0)
{
cout << "\n Can't Buy , No
Available Copies for This Book !\n\n"; accessMenu(user_account_parameter);
}
else { cout << "\n Done ! You bought
'Physics for Scientists & Engineers'\n\n"; book1_copies--; }
}
else if(book_buy_choice == "2")
{
if (book2_copies == 0)
{
cout << "\n Can't Buy , No
Available Copies for This Book !\n\n"; accessMenu(user_account_parameter);
}
else { cout << "\n Done ! You bought
'Electrical Engineering'\n\n"; book2_copies--; }
}
else if(book_buy_choice == "3")

Page 11 of 17
{
if (book3_copies == 0)
{
cout << "\n Can't Buy , No
Available Copies for This Book !\n\n"; accessMenu(user_account_parameter);
}
else { cout << "\n Done ! You bought
'Management'\n\n"; book3_copies--; }
}
else if(book_buy_choice =="4")
{
if (book4_copies == 0)
{
cout << "\n Can't Buy , No
Available Copies for This Book !\n\n"; accessMenu(user_account_parameter);
}
else { cout << "\n Done ! You bought
'Structured Programming with C++'\n\n"; book4_copies--; }
}
else if(book_buy_choice =="5")
{
if (book5_copies == 0)
{
cout << "\n Can't Buy , No
Available Copies for This Book !\n\n"; accessMenu(user_account_parameter);
}
else { cout << "\n Done ! You bought
'Calculus for Scientists & Engineers'\n\n"; book5_copies--; }
}
else
{
cout << "\n Invalid Choice !\n\n";
accessMenu(user_account_parameter);
}
accessMenu(user_account_parameter);
}
else if(user_account_parameter.account_type =="1" ||
user_account_parameter.account_type =="3")
{
cout << "\n Only Staff Can Buy Books !\n\n";
accessMenu(user_account_parameter);
}
}
else if (userChoice == "4") return 0;

}
else
{
cout << "\n Invalid Choice !\n\n";
accessMenu(user_account_parameter);
}
return 0;
}

Page 12 of 17
6. Testing

Page 13 of 17
Page 14 of 17
sada

Page 15 of 17
--------------------------------------------------------------------------------------------------------------------------------------------------

Page 16 of 17
References:
[1] X-Team https://x-team.com/blog/principles-clean-code/
[2] SimpleProgrammer https://simpleprogrammer.com/clean-code-principles-better-programmer/
[3] ButterFly https://www.butterfly.com.au/blog/website-development/clean-high-quality-code-a-guide-on-how-
to-become-a-better-programmer

[4] FreeCodeCamp https://www.freecodecamp.org/news/how-to-write-clean-code-in-c/

[5] AlexDeveroBlog https://blog.alexdevero.com/6-simple-tips-writing-clean-code/

Page 17 of 17

You might also like