Answer to Question #199000 in C++ for ghg

Question #199000

with 0% plagiarism Write a program in C++ that implements a library management system. Theprogram MUST use filing to manage library data. All the required methods mustbe implemented as member functions of the Book class. When program starts, itshould display following menu and ask the user to make a choice:

1. Press 1 to add a new book in the library

2. Press 2 to display list of books available in the library

3. Press 3 to search a book by its name

4. Press 4 to delete a book by its ID

5. Press 5 to issue a book to the user

6. Press 6 to exit

After user has made a choice

hint

Case: User pressed 1:Record following details from the user and save in the file. If the file already exists

then open the file for writing, otherwise create a new file.

● Book name ●book id● Author name ● Genre● Number of pages● Rating● Language● Issued to (User name)● Issue date


1
Expert's answer
2021-05-26T16:43:28-0400
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
class Library{
    fstream records;
    string name, author, genre, language, issue_date, issued_to, rating, id, pages;
    public:
    Library(){
        records.open("Records.txt", ios::out | ios::trunc);
        records.close();
    }
    void addBook(){
        cout<<"Enter book_id: "; cin>>id;
        istringstream ss;
        string s;
        records.open("Records.txt", ios::in);
        if(records) while(getline(records, s)){
            ss = istringstream(s);
            ss>>s;
            if(s == id){
                cout<<"\nBook already in Library";
                records.close();
                return;
            }
        }
        else{
            cout<<"\nError opening file";
            return;
        }
        records.close();
        records.open("Records.txt", ios::out | ios::app);
        cout<<"Enter book name: "; cin>>name;
        cout<<"Enter author: "; cin>>author;
        cout<<"Enter genre: "; cin>>genre;
        cout<<"Enter language: "; cin>>language;
        cout<<"Enter number of pages: "; cin>>pages;
        cout<<"Enter rating: "; cin>>rating;
        issue_date = "not_issued";
        issued_to = "not_issued";
        records<<id<<" "<<name<<" "<<author<<" "<<genre<<" "<<language<<" "<<pages<<" "<<rating<<endl;
        records.close();
    }
    void display(){
        string s;
        cout<<"Book_id, Book_name, Author, genre, language, number of pages, rating\n";
        records.open("Records.txt", ios::in);
        if(records){
            while(getline(records, s)){
                cout<<s<<endl;
            }
            records.close();
        }
        else{
            cout<<"\nError opening file";
            return;
        }
    }
    void search(){
        string n;
        cout<<"\nEnter book name eg. TheLegendOfHercules: "; cin>>n;
        string s;
        records.open("Records.txt", ios::in);
        if(records){
            while(getline(records, s)){
                istringstream ss(s);
                ss>>s;
                ss>>s;
                if(s == n){
                    cout<<"\nBook Available";
                    records.close();
                    return;
                }
            }
            cout<<"\nBook not found";
            records.close();
            return;
        }
        else{
            cout<<"\nError opening file";
            return;
        }
    }
    void del(){
        string i;
        cout<<"\nEnter book id: "; cin>>i;
        string s, tmp;
        records.open("Records.txt", ios::in);
        if(records){
            fstream dump("dump.dat", ios::out | ios::trunc);
            while(getline(records, s)){
                istringstream ss(s);
                ss>>tmp;
                if(tmp == i){
                    cout<<"\nBook deleted";
                    continue;
                }
                dump<<s<<endl;
            }
            dump.close();
            dump.open("dump.dat", ios::in);
            records.close();
            records.open("records.txt", ios::out | ios::trunc);
            while(getline(dump, s)){
                records<<s<<endl;
            }
            records.close();
            dump.close();
            dump.open("dump.dat", ios::out | ios::trunc);
            dump.close();
        }
        else{
            cout<<"\nError opening file";
            return;
        }
    }
    void issue(){
        cout<<"\nEnter book id to issue: "; cin>>id;
        
        string s, tmp;
        records.open("Records.txt", ios::in);
        if(records){
            fstream dump("dump.dat", ios::out | ios::trunc);
            while(getline(records, s)){
                istringstream ss(s);
                ss>>tmp;
                if(tmp == id){
                    while(ss>>tmp){
                        if(tmp == "issued"){
                            cout<<"\nBook already issued";
                            records.close();
                            return;
                        }
                    }
                    cout<<"\nEnter user: ";cin>>issued_to;
                    cout<<"\nEnter issue date: "; cin>>issue_date;
                    dump<<s<<" issued to "<<issued_to<<" on "<<issue_date<<endl;
                    continue;
                }
                else{
                    cout<<"\nBook not found";
                    records.close();
                    return;
                }
                dump<<s<<endl;
            }
            dump.close();
            dump.open("dump.dat", ios::in);
            records.close();
            records.open("records.txt", ios::out | ios::trunc);
            while(getline(dump, s)){
                records<<s<<endl;
            }
            records.close();
            dump.close();
            dump.open("dump.dat", ios::out | ios::trunc);
            dump.close();
        }
        else{
            cout<<"\nError opening file";
            return;
        }
    }
};
int main(){
    int choice;
    Library library;
    do{
        cout<<"\n1. Press 1 to add a new book in the library\n";
        cout<<"2. Press 2 to display list of books available in the library\n";
        cout<<"3. Press 3 to search a book by its name\n";
        cout<<"4. Press 4 to delete a book by its ID\n";
        cout<<"5. Press 5 to issue a book to the user\n";
        cout<<"6. Press 6 to exit\n";
        cin>>choice;
        switch(choice){
            case 1: library.addBook(); break;
            case 2: library.display(); break;
            case 3: library.search(); break;
            case 4: library.del(); break;
            case 5: library.issue(); break;
            default: break;
        }
    }while(choice > 0 && choice < 6);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog