/**************************************************************
*******NAME:-RAM RANJEET KUMAR ********************************
*******ROLL:- 17PGMCA03497 ************************************
***************************************************************
           HEADER FILES USED IN PROJECT
****************************************************************/

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<iomanip.h>
#include<iostream.h>
#include <time.h>
#include<stdlib.h>    



//*****************************************************************************
//            FUNCTION FOR TIME DISPLAY
//*****************************************************************************

void time()
{
    time_t tim;  //create variable of time_t

    time(&tim); //pass variable tim to time function

    cout <<"\nCURRENT TIME: "<<ctime(&tim); // this translates what was returned from time() into a readable format



    
}    
//***************************************************************
//                   CLASS USED IN PROJECT
//****************************************************************



class book
{
    char BookNo[30], BookName[30], AutherName[30];
  public:
    void AddBook()
    {
        
        cout<<"\nNEW BOOK ENTRY=====>\n";
        cout<<"\nEnter The Book No.: ";
        cin>>BookNo;
        cout<<"\n\nEnter The Name of The Book: ";
        gets(BookName);
        cout<<"\n\nEnter The Author's Name: ";
        gets(AutherName);
        cout<<"\n\n\nBook Created..";
    }

    void ShowBook()
    {
        
        cout<<"\nBook no. : "<<BookNo;
        cout<<"\nBook Name : ";
        puts(BookName);
        cout<<"Author Name : ";
        puts(AutherName);
    }

    void EditBook()
    {
        
        cout<<"\nBook no. : "<<BookNo;
        cout<<"\nModify Book Name : ";
        gets(BookName);
        cout<<"\nModify Author's Name of Book : ";
        gets(AutherName);
    }

    char* retBookNo()
    {
        return BookNo;
    }

    void report()
    {cout<<BookNo<<setw(25)<<BookName<<setw(30)<<AutherName<<endl;}


};         //class ends here




class student
{
    char RollNo[30],Name[30], stBookNo[30];
    int token;
public:
    void AddStudent()
    {
        
        
        cout<<"\nNEW STUDENT ENTRY=====>\n";
        cout<<"\nEnter The Roll No. in pattern(17PGMCA03497):";
        cout<<"\n Roll Number: ";
        cin>>RollNo;
        cout<<"\n\nEnter The Name of The Student ";
        gets(Name);
        token=0;
        stBookNo[0]='/0';
        cout<<"\n\nStudent Record Created=====>";

    }

    void ShowStudent()
    {
        cout<<"\nEnter The Roll No. in pattern(17PGMCA03497):";
        cout<<"\nRoll no. : "<<RollNo;
        cout<<"\nStudent Name : ";
        puts(Name);
        cout<<"\nNo of Book issued : "<<token;
        if(token==1)
            cout<<"\nBook No "<<stBookNo;
    }

    void EditStudent()
    {
        cout<<"\nEnter The Roll No. in pattern(17PGMCA03497):";
        cout<<"\nRoll no. : "<<RollNo;
        cout<<"\nModify Student Name : ";
        gets(Name);
    }

    char* retRollNo()
    {
        return RollNo;
    }

    char* retstBookNo()
    {
        return stBookNo;
    }

    int rettoken()
    {
        return token;
    }

    void addtoken()
    {token=1;}

    void resettoken()
    {token=0;}

    void getstBookNo(char t[])
    {
        strcpy(stBookNo,t);
    }

    void report()
    {cout<<"\t"<<RollNo<<setw(25)<<Name<<setw(20)<<token<<endl;}

};         //class ends here




//*******************************************************************
//     Declaration for FILE Stream Object And Class Objects
//*******************************************************************

fstream fp,fp1;
book bk;
student st;


//***************************************************************
//        FUNCTION DEFINITION TO WRITE IN FILE
//****************************************************************

void GetBook()
{
    
    char ch;
    fp.open("book.txt",ios::out|ios::app);
    do
    {
        system("cls");
        bk.AddBook();
        fp.write((char*)&bk,sizeof(book));
        cout<<"\n\nDo you want to add more record..(y/n?)=====>";
        cin>>ch;
    }while(ch=='y'||ch=='Y');
    fp.close();
}

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


//***************************************************************
//        FUNCTIONS DEFINITIONS FOR READ INDIVIDUAL RECORDS
//****************************************************************


void ShowIB(char n[])
{
    
    cout<<"\nBOOK DETAILS\n";
    int flag=0;
    fp.open("book.txt",ios::in);
    while(fp.read((char*)&bk,sizeof(book)))
    {
        if(strcmpi(bk.retBookNo(),n)==0)
        {
            bk.ShowBook();
            flag=1;
        }
    }

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

void ShowIS(char n[])
{
    
    cout<<"\nSTUDENT DETAILS\n";
    int flag=0;
    fp.open("student.txt",ios::in);
    while(fp.read((char*)&st,sizeof(student)))
    {
        if((strcmpi(st.retRollNo(),n)==0))
        {
            st.ShowStudent();
            flag=1;
        }
    }

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


//***************************************************************
//        FUNCTION DEFINITON FOR EDITING RECORDS
//****************************************************************


void EditBook()
{
    
    char n[30];
    int found=0;
    system("cls");
    cout<<"\n\n\tMODIFY BOOK REOCORD=====>";
    cout<<"\n\n\tEnter The book no. of The book";
    cin>>n;
    fp.open("book.txt",ios::in|ios::out);
    while(fp.read((char*)&bk,sizeof(book)) && found==0)
    {
        if(strcmpi(bk.retBookNo(),n)==0)
        {
            bk.ShowBook();
            cout<<"\nEnter The New Details of book"<<endl;
            bk.EditBook();
            int pos=-1*sizeof(bk);
            fp.seekp(pos,ios::cur);
            fp.write((char*)&bk,sizeof(book));
            cout<<"\n\n\t Record Updated";
            found=1;
        }
    }

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


void EditStudent()
{
    
    char n[30];
    int found=0;
    system("cls");
    cout<<"\n\n\tMODIFY STUDENT RECORD=====>";
    cout<<"\nEnter The Roll No. in pattern(17PGMCA03497):";
    cout<<"\nRoll Number: ";
    cin>>n;
    fp.open("student.txt",ios::in|ios::out);
    while(fp.read((char*)&st,sizeof(student)) && found==0)
    {
        if(strcmpi(st.retRollNo(),n)==0)
        {
            st.ShowStudent();
            cout<<"\nEnter The New Details of student"<<endl;
            st.EditStudent();
            int pos=-1*sizeof(st);
            fp.seekp(pos,ios::cur);
            fp.write((char*)&st,sizeof(student));
            cout<<"\n\n\t Record Updated";
            found=1;
        }
    }

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

//***************************************************************
//        function DEFINITION to delete record of file
//****************************************************************


void DeleteStudent()
{
    char n[30];
    int flag=0;
    system("cls");
    cout<<"\n\n\n\tDELETE STUDENT=====>";
    cout<<"\n\nEnter The Roll no. of the Student You Want To Delete in pattern(17PGMCA03497): ";
    cout<<"\nRoll no";
    cin>>n;
    fp.open("student.txt",ios::in|ios::out);
    fstream fp2;
    fp2.open("Temp.txt",ios::out);
    fp.seekg(0,ios::beg);
    while(fp.read((char*)&st,sizeof(student)))
    {
        if(strcmpi(st.retRollNo(),n)!=0)
            fp2.write((char*)&st,sizeof(student));
        else
            flag=1;
    }

    fp2.close();
    fp.close();
    remove("student.txt");
    rename("Temp.txt","student.txt");
    if(flag==1)
        cout<<"\n\n\tRecord Deleted ..";
    else
        cout<<"\n\nXXXX===Record not found=====XXXX";
    getch();
}


void DeleteBook()
{
    
    char n[30];
    system("cls");
    cout<<"\n\n\n\tDELETE BOOK =====>";
    cout<<"\n\nEnter The Book no. of the Book You Want To Delete : ";
    cin>>n;
    fp.open("book.txt",ios::in|ios::out);
    fstream fp2;
    fp2.open("Temp.txt",ios::out);
    fp.seekg(0,ios::beg);
    while(fp.read((char*)&bk,sizeof(book)))
    {
        if(strcmpi(bk.retBookNo(),n)!=0)
        {
            fp2.write((char*)&bk,sizeof(book));
        }
    }

    fp2.close();
    fp.close();
    remove("book.txt");
    rename("Temp.txt","book.txt");
    cout<<"\n\n\tRecord Deleted ..";
    getch();
}



//***************************************************************
//        function  definition to display all students list
//****************************************************************

void PutAllStudent()
{
    system("cls");
    
    fp.open("student.txt",ios::in);
    if(!fp)
    {
        cout<<"XXXX==ERROR!!! FILE COULD NOT BE OPEN ==XXXX";
        getch();
        return;
    }

    cout<<"\n\n========================STUDENT LIST==========================================\n\n";
    cout<<"==============================================================================\n";
    cout<<"\tRoll No."<<setw(25)<<"Name"<<setw(30)<<"Book Issued\n";
    cout<<"===============================================================================\n";

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

    fp.close();
    cout<<"\n\n\n\t\t Please press any key to GO BACK <=====\n";
    getch();
}


//***************************************************************
//        function to display Books list
//****************************************************************

void ShowAllBook()
{
    system("cls");
    fp.open("book.txt",ios::in);
    if(!fp)
    {
        cout<<"\n\n\n\t\tERROR!!! FILE COULD NOT BE OPEN ";
        getch();
        return;
    }

    cout<<"\n\n\t\tBook LIST\n\n";
    cout<<"=========================================================================\n";
    cout<<"Book No."<<setw(25)<<"Book Name"<<setw(25)<<"Author\n";
    cout<<"=========================================================================\n";

    while(fp.read((char*)&bk,sizeof(book)))
    {
        bk.report();
    }
    fp.close();
    cout<<"\n\n\n\t\t Please press any key to GO BACK (<====)\n";
    getch();
}



//***************************************************************
//        function definition to issue book
//****************************************************************

void BookIssue()
{
    char sn[30],bn[30];
    int found=0,flag=0;
    system("cls");
    cout<<"\n\nBOOK ISSUE ...";
    cout<<"\n\n\tEnter The Student's Roll Number in Pattern(17PGMCA03497):";
    cout<<"\n\t Roll No.:  ";
    cin>>sn;
    fp.open("student.txt",ios::in|ios::out);
    fp1.open("book.txt",ios::in|ios::out);
    while(fp.read((char*)&st,sizeof(student)) && found==0)
    {
     if(strcmpi(st.retRollNo(),sn)==0)
     {
      found=1;
      if(st.rettoken()==0)
      {
       cout<<"\n\n\tEnter the Book no.:  ";
       cin>>bn;
       while(fp1.read((char*)&bk,sizeof(book))&& flag==0)
       {
        if(strcmpi(bk.retBookNo(),bn)==0)
        {
         bk.ShowBook();
         flag=1;
         st.addtoken();
         st.getstBookNo(bk.retBookNo());
         int pos=-1*sizeof(st);
         fp.seekp(pos,ios::cur);
         fp.write((char*)&st,sizeof(student));
         cout<<"\n\n\t Book issued successfully.";
         cout<<"\n\nPlease Return Book Under 15 days";
         cout<<" otherwise after that you will charge Rs.2 per Day";
         cout<<"\n\n\n\t\t Please press any key to GO BACK (<====)\n";
         }
        }
        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();
}

//***************************************************************
//        function definition to deposit book
//****************************************************************

void BookDeposit()
{
    char sn[30],bn[30];
    int found=0,flag=0,day,fine;
    system("cls");
    cout<<"\n\nBOOK DEPOSIT ...";
    cout<<"\n\n\tEnter The student Roll no. in Pattern(17PGMCA03497)";
    cout<<"\n Roll Number:  ";
    cin>>sn;
    fp.open("student.txt",ios::in|ios::out);
    fp1.open("book.txt",ios::in|ios::out);
    while(fp.read((char*)&st,sizeof(student)) && found==0)
       {
        if(strcmpi(st.retRollNo(),sn)==0)
        {
            found=1;
            if(st.rettoken()==1)
            {
            while(fp1.read((char*)&bk,sizeof(book))&& flag==0)
            {
               if(strcmpi(bk.retBookNo(),st.retstBookNo())==0)
            {
                bk.ShowBook();
                flag=1;
                cout<<"\n\nBook deposited in no. of days: ";
                cin>>day;
                if(day>15)
                {
                   fine=(day-15)*2;
                   cout<<"\n\nFine has to Deposited Rs. "<<fine;
                }
                    st.resettoken();
                    int pos=-1*sizeof(st);
                    fp.seekp(pos,ios::cur);
                    fp.write((char*)&st,sizeof(student));
                    cout<<"\n\n\t 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();
  }




//***************************************************************
//    definition of first function
//****************************************************************

void first()
{
    system("cls");
    cout<<"\n\n";
    cout<<"  XXXXXXXX       XXXX              XXXX         XXXX\n";
        cout<<" XXXXXXXXX       XXXX               XXXX       XXXX\n";
        cout<<"XXX              XXXX                XXXX     XXXX\n";
        cout<<"XXX              XXXX                 XXXX   XXXX\n";    
        cout<<"XXXXXXXXX     XXXXXXXXXX               XXXX XXXX    \n";
        cout<<" XXXXXXXXX    XXXXXXXXXX               XXXXXXXXX\n ";
        cout<<"      XXX       XXXX                  XXXX XXXX\n";
        cout<<"       XXX       XXXX                 XXXX   XXXX\n";
        cout<<"XXXXXXXXX        XXXXXXX    XXX      XXXX     XXXX\n";
        cout<<"XXXXXXXX         XXXXXXX    XXX     XXXX       XXXX\n";
        cout<<"                                   XXXX         XXXX\n";
    cout<<"********************************************************************************";
    cout<<"\n\t\t\t*****LIBRARY MANAGEMENT SYSTEM*****\n";
    cout<<"********************************************************************************";
    cout<<"\nDeveloped By : RAM RANJEET KUMAR    || Roll No.: 17PGMCA03497";
    cout<<"\nCollege : St. XAVIER'S COLLEGE,RANCHI\n";
        cout<<"******************************************************************";
        cout<<"\t\t";  time();
        cout<<"\n\n\t\t\tPLEASE PRESS ANY KEY TO CONTINUE=====>";
    getch();
}



//***************************************************************
//        ADMIN MENU FUNCTION
//****************************************************************

void admin_menu()
{
    system("cls");
    int ch2;
    time();
    cout<<"\n\n\n\t******ADMIN MENU******";
    cout<<"\n\n\t1.ADD A STUDENT DATA";
    cout<<"\n\n\t2.SHOW ALL STUDENTS";
    cout<<"\n\n\t3.SHOW INDIVIDUAL STUDENT DATA ";
    cout<<"\n\n\t4.UPDATE STUDENT DATA";
    cout<<"\n\n\t5.DELETE A STUDENT DATA";
    cout<<"\n\n\t6.ADD A BOOK IN LIBRARY ";
    cout<<"\n\n\t7.SHOW ALL BOOKS";
    cout<<"\n\n\t8.SHOW INDIVIDUAL BOOK";
    cout<<"\n\n\t9.UPDATE A BOOK";
    cout<<"\n\n\t10.DELETE A BOOK DATA";
    cout<<"\n\n\t11.BACK TO MAIN MENU";
    cout<<"\n\n\tPlease Enter Your Choice (1-11)=====> ";
    cin>>ch2;
    switch(ch2)
    {
            case 1: system("cls");
                GetStudent();
            break;
            case 2:PutAllStudent();
            break;
            case 3:
                   char num[30];
                   system("cls");
                   cout<<"\n\n\tPlease Enter The Roll Number in Pattern(17PGMCA03497).";
            cout<<"\nRoll Number: ";
                   cin>>num;
                   ShowIS(num);
                   break;
              case 4: 
            EditStudent();
            break;
              case 5: 
            DeleteStudent();
            break;
        case 6: system("cls");
            GetBook();
            break;
        case 7:
             ShowAllBook();
             break;
        case 8: 
            {
                    char num[30];
                    system("cls");
                    cout<<"\n\n\tPlease Enter The book No. ";
                    cin>>num;
                    ShowIB(num);
                    break;
            }
              case 9: 
            EditBook();
            break;
              case 10:
            DeleteBook();
            break;
             case 11: 
            return;
              default:
            cout<<"\a";
       }
       admin_menu();
}


//***************************************************************
//        THE MAIN FUNCTION OF PROGRAM
//****************************************************************


void main()
{
    char ch;
    first();
    do
    {
    system("cls");
        cout<<"\n\n\n\t*****MAIN MENU*****";
        time();
        cout<<"\n\n\t01. BOOK ISSUE";
        cout<<"\n\n\t02. BOOK DEPOSIT";
          cout<<"\n\n\t03. ADMIN MENU";
          cout<<"\n\n\t04. EXIT";
        
          cout<<"\n\n\tPlease Select Your Option (1-4)=====> ";
          ch=getche();
          switch(ch)
          {
            case '1':
                system("cls");
                BookIssue();
                   break;
              case '2':
                BookDeposit();
                    break;
              case '3':
                admin_menu();
                break;
              case '4':
                exit(0);
              default :
                cout<<"\a";
        }
        }while(ch!='4');
}

//***************************************************************
//                END OF PROJECT
//***************************************************************