Certainly! Here's an updated version of the code that includes functions to add and delete books in addition to the existing login functionality:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Book {
private:
string title;
string author;
int year;
public:
Book(string t, string a, int y) {
title = t;
author = a;
year = y;
}
string getTitle() {
return title;
}
string getAuthor() {
return author;
}
int getYear() {
return year;
}
};
class Library {
private:
vector<Book> books;
public:
void addBook(Book book) {
books.push_back(book);
}
void deleteBook(string title) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getTitle() == title) {
books.erase(books.begin() + i);
cout << "Book '" << title << "' deleted." << endl;
return;
}
}
cout << "Book with title '" << title << "' not found." << endl;
}
void displayBooks() {
cout << "Library Books:" << endl;
for (int i = 0; i < books.size(); i++) {
cout << "Title: " << books[i].getTitle() << endl;
cout << "Author: " << books[i].getAuthor() << endl;
cout << "Year: " << books[i].getYear() << endl;
cout << "-----------------------" << endl;
}
}
void searchBook(string title) {
vector<Book> foundBooks;
for (int i = 0; i < books.size(); i++) {
if (books[i].getTitle() == title) {
foundBooks.push_back(books[i]);
}
}
if (foundBooks.size() > 0) {
cout << "Search Results for '" << title << "':" << endl;
for (int i = 0; i < foundBooks.size(); i++) {
cout << "Title: " << foundBooks[i].getTitle() << endl;
cout << "Author: " << foundBooks[i].getAuthor() << endl;
cout << "Year: " << foundBooks[i].getYear() << endl;
cout << "-----------------------" << endl;
}
} else {
cout << "No books found with title '" << title << "'." << endl;
}
}
void sortBooks() {
sort(books.begin(), books.end(), [](const Book& b1, const Book& b2) {
return b1.getTitle() < b2.getTitle();
});
cout << "Books sorted by title." << endl;
}
};
int main() {
Library library;
string username, password;
bool isAuthenticated = false;
cout << "Login System" << endl;
cout << "------------" << endl;
while (!isAuthenticated) {
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
isAuthenticated = authenticateUser(username, password);
if (!isAuthenticated) {
cout << "Invalid credentials. Please try again." << endl;
}
}
cout << "Login successful! Welcome, " << username << "!" << endl;
// Adding books
library.addBook(Book("Book 1", "Author 1", 2010));
library.addBook(Book("Book 2", "Author 2", 2015));
library.addBook(Book("Book 3", "Author 3", 2005));
// Displaying books
library.displayBooks();
// Searching book
library.searchBook("Book 2");
// Deleting book
library.deleteBook("Book 3");
library.displayBooks();
// Sorting books
library.sortBooks();
library.displayBooks();
return 0;
}
In this updated code, I added two new functions to the Library class: deleteBook and sortBooks.
The deleteBook function takes a book title as an input parameter and searches the book in the library. If the book is found, it is deleted from the vector of books. If the book is not found, it displays a message indicating that the book was not found.
The sortBooks function sorts the books in the library vector based on the title using the sort function from the <algorithm> library. After sorting, it displays a message indicating that the books have been sorted.
In the main function, an authentication loop is added to repeatedly prompt the user for the username and password until the credentials are valid. After successful login, the add, delete, display, search, and sort operations are performed on the library object.
You can modify the code or add more features as per your requirements.