Library Management System With C++
Library Management System With C++
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
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
ELSE
PRINT " Invalid Choice !"
CALL accessMenu with user_account_parameter
ENDIF
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?" .
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].
Page 6 of 17
5. Implementation
#include <iostream>
#include <string>
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;
}
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;
}
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";
}
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;
}
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
Page 17 of 17