FunnyVALENTINE
D4C LOVE TRAIN
- Joined
- Apr 19, 2026
- Posts
- 1,659
- Reputation
- 1,741
C++:
#include<iostream>
#include<vector>
#include<string>
#include<unordered_map>
#include<cctype>
#include<filesystem>
#include<algorithm>
#include<random>
#include<bitset>
#include<set>
#include<string_view>
#include<memory>
#include<cmath>
#include<cctype>
#include<fstream>
using std::vector;
using std::string;
using std::unordered_map;
using std::bitset;
using std::cout;
using std::cin;
using std::cerr;
class Book
{
public:
int id{};
string title{};
string author{};
int year{};
bool available{};
};
class Library
{
public:
vector<Book> collection =
{
{1,"The Hobbit","J.R.R. Tolkien", 1937, true},
{2,"1984", "George Orwell", 1949, true},
{3,"To Kill A Mockingbird", "Harper Lee", 1960, true},
{4, "The Great Gatsby", "F.Scott Fitzgerald", 1925,true},
{5, "Pride and Prejudice", "Jane Austen", 1813, true},
{6, "The Cather In The Rye", "J.D.Salinger", 1951, true},
{7, "The Alchemist", "Paulo Coelho", 1988, true},
{8, "The Da Vinci Code", "Dan Brown", 2003, true},
{9, "Clean Code", "Robert C Martin", 2008, true},
{10, "The Pragmatic Programmer", "Andrew Hunt & David Thomas", 1999, true}
};
};
bool userExists(const string& username);
class User
{
public:
string username{};
string password{};
bool existing_user{};
void input()
{
bool valid_name{};
do
{
existing_user = false;
valid_name = true;
cout << "Enter Your UserName: ";
std::getline(cin >> std::ws, username);
if (username.length() < 8 or username.length() > 20)
{
cout << "Username should not be less than 8 or more than 20 characters\n";
valid_name = false;
}
else if (userExists(username))
{
cout << "UserName already exists\n";
cout << "Choose a different username or If you have an account login to it\n";
cout << '\n';
valid_name = false;
existing_user = true;
return;
}
for (char c : username)
{
if (!std::isalnum(static_cast<unsigned char>(c)))
{
cout << "Username Should only contain Alphabets or numericals\n";
valid_name = false;
break;
}
}
} while (!valid_name);
bool valid_password{};
bool is_upper{};
bool is_digit{};
bool is_symbol{};
do
{
valid_password = true;
is_upper = false;
is_digit = false;
is_symbol = false;
cout << "Enter a Password Between 8-20 characters: ";
cin >> password;
if (password.length() < 8 or password.length() > 20)
{
cout << "Password should not be above 8 or 20 characters\n";
valid_password = false;
}
for (char c : password)
{
if (std::isupper(static_cast<unsigned char>(c)))
{
is_upper = true;
}
if (std::isdigit(static_cast<unsigned char>(c)))
{
is_digit = true;
}
if (std::ispunct(static_cast<unsigned char>(c)))
{
is_symbol = true;
}
}
if (!is_upper)
{
cout << "Password must contain alteast one uppercase letter\n";
valid_password = false;
}
if (!is_digit)
{
cout << "Password must contain alteast one digit\n";
valid_password = false;
}
if (!is_symbol)
{
cout << "Password must contain alteast one digit\n";
valid_password = false;
}
} while (!valid_password);
}
};
bool bookExists(int id);
int main()
{
User a;
int option{};
do
{
cout << "------------------MAIN MENU------------------\n";
cout << "1.Sign Up and Register to start borrowing books\n";
cout << "2.Login and Explore the Library to borrow and exchange books\n";
cout << "3.Exit\n";
cout << "Enter a number between 1-3 to begin operation: ";
cin >> option;
if (option == 1)
{
a.input();
if (!a.existing_user)
{
std::ofstream file{ "login.txt" };
if (!file)
{
cout << "File cannot be opened for writing\n";
}
file << "Username: " << a.username << '\n';
file << "Password: " << a.password << '\n';
file.close();
}
}
else if (option == 2)
{
int count{};
string s_ID{};
std::ifstream file3{ "borrowed_books.json" };
if (!file3)
{
cerr << "File will be created once you borrow your first book\n";
}
while (std::getline(file3, s_ID))
{
if (s_ID.find("\"id\"") != string::npos)
{
++count;
}
}
string stored_username{};
string stored_password{};
string Username{};
string Password{};
std::ifstream file{ "login.txt" };
if (!file)
{
cout << "File cannot be opened for reading\n";
}
cout << "Enter Your Username\n";
std::getline(cin >> std::ws, Username);
cout << "Enter Your Password\n";
cin >> Password;
while (std::getline(file, stored_username) && std::getline(file, stored_password))
{
if (stored_username == "Username: " + Username && stored_password == "Password: " + Password)
{
cout << "Login Successful\n";
bool logged_in{ true };
int choice{};
Library library{};
Book book;
while (logged_in)
{
cout << "----------MENU----------\n";
cout << "------------------------\n";
cout << "1.View Books\n";
cout << "2.Check Book Availability\n";
cout << "3.Search and Borrow Books\n";
cout << "4.Return Books\n";
cout << "5.To Exit\n";
cout << "Enter a Number between 1-4 to beging Operation: ";
cin >> choice;
switch (choice)
{
case 1:
{
for (const auto& book : library.collection)
{
cout << book.id << ". "
<< book.title
<< " by "
<< book.author
<< '\n';
}
break;
}
case 2:
{
for (const auto& book : library.collection)
{
cout << book.title << " by "
<< book.author << " is "
<< (book.available ? "Available" : "Not Available")
<< '\n';
}
break;
}
case 3:
{
int search_id{};
cout << "Enter the search Id of the book you want to borrow: ";
cin >> search_id;
auto it = std::find_if(library.collection.begin(), library.collection.end(), [search_id](const Book& book)
{
return book.id == search_id;
});
if (it == library.collection.end())
{
cout << "Book Not Found\n";
break;
}
if (count == 5)
{
cout << "You Already have 5 books\n";
break;
}
if (bookExists(it->id))
{
cout << "Book Already Exists In Your Collection!\n";
break;
}
if (it->available)
{
std::ofstream file{ "borrowed_books.json" };
file << "{\n";
file << " \"books\": [\n";
file << " {\n";
file << " \"id\": " << it->id << "," << '\n';
file << " \"title\": \"" << it->title << "\"" << "," << '\n';
file << " \"author\": \"" << it->author << "\"" << "," << '\n';
file << " \"year\": " << it->year << "," << '\n';
it->available = false;
file << " \"available\": " << std::boolalpha << it->available << '\n';
file << " },\n";
file << " ]\n";
file << "}\n";
file.close();
string line{};
vector<string>lines{};
char option{};
int num{ 4 };
std::ifstream file1{ "borrowed_books.json" };
while (std::getline(file1, line))
{
lines.push_back(line);
}
file1.close();
for (size_t i = 0; i < 4; ++i)
{
cout << "You can " << num << " more books continue ? : ";
cin >> option;
if (option == 'y' || option == 'Y')
{
for (size_t j = 0; j < lines.size(); ++j)
{
if (lines[j].find(']') != std::string::npos)
{
cout << "Enter the search Id of the book you want to borrow: ";
cin >> search_id;
auto it = std::find_if(library.collection.begin(), library.collection.end(), [search_id](const Book& book)
{
return book.id == search_id;
});
if (it == library.collection.end())
{
cout << "Book Not Found\n";
break;
}
it->available = false;
lines.insert(lines.begin() + j,
{
" {",
" \"id\": " + std::to_string(it->id) + ",",
" \"title\": \"" + it->title + "\",",
" \"author\": \"" + it->author + "\",",
" \"year\": " + std::to_string(it->year) + ",",
" \"available\": " + std::string(it->available ? "true" : "false"),
" },"
});
num -= 1;
std::ofstream file{ "borrowed_books.json" };
for (const auto& x : lines)
{
file << x << '\n';
}
break;
}
}
}
else
{
break;
}
}
}
else
{
cout << "Book Not Available\n";
}
break;
}
case 4:
{
char choiceYN{};
do
{
int book_ID{};
cout << "Enter the Id of the book you wanna return: ";
cin >> book_ID;
if (bookExists(book_ID))
{
vector<string>lines{};
string line{};
std::ifstream file{ "borrowed_books.json" };
while (std::getline(file, line))
{
lines.push_back(line);
}
for (size_t i = 1;i < lines.size(); ++i)
{
if (lines[i].find("\"id\"") != string::npos)
{
string stored_id = lines[i];
stored_id = stored_id.substr(12);
stored_id.erase(stored_id.size() - 1);
int new_stored_id = stoi(stored_id);
if (new_stored_id == book_ID)
{
lines.erase(lines.begin() + i - 1, lines.begin() + i + 6);
break;
}
}
}
std::ofstream file1{ "borrowed_books.json" };
for (const auto& x : lines)
{
file1 << x << '\n';
}
}
cout << "Do you want to continue ?? (q to quit): ";
cin >> choiceYN;
} while (choiceYN != 'q');
break;
}
case 5:
{
logged_in = false;
break;
}
default:
{
cout << "Invalid Option\n";
}
}
}
}
}
}
else if (option == 3)
{
cout << "See You Again\n";
}
}
while (option != 3);
return 0;
}
bool userExists(const string& username)
{
string stored_username{};
std::ifstream file{ "login.txt" };
if (!file)
{
cerr << "file cannot be opened for reading\n";
return false;
}
while (std::getline(file, stored_username))
{
if (stored_username == "Username: " + username)
{
cout << "Checking.....\n";
return true;
}
}
return false;
}
bool bookExists(int id)
{
string stored_id{};
std::ifstream file{ "borrowed_books.json" };
std::getline(file, stored_id);
std::getline(file, stored_id);
while (std::getline(file, stored_id))
{
if (stored_id.find("\"id\"") != std::string::npos)
{
stored_id = stored_id.substr(12);
stored_id.erase(stored_id.size() - 1);
int new_id = std::stoi(stored_id);
if (new_id == id)
{
cout << "Book Found\n";
return true;
}
}
}
return false;
}
Today I just built a solid Library system
.Very soon I'll Work with real databases

Say No To VibeCoding

Say NO to AI

If you build projects with AI my nigga just rope at this point you are not a real developer
Very Very Soon I'll build something like facebook and become a billionaire
HAHAHAHAHAHAHAAHA KEEP ROTTING INCELS
I'LL GET INVESTMENTS FOR MY APP
AND WILL BECOME THE KING OF SILICON VALLEY


.