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

Design a C++ program, the solution Includes all the following parts: A. The code

ID: 3703465 • Letter: D

Question

Design a C++ program, the solution Includes all the following parts:

A. The code and the files necessary to compile and test the project.

B. Test Plan: Showing how you tested the program (show the steps of your testing procedure along with screen shots).

C. A short description of the design plan and general comments.

Objective: Write a C++ program to manage a library system. The main user is the librarian.

Building Requirements:

1. The system should load a catalog of books, journals, and magazines at the start of the program. (at least five of each)

2. A user can search the catalog: The user of the system can search the library’s catalog by using the name of the item. Also, a user can list books by category (Book, Journal, or magazine). If you are looking specifically for a book, you can search by the name of the author.

3. A user can check out books, journals, or magazines: The user needs to find the item first, then they can check it out. A user cannot check out an item that is already checked out.

4. A user can return books, journals, or magazines: A user can return an item. The user needs to find the item first, then they can return it.

5. Add new books, journals, or magazines to the catalog. A librarian can add a new item to the catalog.

PS: Each item should have the following data members: ID, Name, and Status. Books have the “Author_name” as an additional data member. Journals have the “Volume” as an additional data member. Magazines have the “Issue_number” as an additional data member.

The program must have the following properties:

- You should do error handling (Ex: A librarian cannot add a book that already exists)

- You should use C++ concepts that we covered in class. Example: (inheritance) You can design a generic library item, then design derived classes for different kinds of items that a user can check out.

- Your program should use a suitable data structure to store the items.

Also, design a menu (should still appear until the exit option is chosen) in the Main program that has the following options implemented to test your classes’ functionality(It's very important!):

1. Search Catalog

2. Check out

3. Return

4. Add a new item

5. Exit

Extra Credit:

- Save and load data from and to a file.

Explanation / Answer

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
#include<iomanip.h>

class book
{
   char bno[6];
   char bname[50];
   char aname[20];
public:
   void create_book()
   {
       cout<<" NEW BOOK ENTRY... ";
       cout<<" Enter The book no.";
       cin>>bno;
       cout<<" Enter The Name of The Book ";
       gets(bname);
       cout<<" Enter The Author's Name ";
       gets(aname);
       cout<<" Book Created..";
   }

   void show_book()
   {
       cout<<" Book no. : "<<bno;
       cout<<" Book Name : ";
       puts(bname);
       cout<<"Author Name : ";
       puts(aname);
   }

   void modify_book()
   {
       cout<<" Book no. : "<<bno;
       cout<<" Modify Book Name : ";
       gets(bname);
       cout<<" Modify Author's Name of Book : ";
       gets(aname);
   }

   char* retbno()
   {
       return bno;
   }

   void report()
   {cout<<bno<<setw(30)<<bname<<setw(30)<<aname<<endl;}


};


class student
{
   char admno[6];
   char name[20];
   char stbno[6];
   int token;
public:
   void create_student()
   {
       clrscr();
       cout<<" NEW STUDENT ENTRY... ";
       cout<<" Enter The admission no. ";
       cin>>admno;
       cout<<" Enter The Name of The Student ";
       gets(name);
       token=0;
       stbno[0]='/0';
       cout<<" Student Record Created..";
   }

   void show_student()
   {
       cout<<" Admission no. : "<<admno;
       cout<<" Student Name : ";
       puts(name);
       cout<<" No of Book issued : "<<token;
       if(token==1)
           cout<<" Book No "<<stbno;
   }

   void modify_student()
   {
       cout<<" Admission no. : "<<admno;
       cout<<" Modify Student Name : ";
       gets(name);
   }

   char* retadmno()
   {
       return admno;
   }

   char* retstbno()
   {
       return stbno;
   }

   int rettoken()
   {
       return token;
   }

   void addtoken()
   {token=1;}

   void resettoken()
   {token=0;}

   void getstbno(char t[])
   {
       strcpy(stbno,t);
   }

   void report()
   {cout<<" "<<admno<<setw(20)<<name<<setw(10)<<token<<endl;}

};

fstream fp,fp1;
book bk;
student st;


void write_book()
{
   char ch;
   fp.open("book.dat",ios::out|ios::app);
   do
   {
       clrscr();
       bk.create_book();
       fp.write((char*)&bk,sizeof(book));
       cout<<" Do you want to add more record..(y/n?)";
       cin>>ch;
   }while(ch=='y'||ch=='Y');
   fp.close();
}

void write_student()
{
   char ch;
   fp.open("student.dat",ios::out|ios::app);
   do
   {
       st.create_student();
       fp.write((char*)&st,sizeof(student));
       cout<<" do you want to add more record..(y/n?)";
       cin>>ch;
   }while(ch=='y'||ch=='Y');
   fp.close();
}

void display_spb(char n[])
{
   cout<<" BOOK DETAILS ";
   int flag=0;
   fp.open("book.dat",ios::in);
   while(fp.read((char*)&bk,sizeof(book)))
   {
       if(strcmpi(bk.retbno(),n)==0)
       {
           bk.show_book();
          flag=1;
       }
   }

   fp.close();
   if(flag==0)
       cout<<" Book does not exist";
   getch();
}

void display_sps(char n[])
{
   cout<<" STUDENT DETAILS ";
   int flag=0;
   fp.open("student.dat",ios::in);
   while(fp.read((char*)&st,sizeof(student)))
   {
       if((strcmpi(st.retadmno(),n)==0))
       {
           st.show_student();
           flag=1;
       }
   }

   fp.close();
   if(flag==0)
       cout<<" Student does not exist";
    getch();
}

void modify_book()
{
   char n[6];
   int found=0;
   clrscr();
   cout<<" MODIFY BOOK REOCORD.... ";
   cout<<" Enter The book no. of The book";
   cin>>n;
   fp.open("book.dat",ios::in|ios::out);
   while(fp.read((char*)&bk,sizeof(book)) && found==0)
   {
       if(strcmpi(bk.retbno(),n)==0)
       {
           bk.show_book();
           cout<<" Enter The New Details of book"<<endl;
           bk.modify_book();
           int pos=-1*sizeof(bk);
           fp.seekp(pos,ios::cur);
           fp.write((char*)&bk,sizeof(book));
           cout<<" Record Updated";
           found=1;
       }
   }

   fp.close();
   if(found==0)
       cout<<" Record Not Found ";
   getch();
}


void modify_student()
{
   char n[6];
   int found=0;
   clrscr();
   cout<<" MODIFY STUDENT RECORD... ";
   cout<<" Enter The admission no. of The student";
   cin>>n;
   fp.open("student.dat",ios::in|ios::out);
   while(fp.read((char*)&st,sizeof(student)) && found==0)
   {
       if(strcmpi(st.retadmno(),n)==0)
       {
           st.show_student();
           cout<<" Enter The New Details of student"<<endl;
           st.modify_student();
           int pos=-1*sizeof(st);
           fp.seekp(pos,ios::cur);
           fp.write((char*)&st,sizeof(student));
           cout<<" Record Updated";
           found=1;
       }
   }

   fp.close();
   if(found==0)
       cout<<" Record Not Found ";
   getch();
}

void delete_student()
{
   char n[6];
   int flag=0;
   clrscr();
   cout<<" DELETE STUDENT...";
   cout<<" Enter The admission no. of the Student You Want To Delete : ";
   cin>>n;
   fp.open("student.dat",ios::in|ios::out);
   fstream fp2;
   fp2.open("Temp.dat",ios::out);
   fp.seekg(0,ios::beg);
   while(fp.read((char*)&st,sizeof(student)))
   {
       if(strcmpi(st.retadmno(),n)!=0)
          fp2.write((char*)&st,sizeof(student));
       else
          flag=1;
   }

   fp2.close();
   fp.close();
   remove("student.dat");
   rename("Temp.dat","student.dat");
   if(flag==1)
       cout<<" Record Deleted ..";
   else
        cout<<" Record not found";
   getch();
}


void delete_book()
{
   char n[6];
   clrscr();
   cout<<" DELETE BOOK ...";
   cout<<" Enter The Book no. of the Book You Want To Delete : ";
   cin>>n;
   fp.open("book.dat",ios::in|ios::out);
   fstream fp2;
   fp2.open("Temp.dat",ios::out);
   fp.seekg(0,ios::beg);
   while(fp.read((char*)&bk,sizeof(book)))
   {
       if(strcmpi(bk.retbno(),n)!=0)
       {
           fp2.write((char*)&bk,sizeof(book));
       }
   }

   fp2.close();
   fp.close();
   remove("book.dat");
   rename("Temp.dat","book.dat");
   cout<<" Record Deleted ..";
   getch();
}


void display_alls()
{
   clrscr();
    fp.open("student.dat",ios::in);
    if(!fp)
    {
        cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
        getch();
       return;
    }

   cout<<" STUDENT LIST ";
   cout<<"================================================================== ";
   cout<<" Admission No."<<setw(10)<<"Name"<<setw(20)<<"Book Issued ";
   cout<<"================================================================== ";

   while(fp.read((char*)&st,sizeof(student)))
   {
       st.report();
   }

   fp.close();
   getch();
}

void display_allb()
{
   clrscr();
   fp.open("book.dat",ios::in);
   if(!fp)
   {
       cout<<"ERROR!!! FILE COULD NOT BE OPEN ";
        getch();
       return;
    }

   cout<<" Book LIST ";
   cout<<"========================================================================= ";
   cout<<"Book Number"<<setw(20)<<"Book Name"<<setw(25)<<"Author ";
   cout<<"========================================================================= ";

   while(fp.read((char*)&bk,sizeof(book)))
   {
       bk.report();
   }
    fp.close();
   getch();
}


void book_issue()
{
   char sn[6],bn[6];
   int found=0,flag=0;
   clrscr();
   cout<<" BOOK ISSUE ...";
   cout<<" Enter The student's admission no.";
   cin>>sn;
   fp.open("student.dat",ios::in|ios::out);
   fp1.open("book.dat",ios::in|ios::out);
   while(fp.read((char*)&st,sizeof(student)) && found==0)
    {
       if(strcmpi(st.retadmno(),sn)==0)
       {
           found=1;
           if(st.rettoken()==0)
           {
               cout<<" Enter the book no. ";
               cin>>bn;
               while(fp1.read((char*)&bk,sizeof(book))&& flag==0)
               {
                  if(strcmpi(bk.retbno(),bn)==0)
                   {
                       bk.show_book();
                       flag=1;
                       st.addtoken();
                       st.getstbno(bk.retbno());
                        int pos=-1*sizeof(st);
                       fp.seekp(pos,ios::cur);
                       fp.write((char*)&st,sizeof(student));
                       cout<<" Book issued successfully Please Note: Write current date                       in backside of book and submit within 15 days fine Rs. 1 for each day                   after 15 days period";
                   }
               }
               if(flag==0)
                   cout<<"Book no does not exist";
           }
           else
               cout<<"You have not returned the last book ";

       }
   }
   if(found==0)
       cout<<"Student record not exist...";
   getch();
   fp.close();
   fp1.close();
}


void book_deposit()
{
char sn[6],bn[6];
int found=0,flag=0,day,fine;
clrscr();
cout<<" BOOK DEPOSIT ...";
cout<<" Enter The student’s admission no.";
cin>>sn;
fp.open("student.dat",ios::in|ios::out);
fp1.open("book.dat",ios::in|ios::out);
while(fp.read((char*)&st,sizeof(student)) && found==0)
{
   if(strcmpi(st.retadmno(),sn)==0)
   {
       found=1;
       if(st.rettoken()==1)
       {
           while(fp1.read((char*)&bk,sizeof(book))&& flag==0)
           {
           if(strcmpi(bk.retbno(),st.retstbno())==0)
           {
               bk.show_book();
               flag=1;
               cout<<" Book deposited in no. of days";
               cin>>day;
               if(day>15)
               {
               fine=(day-15)*1;
               cout<<" Fine has to deposited Rs. "<<fine;
               }
                   st.resettoken();
                   int pos=-1*sizeof(st);
                   fp.seekp(pos,ios::cur);
                   fp.write((char*)&st,sizeof(student));
                   cout<<" Book deposited successfully";
           }
       }
       if(flag==0)
       cout<<"Book no does not exist";
       }
       else
           cout<<"No book is issued..please check!!";
       }
   }
if(found==0)
   cout<<"Student record not exist...";
   getch();
fp.close();
fp1.close();
}

void intro()
{
   clrscr();
   gotoxy(35,11);
   cout<<"LIBRARY";
   gotoxy(35,14);
   cout<<"MANAGEMENT";
   gotoxy(35,17);
   cout<<"SYSTEM";
   cout<<" MADE BY : PRAGYA NAHAR";
   cout<<" SANSKRITI COMPUTER EDUCATION COLLEGE";
   getch();
}


void admin_menu()
{
   clrscr();
   int ch2;
   cout<<" ADMINISTRATOR MENU";
   cout<<" 1.CREATE STUDENT RECORD";
   cout<<" 2.DISPLAY ALL STUDENTS RECORD";
   cout<<" 3.DISPLAY SPECIFIC STUDENT RECORD ";
   cout<<" 4.MODIFY STUDENT RECORD";
   cout<<" 5.DELETE STUDENT RECORD";
   cout<<" 6.CREATE BOOK ";
   cout<<" 7.DISPLAY ALL BOOKS ";
   cout<<" 8.DISPLAY SPECIFIC BOOK ";
   cout<<" 9.MODIFY BOOK ";
   cout<<" 10.DELETE BOOK ";
   cout<<" 11.BACK TO MAIN MENU";
   cout<<" Please Enter Your Choice (1-11) ";
   cin>>ch2;
   switch(ch2)
   {
       case 1: clrscr();
           write_student();break;
       case 2: display_alls();break;
       case 3:
          char num[6];
          clrscr();
          cout<<" Please Enter The Admission No. ";
          cin>>num;
          display_sps(num);
          break;
       case 4: modify_student();break;
       case 5: delete_student();break;
       case 6: clrscr();
           write_book();break;
       case 7: display_allb();break;
       case 8: {
          char num[6];
          clrscr();
           cout<<" Please Enter The book No. ";
          cin>>num;
          display_spb(num);
          break;
           }
       case 9: modify_book();break;
       case 10: delete_book();break;
        case 11: return;
       default:cout<<"";
    }
    admin_menu();
}


void main()
{
   char ch;
   intro();
   do
   {
       clrscr();
       cout<<" MAIN MENU";
       cout<<" 01. BOOK ISSUE";
       cout<<" 02. BOOK DEPOSIT";
       cout<<" 03. ADMINISTRATOR MENU";
       cout<<" 04. EXIT";
       cout<<" Please Select Your Option (1-4) ";
       ch=getche();
       switch(ch)
       {
           case '1':clrscr();
               book_issue();
              break;
           case '2':book_deposit();
               break;
           case '3':admin_menu();
               break;
           case '4':exit(0);
           default :cout<<"";
       }
   }while(ch!='4');
}