0% found this document useful (0 votes)
6 views3 pages

dsalab3

Uploaded by

shalom.daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

dsalab3

Uploaded by

shalom.daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <string>
using namespace std;

class Book {
public:
string title;
string author;
string isbn;

// Constructor to initialize a Book object


Book(string t, string a, string i) {
title = t;
author = a;
isbn = i;
}

// Method to display book details


void displayInfo() {
cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
cout << "ISBN: " << isbn << endl;
}
};

class Library {
public:
Book* books[5]; // Array to store book pointers (maximum 5 books for
simplicity)
int count;

// Constructor to initialize the Library with no books


Library() {
count = 0;
}

// Method to add a book to the library


void addBook(string title, string author, string isbn) {
if (count < 5) {
books[count] = new Book(title, author, isbn);
count++;
cout << "Book added successfully!" << endl;
} else {
cout << "Library is full. Cannot add more books." << endl;
}
}

// Linear Search method to find a book by title


void searchByTitle(string title) {
bool found = false;
for (int i = 0; i < count; i++) {
if (books[i]->title == title) {
cout << "Book found:" << endl;
books[i]->displayInfo();
found = true;
break;
}
}
if (!found) {
cout << "Book not found in the library." << endl;
}
}

// Method to display all books in the library


void displayAllBooks() {
if (count == 0) {
cout << "No books available in the library." << endl;
} else {
cout << "\nBooks in Library:\n";
for (int i = 0; i < count; i++) {
books[i]->displayInfo();
cout << "------------------------" << endl;
}
}
}
};

int main() {
Library library;
int choice;

while (true) {
cout << "\nLibrary Management System" << endl;
cout << "1. Add Book" << endl;
cout << "2. Display All Books" << endl;
cout << "3. Search Book by Title" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(); // To ignore newline character from previous input

switch (choice) {
case 1:
// Add a new book
{
string title, author, isbn;
cout << "Enter book title: ";
getline(cin, title);
cout << "Enter author name: ";
getline(cin, author);
cout << "Enter ISBN: ";
getline(cin, isbn);
library.addBook(title, author, isbn);
break;
}
case 2:
// Display all books
library.displayAllBooks();
break;
case 3:
// Search for a book by title
{
string searchTitle;
cout << "Enter book title to search: ";
getline(cin, searchTitle);
library.searchByTitle(searchTitle);
break;
}
case 4:
// Exit the program
cout << "Exiting the Library Management System." << endl;
return 0;
default:
cout << "Invalid choice. Please try again." << endl;
}
}

return 0;
}

You might also like