Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write below code in C++ We are going to make a C++ program named “Library Manage

ID: 3767819 • Letter: W

Question

Write below code in C++

We are going to make a C++ program named “Library Manager”. The program

manages books for a library (adding, removing, checking in/out, and so on). The

main menu is following.

1. Add a new book

2. Remove a book (input ISBN)

3. Check out a book (input ISBN and the due date)

4. Check in a book (input ISBN)

5. Display all book details (along with their specialization)

6. Display all available book details

7. Display all checked-out books

8. Exit program

The main menu will repeat to display until the user select menu 8.

Each book has following properties such as ISBN, title, and so on (please see the

next page). Please create a “Book” structure consisting of the members. Then,

you have to ask such information of a book to a user when adding a new book.

Please check if the input is valid or not.

In this program, ISBN is a key, which means an identifier of a book (i.e., we

assume that there is only one book of the ISBN in this library, though it’s

unrealistic in practice). When adding a new book, you have to check if there is the

same ISBN already stored in the library. If so, you have to give an error message

to a user and ask again to input a different ISBN.

1. Has an ISBN (10 digits): e.g., 1001234567

2. Has a title (string): e.g., C++ Programming Language

3. Has a type (string): e.g., textbook, magazine, journal, newspaper, novel, and other

4. Has a publisher (string): e.g., TAMUC

5. Has a page information (how many pages) (integer): e.g., 506

6. Has a price (float): e.g., 20.00

7. Has a publication year (integer): e.g., 2013

8. Has a status (integer): e.g., 0=available, 1=checked-out

9. Has a name who checked out the book (string): e.g., Tommy

10. Has a due date (Date): use Date structure

Please use the “Date” structure for “Due Date” described as,

struct Date

int nYear;

int nMonth;

int nDay;

The Library manager may have a 1D array of the Book structure. You can define

the maximum book number (e.g., 100) that the library can store.

Explanation / Answer

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include<iomanip>
#include<stdio.h>
#include <string.h>
#include "Book.h"
#include "Student.h"

using namespace std;

