0% found this document useful (0 votes)
40 views

Software Engineering Theory Assignment 2 Library Management System Code

The document contains the code for a C++ library management program that allows a user to add, remove, and view books in a library. It includes classes for a Book that stores title, author, and other metadata, and a Library class that manages a collection of Book objects using maps and multimaps. The main function displays a menu and calls functions on the Library class to handle user input and interactions with the library data.

Uploaded by

Gowtham Sai
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Software Engineering Theory Assignment 2 Library Management System Code

The document contains the code for a C++ library management program that allows a user to add, remove, and view books in a library. It includes classes for a Book that stores title, author, and other metadata, and a Library class that manages a collection of Book objects using maps and multimaps. The main function displays a menu and calls functions on the Library class to handle user input and interactions with the library data.

Uploaded by

Gowtham Sai
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

/******************************************************************************

Online C++ Compiler.


Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

//main.cpp

#include <string>
#include <iostream>
#include <iomanip>

#include "Library.h"

void displayMenu();
bool menuSelection( int option, Library* lib );
void addBookInformation( Library* lib );
void removeBook( Library* lib );
void displayBook( Library* lib );

int main() {
Library lib;

std::cout << "Welcome to the Library!\n";


displayMenu();

int option = 0;
do {
std::cout << "\nChoose an option from the menu ";
std::cin >> option;
std::cout << '\n';
} while( menuSelection( option, &lib ) );

std::cout << "\nPress any key and enter to quit.\n";


std::cin.get();
return 0;
}

void displayMenu() {
std::cout << "================================================\n";
std::cout << "1: Add a book to the library\n";
std::cout << "2: Remove book from the library\n";
std::cout << "3: Display the number of books in the library\n";
std::cout << "4: Display a books information\n";
std::cout << "5: Display the list of all books\n";
std::cout << "6: Display menu option\n";
std::cout << "7: Exit the library and quit\n";
std::cout << "================================================\n";
}

bool menuSelection( int option, Library* lib ) {


switch( option ) {
case 1: {
addBookInformation( lib );
std::cout.flush();
system( "clear" );
displayMenu();
std::cout << "\nYou have entered a book into the libray.\n";
break;
}
case 2: {
removeBook( lib );
std::cout.flush();
system( "clear" );
displayMenu();
std::cout << "\nYou have removed a book from the library.\n";
break;
}
case 3: {
unsigned int numBooks = lib->totalBooks();
if( numBooks != 1 ) {
std::cout << "\nThere are " << numBooks << " books in our library.\
n";
} else {
std::cout << "\nThere is 1 book in our library.\n";
}
break;
}
case 4: {
displayBook( lib );
break;
}
case 5: {
unsigned int numBooks = lib->totalBooks();
if( numBooks > 0 ) {
std::cout << *lib;
} else {
std::cout << "\nThere are no books to display.\n";
}
break;
}
case 6: {
std::cin.ignore();
std::cout.flush();
system( "clear" );
displayMenu();
break;
}
case 7: {
return false;
}
default: {
std::cout << "\nInvalid selection please try again.\n";
break;
}
}
return true;
}

void addBookInformation( Library* lib ) {


static unsigned int bookId = 0;

std::string title, author, releasedate, language;


std::cin.ignore();
std::cout << "Please enter the books title: ";
std::getline( std::cin, title );
std::cout << "Please enter the books author: ";
std::getline( std::cin, author );

std::cout << "Please enter the books releasedate: ";


std::getline( std::cin, releasedate );
std::cout << "Please enter the books language: ";
std::getline( std::cin, language );

bookId++; // increment our book id so each one is unique TODO: this can be
replaced to have same id for multiple books if the books are exact matches.

Book book( title, author, releasedate, language );


lib->addBook( std::to_string( bookId ), book );
}

void removeBook( Library* lib ) {


unsigned int numBooks = lib->totalBooks();
if( numBooks == 0 ) {
std::cout << "\nThere are 0 books in library; nothing to remove.\n";
return;
}

std::cout << "\nRemove book by ID(I) or by Book(B)\n";


char choice;
std::cin >> choice;
if( choice == 'i' || choice == 'I' ) {
std::cout << "Enter the books ID ";
unsigned int id;
std::cin >> id;
lib->removeBook( std::to_string( id ) );
} else if( choice == 'b' || choice == 'B' ) {
std::cin.ignore();
std::cout << "What is the title of the book? ";
std::string title;
std::getline( std::cin, title );
std::cout << "Who is the author of the book? ";
std::string author;
std::getline( std::cin, author );

std::cout << "What is the releasedate of the book? ";


std::string releasedate;
std::getline( std::cin, releasedate );
std::cout << "What is the language of the book? ";
std::string language;
std::getline( std::cin, language );
Book book( title, author, releasedate, language );
lib->removeBook( book );
} else {
std::cout << "\nYou entered an invalid selection\n";
}
}

void displayBook( Library* lib ) {


unsigned int numBooks = lib->totalBooks();
if( numBooks == 0 ) {
std::cout << "\nThere are 0 books in the library; nothing to display.\n";
return;
}
std::cout << "\nFind book by ID(I) or by Book(B)\n";
char choice;
std::cin >> choice;
if( choice == 'i' || choice == 'I' ) {
std::cout << "Enter the books ID ";
unsigned int id;
std::cin >> id;
Book* book = lib->findBook( std::to_string( id ) );
if( book ) {
std::cout << *book;
} else {
std::cout << "\nBook was not found.\n";
}

} else if( choice == 'b' || choice == 'B' ) {


std::cin.ignore();
std::cout << "What is the title of the book? ";
std::string title;
std::getline( std::cin, title );
std::cout << "Who is the author of the book? ";
std::string author;
std::getline( std::cin, author );

std::cout << "What is the releasedate of the book? ";


std::string releasedate;
std::getline( std::cin, releasedate );
std::cout << "What is the language of the book? ";
std::string language;
std::getline( std::cin, language );

Book bookToFind( title, author, releasedate, language );


Book* actualBook = lib->findBook( bookToFind );

if( actualBook ) {
std::cout << *actualBook;
} else {
std::cout << "\nBook was not found.\n";
}

} else {
std::cout << "\nYou entered an invalid selection\n";
}
}

//Book.h

#ifndef BOOK_H
#define BOOK_H

#include <string>
#include <iostream>
#include <iomanip>

class Book {
private:
std::string title_;
std::string author_;

std::string releasedate_;
std::string language_;

public:
Book(); // default
Book( const std::string& title, const std::string& author, const std::string&
releasedate, const std::string& language );

std::string titleIs() const;


std::string authorIs() const;

std::string releasedateIs() const;


std::string languageIs() const;

void updateTitle( const std::string& title );


void updateAuthor( const std::string& author );

void updatereleasedate( const std::string& releasedate );


void updatelanguage( const std::string& language );

bool operator==( const Book& other ) const;


};

std::ostream& operator<<( std::ostream& out, const Book& book );

#endif // BOOK_H

//Book.cpp

#include "Book.h"

Book::Book() {
} // default

Book::Book( const std::string& title, const std::string& author, const std::string&


releasedate, const std::string& language ) :
title_( title ),
author_( author ),

releasedate_( releasedate ),
language_( language ){
}

std::string Book::titleIs() const {


return title_;
}

std::string Book::authorIs() const {


return author_;
}

std::string Book::releasedateIs() const {


return releasedate_;
}

std::string Book::languageIs() const {


return language_;
}

void Book::updateTitle( const std::string& title ) {


title_ = title;
}

void Book::updateAuthor( const std::string& author ) {


author_ = author;
}

void Book::updatereleasedate( const std::string& releasedate ) {


releasedate_ = releasedate;
}

void Book::updatelanguage( const std::string& language ) {


language_ = language;
}

bool Book::operator==( const Book& other ) const {


return ( title_ == other.title_ &&
author_ == other.author_ &&

releasedate_ == other.releasedate_ &&


language_ == other.language_);
}

std::ostream& operator<<( std::ostream& out, const Book& book ) {


out << std::setw( 15 ) << "Title: " << book.titleIs() << '\n'
<< std::setw( 15 ) << "Author: " << book.authorIs() << '\n'

<< std::setw( 15 ) << "Releasedate: " << book.releasedateIs() << '\n'


<< std::setw( 15 ) << "Language: " << book.languageIs() << '\n';
return out;
}

//Library.h

#ifndef LIBRARY_H
#define LIBRARY_H

#include "Book.h"
#include <map>

class Library {
private:
std::multimap<std::string, Book> books_;
// This is used if the library has several copies of the same book
// that have the same ID in the multimap above.
std::map<std::string, unsigned int> inventory_;

public:
Library(); // deafault

void addBook( const std::string& id, Book& book );


void removeBook( const std::string& id );
void removeBook( Book& book );

Book* findBook( const std::string& id );


Book* findBook( Book& book );

std::size_t totalBooks() const;


std::size_t totalUniqueBooks() const;

// Three different ways to return the library back to user


std::multimap<std::string, Book> mapOfBooks() const;

// Currently Supports List and Vector


template< template < class ... > class Container, class ... Args >
void listOfBooks( Container<Book, Args...>& c ) const;

private:
// Helper function to calculate the number of unique books.
std::size_t calculateUniqueNumberOfBooks() const;
};

template<template<class...> class Container, class...Args>


void Library::listOfBooks( Container<Book, Args...>& c ) const {
auto it = books_.begin();
while ( it != books_.end() ) {
c.emplace_back( it->second );
}
}

std::ostream& operator<<( std::ostream& out, const Library& library );

void displayLibrary( const Library& library );

#endif // LIBRARY_H

//Library.cpp

#include "Library.h"
#include <vector>

Library::Library() {
} // deafault

void Library::addBook( const std::string& id, Book& book ) {


books_.insert( std::pair<std::string,Book>( id, book ) );
}

void Library::removeBook( const std::string& id ) {


auto it = books_.begin();
while( it != books_.end() ) {
if( id == it->first ) {
// found match so remove it
it = books_.erase( it );
} else {
it++;
}
}
}

void Library::removeBook( Book& book ) {


auto it = books_.begin();
while( it != books_.end() ) {
if( book == it->second ) {
// found match so remove it
it = books_.erase( it );
} else {
it++;
}
}
}

Book* Library::findBook( const std::string& id ) {


auto it = books_.begin();
while( it != books_.end() ) {
if( id == it->first ) {
return &it->second;
} else{
it++;
}
}
return nullptr;
}

Book* Library::findBook( Book& book ) {


auto it = books_.begin();
while( it != books_.end() ) {
if( book == it->second ) {
return &it->second;
} else {
it++;
}
}
return nullptr;
}

std::multimap<std::string, Book> Library::mapOfBooks() const {


return books_;
}

std::size_t Library::totalBooks() const {


return books_.size();
}

std::size_t Library::totalUniqueBooks() const {


//TODO: For now just return total number of books
return books_.size();
}

std::size_t Library::calculateUniqueNumberOfBooks() const {


//TODO: For now just return total number of books
return books_.size();
}

std::ostream& operator<<( std::ostream& out, const Library& library ) {


for( auto b : library.mapOfBooks() ) {
out << "ID " << b.first << '\n'
<< b.second;
}
return out;
}

void displayLibrary( const Library& library ) {


std::cout << library;
}

You might also like