You are on page 1of 5

#include <iostream>

#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

struct Content {
string id;
};

struct Book {
string id;
string author;
string title;
string ageRange;
vector<string> illustrations;
vector<string> keywords;
};

struct TreeNode {
Book book;
vector<Content> contents;
};

using Tree = map<string, TreeNode>;

string readLine() {
string line;
getline(cin, line);
return line;
}

bool matchesPattern(const string& str, const string& pattern) {


string strLower = str;
transform(strLower.begin(), strLower.end(), strLower.begin(), ::tolower);
string patternLower = pattern;
transform(patternLower.begin(), patternLower.end(),
patternLower.begin(), ::tolower);
return strLower.find(patternLower) != string::npos;
}

void searchBooksByPattern(const string& pattern, const Tree& tree) {


bool found = false;
for (const auto& nodePair : tree) {
const TreeNode& node = nodePair.second;
if (matchesPattern(node.book.title, pattern)) {
cout << "Author: " << node.book.author << endl;
cout << "Title: " << node.book.title << endl;
cout << "Age Range: " << node.book.ageRange << endl;
cout << endl;
found = true;
}
}

if (!found) {
cout << "No books matching the pattern found." << endl;
}
}

void searchBooksByKeyword(const string& keyword, const Tree& tree) {


bool found = false;
for (const auto& nodePair : tree) {
const TreeNode& node = nodePair.second;
if (find(node.book.keywords.begin(), node.book.keywords.end(), keyword) !=
node.book.keywords.end()) {
cout << "Author: " << node.book.author << endl;
cout << "Title: " << node.book.title << endl;
cout << "Age Range: " << node.book.ageRange << endl;
cout << endl;
found = true;
}
}

if (!found) {
cout << "No books matching the keyword found." << endl;
}
}

bool isUser(const string& id, const string& password) {


return (id == "Salah" && password == "BABY") ||
(id == "Ayoub" && password == "BENYICHE") ||
(id == "Abderrahim" && password == "ELHADI");
}

void editBookInfo(const string& userId, const string& userPassword, Tree& tree) {


cout << "User ID: ";
string id = readLine();
cout << "Password: ";
string password = readLine();

if (!isUser(id, password) || (id != userId || password != userPassword)) {


cout << "Invalid user credentials!" << endl;
return;
}

cout << "Enter the Book ID to edit: ";


string bookId = readLine();

auto bookIt = tree.find(bookId);

if (bookIt != tree.end()) {
cout << "Book information edited successfully!" << endl;
} else {
cout << "Book not found with the provided ID." << endl;
}
}

void loadDataFromFile(Tree& tree) {


ifstream inputFile("book_data.txt");
if (!inputFile.is_open()) {
cout << "Error opening file!" << endl;
return;
}
string line;
string bookId = "B001";
while (getline(inputFile, line)) {
stringstream ss(line);
Book book;
book.id = bookId;
ss >> book.author >> book.title >> book.ageRange;

string keyword;
while (ss >> keyword) {
book.keywords.push_back(keyword);
}

TreeNode node;
node.book = book;
tree[bookId] = node;
bookId[3]++;
}

inputFile.close();
}

int main() {
Tree tree;

loadDataFromFile(tree);

TreeNode computerBookNode;
computerBookNode.book.id = "B002";
computerBookNode.book.author = "Matthew Justice";
computerBookNode.book.title = "How Computers Really Work: A Hands-On Guide to
the Inner Workings of the Machine";
computerBookNode.book.ageRange = "14 and up";
computerBookNode.book.illustrations = {"Cover2.jpg", "Inside3.jpg",
"Inside4.jpg"};
computerBookNode.book.keywords = {
"Computer", "Architecture", "Digital Logic", "Processor", "Computer
Science",
"Technology", "Hardware", "Programming", "Circuitry", "Electronics"
};
tree["B002"] = computerBookNode;

TreeNode kissQuotientBookNode;
kissQuotientBookNode.book.id = "B003";
kissQuotientBookNode.book.author = "Helen Hoang";
kissQuotientBookNode.book.title = "The Kiss Quotient";
kissQuotientBookNode.book.ageRange = "Adult";
kissQuotientBookNode.book.keywords = {
"Romance", "Contemporary", "Love Story", "Relationships", "Autism
Spectrum",
"Diversity", "Vietnamese-American", "Adult Fiction", "Emotional",
"Heartwarming"
};
tree["B003"] = kissQuotientBookNode;

TreeNode treasureIslandBookNode;
treasureIslandBookNode.book.id = "B004";
treasureIslandBookNode.book.author = "Robert Louis Stevenson";
treasureIslandBookNode.book.title = "Treasure Island";
treasureIslandBookNode.book.ageRange = "12 and up";
treasureIslandBookNode.book.keywords = {
"Adventure", "Pirates", "Treasure Hunt", "Classic Literature", "Action",
"Coming of Age", "Robert Louis Stevenson", "High Seas", "Island", "Buried
Treasure"
};
tree["B004"] = treasureIslandBookNode;

TreeNode duneBookNode;
duneBookNode.book.id = "B005";
duneBookNode.book.author = "Frank Herbert";
duneBookNode.book.title = "Dune";
duneBookNode.book.ageRange = "14 and up";
duneBookNode.book.keywords = {
"Science Fiction", "Space Opera", "Fantasy", "Dystopian", "Politics",
"Religion", "Epic", "Societal Conflict", "Frank Herbert", "Desert Planet"
};
tree["B005"] = duneBookNode;

TreeNode worldWarZBookNode;
worldWarZBookNode.book.id = "B006";
worldWarZBookNode.book.author = "Max Brooks";
worldWarZBookNode.book.title = "World War Z";
worldWarZBookNode.book.ageRange = "Adult";
worldWarZBookNode.book.keywords = {
"Zombies", "Apocalypse", "Horror", "Survival", "Pandemic", "Global",
"Oral History", "Max Brooks", "Post-Apocalyptic", "Worldwide Conflict"
};
tree["B006"] = worldWarZBookNode;

TreeNode spyBookNode;
spyBookNode.book.id = "B007";
spyBookNode.book.author = "John le Carré";
spyBookNode.book.title = "The Spy Who Came In From the Cold";
spyBookNode.book.ageRange = "Adult";
spyBookNode.book.keywords = {
"Espionage", "Cold War", "Spy", "Thriller", "Intelligence", "John le
Carré",
"Undercover", "Political Intrigue", "Mystery", "Suspense"
};
tree["B007"] = spyBookNode;

while (true) {
cout << "1. Search books by pattern" << endl;
cout << "2. Search books by keyword" << endl;
cout << "3. Edit book information" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";

int choice;
cin >> choice;
cin.ignore();

switch (choice) {
case 1:
cout << "Enter the pattern: ";
searchBooksByPattern(readLine(), tree);
break;
case 2:
cout << "Enter the keyword: ";
searchBooksByKeyword(readLine(), tree);
break;
case 3:
editBookInfo("Salah", "BABY", tree);
editBookInfo("Ayoub", "BENYICHE", tree);
editBookInfo("Abderrahim", "ELHADI", tree);
break;
case 4:
cout << "Exiting the program..." << endl;
return 0;
default:
cout << "Invalid choice! Please try again." << endl;
}

cout << endl;


}

return 0;
}

You might also like