int main() {
    Student student4;
    Book book2;

    while(1)
    {
        char mainSelect;
        char studentOption;
        char bookOption;
        char name[30];

        bool processed = false;
        while(!processed)
        {
        // User Menu

        // Read user selection
        cin.getline( name, 80);
        mainSelect = name[0];

        // Switch statement to select between the options
        switch (mainSelect)
        {
          case '1':
            processed = true;
            break;
          case '2':
            processed = true;
            break;
          case '3':
            processed = true;
            break;
          case '4':
            processed = true;
            exit(0);
            break;
          case '5':
            default:
            cout<<"Incorrect selection. Please select from the given options." <<endl;
            processed = false;
            break;
        }
        };
         if (mainSelect == '1')
            {
                // User Menu

                cin.getline(name, 80);
                bookOption = name[0];

                switch(bookOption)
                {
                    case '1':
                        book2.issueBook();
                        break;
                    case '2':
                        book2.returnBook();
                        break;
                    case '3':
                        book2.insertBook();
                        break;
                    case '4':
                        book2.updateBook();
                        break;
                    case '5':
                        book2.deleteBook();
                        break;
                    case '6':
                        char ISBN[6];
                        cout<<"Enter The book ISBN: " <<endl;
                        cin>>ISBN;
                        book2.searchBook(ISBN);
                        break;
                    case '7':
                        book2.showallBooks();
                        break;
                    case '8':
                        break;
                    case '9':
                        exit(0);
                        break;
                }
            }
            else if (mainSelect=='2')
            {
                // User Menu

                cin.getline( name, 80);
                studentOption = name[0];

                switch(studentOption){
                    case '1':
                        student4.insertStudent();
                        break;
                    case '2':
                        student4.showallStudents();
                        break;
                    case '3':
                        char regno[6];
                        cout<<"Enter the registration no. of the student you want to search: "<<endl;
                        cin>>regno;
                        student4.searchStudent(regno);
                        break;
                    case '4':
                        student4.updateStudent();
                        break;
                    case '5':
                        student4.deleteStudent();
                        break;
                    case '6':
                        exit(0);
                        break;
                    case '7':
                        cout << "Invalid selection!" << endl;
                        break;
                 }

              }

    return 0;
};

Media.h

#ifndef MEDIA_H_
#define MEDIA_H_

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include<iomanip>

class Media
{
public:
    char ISBN[6];
    char name[50];
    char num[6];

    char* retISBN()
        {
            return ISBN;
        }
};

Book.h

#ifndef BOOK_H_
#define BOOK_H_

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include<iomanip>
#include "Media.h"

using namespace std;

class Book : public Media
{
    char author[20];

public:
    void insertBook();
    void searchBook(char ISBN[]);
    void updateBook();
    void deleteBook();
    void showallBooks();
    void issueBook();
    void returnBook();

    void newBook()
    {
        cout<<"Enter the Book Name: " <<endl;
        fgets(name, sizeof(name), stdin);
        cout<<"Enter the Name of Author: " <<endl;
        fgets(author, sizeof(author), stdin);
        cout<<"Enter the book ISBN: " <<endl;
        cin>>ISBN;
        cout<<"Book Added to the library" <<endl;
    }

    void displayBook()
    {
        cout<<"Enter the Book Name: " <<endl;
        puts(name);
        cout<<"Enter the Name of Author: ";
        puts(author);
        cout<<"Enter the Book ISBN: " <<endl;
        cin>>ISBN;
    }

    void editBook()
    {
        cout<<"Enter the Book Name you want to Update: " <<endl;
        cin>>ISBN;
        cout<<"Enter New Book Name: " <<endl;
        fgets(name, sizeof(name), stdin);
        cout<<"Enter New Name of Author: " <<endl;
        fgets(author, sizeof(author), stdin);
    }

    void report()
    {
      
        cout<<"|Name of Book| "<<"|Director of Book| "<<"|ISBN of Book| " <<endl;

        cout<<" "<<name<<"------------"<<author<<"-----------------"<<ISBN<<endl;
    }


};         //class ends here


#endif /* BOOK_H_ */

Book.cpp

#include <stdlib.h>
#include <fstream>
#include <string.h>
#include <iostream>
#include<iomanip>
#include "Book.h"
#include "Student.h"

using namespace std;

Student student2;
Book book1;

void Book::insertBook()
{
    fstream file;
    file.open("book.dat",ios::out|ios::app);
    newBook();
    file.write((char*)&book1,sizeof(Book));
    file.close();
}

void Book::searchBook(char ISBN[6])
{
    fstream file;
    int sys=0;
    file.open("book.dat",ios::in);
    while(file.read((char*)&book1,sizeof(Book)))
    {
        if(strcmp(retISBN(),ISBN)==0)
        {
            displayBook();
            sys=1;
        }
    }

Student.h

#ifndef STUDENT_H_
#define STUDENT_H_

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include<iomanip>

using namespace std;

class Student
{
    char registrationno[6];
    char name[20];
    char stbookbar[6];
    char stdvdbar[6];

public:
    void insertStudent();
    void searchStudent(char regno[]);
    void updateStudent();
    void deleteStudent();
    void showallStudents();

    char* retregistrationNo()
    {
        return registrationno;
    }

    char* retstudentbookBar()
    {
        return stbookbar;
    }

    char* retstudentdvdBar()
    {
        return stdvdbar;
    }

    void getstudentbookBar(char t[])
    {
        strcpy(stbookbar,t);
    }

    void getstudentdvdBar(char t[])
    {
        strcpy(stdvdbar,t);
    }

    void report()
    {
        cout<<"_________________"<<" _________________"<<endl;
        cout<<"|Name of Student| "<<"|Registration No|" <<endl;
        cout<<" "<<name<<"-----------"<<registrationno<<endl;
    }

    void newStudent()
    {
        cout<<"Enter the registration no. " <<endl;
        cin>>registrationno;
        cin.ignore();
        cout<<"Enter the name of the student " <<endl;
        fgets(name, sizeof(name), stdin);
        stbookbar[0]='/0';
        cout<<"Student added to system." <<endl;
    }

    void displayStudent()
    {
        cout<<"Enter the registration no. : " <<endl;
        cin>>registrationno;
        cin.ignore();
        cout<<"Enter the name of the student: " <<endl;
        puts(name);
    }

    void editStudent()
    {
        cout<<"registration no. : " <<endl;
        cin>>registrationno;
        cout<<"Modify Student Name : " <<endl;;
        fgets(name, sizeof(name), stdin);
    }

};        

Student.cpp

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include<iomanip>
#include "Student.h"

using namespace std;

Student student1;

void Student::insertStudent()
{
    fstream file;
    file.open("student.dat",ios::out|ios::app);
    newStudent();
    file.write((char*)&student1,sizeof(Student));
    file.close();
}

void Student::searchStudent(char regno[])
{
    fstream file;
    int flag=0;
    file.open("student.dat",ios::in);
    while(file.read((char*)&student1,sizeof(Student)))
    {
        if((strcmp(retregistrationNo(),regno)==0))
        {
            displayStudent();
            flag=1;
        }
    }

    file.close();
    if(flag==0)
            cout<<"Error: Student not found in the system. "<<endl;
}

void Student::updateStudent()
{
    fstream file;
    char regno[6];
    int found=0;
    cout<<"Enter Student Registration no. " <<endl;
    cin>>regno;
    file.open("student.dat",ios::in|ios::out);
    while(file.read((char*)&student1,sizeof(Student)) && found==0)
    {
        if(strcmp(retregistrationNo(),regno)==0)
        {
            displayStudent();
            cout<<"Enter student's new details"<<endl;
            editStudent();
            long pos=-1*sizeof(student1);
            file.seekp(pos,ios::cur);
            file.write((char*)&student1,sizeof(Student));
            cout<<"Student's details Updated"<<endl;
            found=1;
        }
    }

    file.close();
    if(found==0)
        cout<<"Error: Student not found in the system. "<<endl;

}

void Student::deleteStudent()
{
    fstream file, file2;
    char n[6];
    int flag=0;

        cout<<"Enter The registration no. of the Student You Want To Delete : "<<endl;
        cin>>n;
        file.open("student.dat",ios::in|ios::out);
        file2.open("Temp.dat",ios::out);
        file.seekg(0,ios::beg);
        while(file.read((char*)&student1,sizeof(Student)))
    {
        if(strcmp(retregistrationNo(),n)!=0)
                file2.write((char*)&student1,sizeof(Student));
        else
            flag=1;
    }

    file2.close();
        file.close();
        remove("student.dat");
        rename("Temp.dat","student.dat");
        if(flag==1)
            cout<<"Student deleted from the system. " <<endl;
        else
            cout<<"Error: Student not found in the system. " <<endl;
}

void Student::showallStudents()
{
        fstream file;
        file.open("student.dat",ios::in);
        if(!file)
        {
            cout<<"Error: File could not be opened. "<<endl;

            return;
        }

    while(file.read((char*)&student1,sizeof(Student)))
    {
        report();
        break;
    }

    file.close();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote