




















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





















ļ§ Methods: Library() (constructor), loadBooks() , saveBooks() , addBook() , displayBooks() , searchBook() , issueBook() , returnBook(). ļ§ This class manages the collection of Book objects, handling operations like adding, displaying, searching, issuing, and returning books. It also handles persistence by loading and saving book data to a file.
- User Class (Conceptual): ļ§ Attributes: username (string), password (string), role (string). ļ§ While not explicitly defined as a C++ class in the provided code, the User concept is implemented using global arrays ( usernames , passwords , roles ) and functions ( loadUsers , saveUser , signUp , login ). For the purpose of the class diagram, it represents the logical entity responsible for user authentication and authorization.
ļ§ Composition: The Library class contains an array of Book objects, indicating that Book objects are part of the Library and their lifecycle is managed by the Library. UML Sequence Diagram The provided UML sequence diagram illustrates the interactions between a User, Product1, Product2, and Invoice objects in a simplified scenario. This diagram focuses on the flow of control and messages exchanged between these objects.
ļ§ Object (name): Represents an instance of a class, denoted by a rectangle with the object's name (e.g., product1 , invoice ). ļ§ User (user): Represents an actor initiating the interactions, typically a human user.
User Instantiates Product1: The User initiates the process by sending an instantiate message to create product1. This signifies the creation of the first product object.
User Instantiates Product2: Following the creation of product1, the User sends another instantiate message to create product2, indicating the creation of a second product object.
User Instantiates Invoice: The User then instantiates an invoice object, which will likely be used to manage the products.
User Adds Product to Invoice (First Product): The User sends an add (...) message to the invoice object. This implies adding product1 (or some representation of it) to the invoice. ļ§ Invoice Gets Price from Product1: As part of processing the add operation, the invoice object sends a getPrice () message to product1. This is a function call to retrieve the price of product1. ļ§ Product1 Returns Price: product1 processes the getPrice () request and returns the price to the invoice object. The blue rectangle on product1ās lifeline indicates its active period during this operation.
User Adds Product to Invoice (Second Product): The User sends another add (...) message to the invoice object, likely to add product2. ļ§ Invoice Gets Price from Product2: Similar to the first product, the invoice object sends a getPrice () message to product2 to retrieve its price. ļ§ Product2 Returns Price: product2 returns its price to the invoice object.
User Prints Invoice: Finally, the User sends a print(...) message to the invoice object, indicating a request to display or generate a summary of the invoice, which now includes both products. This sequence diagram effectively visualizes the dynamic behavior of the system, showing the order of operations and how different objects collaborate to achieve a specific task, such as creating an invoice with multiple products and then printing it.
cout << "."; cout.flush(); pauseSeconds(1); } cout << " Done!\n"; pauseSeconds(1); clearScreen(); } // ======================== Book Class ======================== class Book { private: int id; string title; string author; bool isIssued; public: Book() : id(0), isIssued(false) {} void reset() { id = 0; title = ""; author = ""; isIssued = false; } void input() { cout << "Enter Book ID: ";
cin >> id; cin.ignore(); cout << "Enter Title: "; getline(cin, title); cout << "Enter Author: "; getline(cin, author); isIssued = false; } void display() const { cout << "\nID: " << id << "\nTitle: " << title << "\nAuthor: " << author << "\nIssued: " << (isIssued? "Yes" : "No") << "\n"; } int getID() const { return id; } void issue() { if (!isIssued) { isIssued = true; cout << "Book issued successfully.\n"; } else { cout << "Book is already issued.\n"; } } void returnBook() { if (isIssued) {
string authorStr = line.substr(pos2 + 1, pos3 - pos2 - 1); string issuedStr = line.substr(pos3 + 1); if (idStr.empty() || titleStr.empty() || authorStr.empty() || issuedStr.empty()) { reset(); return; } istringstream issId(idStr); istringstream issIssued(issuedStr); int idVal = 0, issuedVal = 0; if (!(issId >> idVal)) { reset(); return; } if (!(issIssued >> issuedVal)) { reset(); return; } id = idVal; title = titleStr; author = authorStr; isIssued = (issuedVal != 0); } }; // ======================== Library Class ========================
class Library { private: Book books[100]; int count public: Library() : count(0) {} void loadBooks() { count = 0; ifstream in("books.txt"); string line; while (getline(in, line) && count < 100) { books[count].loadFromFile(line); if (books[count].getID() != 0) { count++; } } in.close(); } void saveBooks() const { ofstream out("books.txt"); for (int i = 0; i < count; i++) { books[i].saveToFile(out); } out.close(); } void addBook() { if (count < 100) {
void issueBook(int id) { for (int i = 0; i < count; ++i) { if (books[i].getID() == id) { books[i].issue(); saveBooks(); return; } } cout << "Book not found.\n"; } void returnBook(int id) { for (int i = 0; i < count; ++i) { if (books[i].getID() == id) { books[i].returnBook(); saveBooks(); return; } } cout << "Book not found.\n"; } }; // ======================== User Management ======================== string usernames[10], passwords[10], roles[10]; int userCount = 0; void loadUsers() { userCount = 0;
ifstream in("users.txt"); string line; while (getline(in, line) && userCount < 10) { size_t pos1 = line.find("|"); size_t pos2 = line.find("|", pos1 + 1); if (pos1 == string::npos || pos2 == string::npos) { continue; } usernames[userCount] = line.substr(0, pos1); passwords[userCount] = line.substr(pos1 + 1, pos2 - pos1 - 1); roles[userCount] = line.substr(pos2 + 1); userCount++; } in.close(); } void saveUser(const string& uname, const string& pass, const string& role) { ofstream out("users.txt", ios::app); out << uname << "|" << pass << "|" << role << "\n"; out.close(); } void signUp() { if (userCount >= 10) { cout << "User limit reached.\n"; return; } string uname, pass, role; cout << "Signup as (manager/user): "; cin >> role;
cin >> pass; for (int i = 0; i < userCount; i++) { if (usernames[i] == uname && passwords[i] == pass) { cout << "Login successful!\n"; pauseSeconds(1); clearScreen(); return roles[i]; } } cout << "Invalid credentials.\n"; pauseSeconds(1); clearScreen(); return login(); // Recursive call until successful login } // ======================== Unified Menu for Both Roles ======================== void commonMenu(Library& lib) { int choice, id; while (true) { cout << "--- Menu ---\n"; cout << "1. Add Book\n"; cout << "2. Display All Books\n"; cout << "3. Search Book by ID\n"; cout << "4. Issue Book\n"; cout << "5. Return Book\n"; cout << "6. Logout\n"; cout << "Enter choice: "; cin >> choice;
clearScreen(); switch (choice) { case 1: lib.addBook(); break; case 2: lib.displayBooks(); break; case 3: cout << "Enter Book ID: "; cin >> id; lib.searchBook(id); break; case 4: cout << "Enter Book ID: "; cin >> id; lib.issueBook(id); break; case 5: cout << "Enter Book ID: "; cin >> id; lib.returnBook(id); break; case 6: clearScreen(); return; default: cout << "Invalid choice.\n"; }