Skip to main content
Notice removed Improve details by coder
Bounty Ended with cariehl's answer chosen by coder
Tweeted twitter.com/StackCodeReview/status/1039982160839667713
Notice added Improve details by coder
Bounty Started worth 50 reputation by coder
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Source Link
coder
  • 2.5k
  • 4
  • 33
  • 60

Library Management using File Handling

I have never written program in file handling. Help me improve this program.

book.h

#ifndef BOOK_H_
#define BOOK_H_

#include <string>

class Book
{
    std::string book_num;
    std::string book_name;
    std::string author_name;

  public:
    Book()                       = default;
    Book(const Book&)            = delete;
    Book &operator=(const Book&) = delete;
    ~Book()                      = default;

    void new_entry();
    void show_book(std::string&, std::string&, std::string&);
    std::string get_book_num() const;
    void show_record();
};

#endif

book.cpp

#include <iostream>
#include <iomanip>
#include <fstream>
#include "book.h"

void Book::new_entry()
{
    std::cin.ignore();
    std::cout << "Enter Book Number : ";
    std::getline(std::cin, book_num);
    std::cout << "\nEnter Book Name : ";
    std::getline(std::cin, book_name);
    std::cout << "\nEnter Author Name : ";
    std::getline(std::cin, author_name);

    std::fstream fp;
    fp.open("Books.dat", std::ios::out | std::ios::app);
    if (fp)
    {
          fp << book_num << " " << book_name << " " << author_name << '\n';
    }
    fp.close();
    std::cout << "Entry Successfull!!\n";
}

void Book::show_book(std::string& b_num, std::string& b_name, std::string& a_name)
{
    std::cout << "Book Number :" << std::setw(10) << b_num << '\n';
    std::cout << "Book Name :  " << std::setw(10) << b_name << '\n';
    std::cout << "Author Name :" << std::setw(10) << a_name << '\n';
}

std::string Book::get_book_num() const
{
    return book_num;
}

void Book::show_record()
{
    std::fstream fp;
    std::string record;
    fp.open("Books.dat", std::ios::in);
    if (!fp)
    {
        std::cerr << "File could not be opened\n";
        return;
    }
    else
    {
        while (fp >> book_num >> book_name >> author_name)
        {
            if (fp.eof())
            {
                break;
            }
            else
            {
                std::cout << book_num << std::setw(50) << book_name << std::setw(50) << author_name << '\n';
            }
        }
    }
    fp.close();
}

student.h

#ifndef STUDENT_H_
#define STUDENT_H_

class Student
{
    std::string roll_num;
    std::string stu_name;
    std::string issued_book_num;
    unsigned int token;

  public:
    Student()                          = default;
    Student(const Student&)            = delete;
    Student &operator=(const Student&) = delete;
    ~Student()                         = default;

    void new_entry();
    void show_stu(std::string&, std::string&, unsigned int, std::string&);
    void reset_issued_book_num();
    void reset_token();
    void show_record();
};
#endif

student.cpp

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include "student.h"

void Student::new_entry()
{
    std::cin.ignore();
    std::cout << "Enter Roll Number : ";
    std::getline(std::cin, roll_num);
    std::cout << "\nEnter Student Name : ";
    std::getline(std::cin, stu_name);
    token = 0;
    issued_book_num = "No";

    std::fstream fp;
    fp.open("Students.dat", std::ios::out | std::ios::app);
    if (fp)
    {
        fp << roll_num << " " << stu_name << " " << token << " " << issued_book_num << '\n';
    }
    fp.close();
    std::cout << "Entry Successfull!!\n";
}

void Student::show_stu(std::string& r_num, std::string& s_name, unsigned int tkn, std::string& issued_b_num)
{
    std::cout << "Roll Number : " << std::setw(10) << r_num << '\n';
    std::cout << "Student Name :" << std::setw(10) << s_name << '\n';
    std::cout << "Books issued :" << std::setw(10) << tkn << '\n';
    if (tkn == 1)
    {
        std::cout << "Book Number :" << std::setw(10) << issued_b_num << '\n';
    }
    std::cout << '\n';
}

void Student::reset_issued_book_num()
{
    issued_book_num = "";
}

void Student::reset_token()
{
    token = 0;
}

void Student::show_record()
{
    std::fstream fp;
    std::string record;
    fp.open("Students.dat", std::ios::in);
    if (!fp)
    {
        std::cerr << "File could not be opened\n";
        return;
    }
    else
    {
        std::string line;
        while (std::getline(fp, line))
        {
            std::stringstream ss(line);
            ss >> roll_num >> stu_name >> token >> issued_book_num;
            std::getline(ss, line);
            if (fp.eof())
            {
                break;
            }
            else
            {
              std::cout << roll_num << "\t\t\t" << stu_name << "\t\t\t" << token << "\t\t\t" << issued_book_num << '\n';
            }
        }
      }
      fp.close();
}

library.h

#ifndef LIBRARY_H_
#define LIBRARY_H_

#include <fstream>
#include <string>
#include "book.h"
#include "student.h"

class Library : public Book, public Student
{
    std::fstream fp;
    //std::ifstream ifs;
    Book book;
    Student student;

  public:
    Library()                          = default;
    Library(const Library&)            = delete;
    Library &operator=(const Library&) = delete;
    ~Library()                         = default;

    void enter_book();
    void enter_stu();
    void display_book(const std::string&);
    void display_stu(const std::string&);
    void delete_book();
    void delete_stu(std::string&);
    void display_all_stu();
    void display_all_book();
    void book_issue();
    void book_deposit();
    void menu();
};
#endif

library.cpp

#include <iostream>
#include <string>
#include <fstream>
#include <boost/algorithm/string.hpp> //case insensitive compare
#include <stdlib.h>
#include <iomanip>
#include <sstream>
#include "library.h"
#include "book.h"
#include "student.h"

void Library::enter_book()
{
    char choice;
    do
    {
        book.new_entry();
        std::cout << "Do you want to add more records? (Y/N)\n";
        std::cin >> choice;
    }while(choice == 'y' || choice == 'Y');
    fp.close();
}

void Library::enter_stu()
{
    char choice;
    do
    {
        student.new_entry();
        std::cout << "Do you want to add more records? (Y/N)\n";
        std::cin >> choice;
    }while(choice == 'y' || choice == 'Y');
    fp.close();
}

void Library::display_book(const std::string& num)
{
    std::cout << "---------------Book Details-------------\n";
    bool exist = false;
    std::string b_num, b_name, a_name;
    fp.open("Books.dat", std::ios::in);
    while(fp >> b_num >> b_name >> a_name)
    {
        if (boost::iequals(b_num, num))
        {
            book.show_book(b_num, b_name, a_name);
            exist = true;
        }
    }
    fp.close();

    if (!exist)
    {
        std::cerr << "Book does not exist\n";
    }
}

void Library::display_stu(const std::string& num)
{
    std::cout << "--------------Student Details-----------------\n";
    bool exist = false;
    std::string r_num, s_name, issued_b_num;
    std::string line, str;
    unsigned int tkn;
    fp.open("Students.dat", std::ios::in);
    while(std::getline(fp, line))
    {
         std::stringstream ss(line);
         ss >> r_num >> s_name >> tkn >> issued_b_num;
         std::getline(ss, str);
        if (boost::iequals(r_num, num))
        {
            student.show_stu(r_num, s_name, tkn, issued_b_num);
            exist = true;
        }
      }
      fp.close();

      if (!exist)
      {
          std::cerr << "Student does not exist\n";
      }
}

void Library::delete_book()
{
    std::string num;
    std::cout << "Delete Book\n";
    std::cout << "Enter number of the book you want to delete : ";
    std::cin.ignore();
    std::getline(std::cin, num);
    std::cout << '\n';
    fp.open("Books.dat", std::ios::in | std::ios::out);
    std::fstream fp1;
    fp1.open("Temp.dat", std::ios::out);
    fp.seekg(0, std::ios::beg);

    std::string b_num, b_name, a_name;

    while(fp >> b_num >> b_name >> a_name)
    {
        if (!boost::iequals(b_num, num))
        {
            fp1 << b_num << " " << b_name << " " << a_name << '\n';
        }
    }
    fp1.close();
    fp.close();
    std::remove("Books.dat");
    std::rename("Temp.dat", "Books.dat");
}

void Library::delete_stu(std::string& num)
{
    fp.open("Students.dat", std::ios::in | std::ios::out);
    std::fstream fp1;
    fp1.open("Temp.dat", std::ios::out);
    fp.seekg(0, std::ios::beg);
    std::string r_num, s_name, issued_b_num;
    int tkn;
    while(fp >> r_num >> s_name >> tkn >> issued_b_num)
    {
        if (!boost::iequals(r_num, num))
        {
            fp1 << r_num << " " << s_name << " " << " " << tkn << " " << issued_b_num << '\n';
        }
    }
    fp1.close();
    fp.close();
    std::remove("Students.dat");
    std::rename("Temp.dat", "Students.dat");
}

void Library::display_all_stu()
{
    std::cout << " ---------------Students List ----------------\n";
    std::cout << "Roll No." << "\t\t\t" << "Name" << "\t\t\t" << "Book Issued" << "\t\t\t" << "Issued Book No.\n";
    student.show_record();
    fp.close();
}

void Library::display_all_book()
{
    std::cout << "-----------------Books List------------------\n";
    std::cout << "Book No." << std::setw(50) << "Name" << std::setw(50) << "Author\n";
    book.show_record();
}

void Library::book_issue()
{
    std::string r_num, b_num; // roll num and book num
    std::string roll_n, s_name, issued_b_num;
    std::string book_n, b_name, a_name;
    unsigned int tkn;
    std::string line, str;
    std::fstream fp1;
    bool found_stu = false;
    bool found_book = false;
    std::cout << "-----------------Book Issue--------------------\n";
    std::cout << "Enter student's roll no. : ";
    std::cin.ignore();
    std::getline(std::cin, r_num);
    std::cout << '\n';
    fp.open("Students.dat", std::ios::in | std::ios::out);
    fp1.open("Books.dat", std::ios::in | std::ios::out);
    int oldPos = fp.tellg();
    while (std::getline(fp, line) && !found_stu)
    {
        std::stringstream ss(line);
        ss >> roll_n >> s_name >> tkn >> issued_b_num;
        std::getline(ss, line);
        if (boost::iequals(roll_n, r_num))
        {
            found_stu = true;
            if (tkn == 0)
            {
                std::cout << "Enter Book No. : ";
                std::getline(std::cin, b_num);
                while (fp1 >> book_n >> b_name >> a_name && !found_book)
                {
                    if (boost::iequals(book_n, b_num))
                    {
                        book.show_book(book_n, b_name, a_name);
                        found_book = true;
                        tkn = 1;
                        student.reset_issued_book_num();
                        issued_b_num = book_n;
                        fp.seekg(oldPos);
                       fp << roll_n << " " << s_name << " " << tkn << " " << issued_b_num << '\n';
                        std::cout << "Book Issued Successfully\n";
                        break;
                    }
                }
                if (!found_book)
                {
                    std::cerr << "Book does not exist\n";
                }
            }
        }
    }
    if (!found_stu)
    {
        std::cout << "Student record does not exist\n";
    }
    fp.close();
    fp1.close();
}

void Library::book_deposit()
{
    std::string r_num, b_num; // roll num and book num
    std::string roll_n, s_name, issued_b_num;
    std::string book_n, b_name, a_name;
    unsigned int tkn;
    std::string line, str;
    std::fstream fp1;
    int days, fine;
    bool found_stu = false;
    bool found_book = false;
    std::cout << "-----------------Book Deposit---------------------\n";
    std::cout << "Enter student's roll no. : ";
    std::cin.ignore();
    std::getline(std::cin, r_num);
    std::cout << '\n';
    fp.open("Students.dat", std::ios::in | std::ios::out);
    fp1.open("Books.dat", std::ios::in | std::ios::out);

    while (std::getline(fp, line) && !found_stu)
    {
        std::stringstream ss(line);
        ss >> roll_n >> s_name >> tkn >> issued_b_num;
        std::cout << "IBN " << issued_b_num << '\n';
        std::getline(ss, line);
        if (boost::iequals(roll_n, r_num))
        {
            found_stu = true;
            if (tkn == 1)
            {
                while (fp1 >> book_n >> b_name >> a_name && !found_book)
                {
                    if (boost::iequals(book_n, issued_b_num))
                    {
                        book.show_book(book_n, b_name, a_name);
                        found_book = true;
                        std::cout << "Book deposited in no. of days : ";
                        std::cin >> days;
                        if (days > 15)
                        {
                            fine = days - 15;
                            std::cout << "Fine has to be deposited : " << fine << '\n';
                        }
                        student.reset_token();
                        int pos = -1 * sizeof("Students.dat");
                        fp.seekp(pos, std::ios::cur);
                        fp << roll_n << s_name << tkn << issued_b_num;
                        std::cout << "Book Deposited Successfully\n";
                    }
                }
                if (!found_book)
                {
                    std::cerr << "Book does not exist\n";
                }
            }
        }
    }
    if (!found_stu)
    {
        std::cout << "Student record does not exist\n";
    }
    fp.close();
    fp1.close();
}

void Library::menu()
{
    int choice;
    std::cout << "Menu\n";
    std::cout << "1. Create Student Record\n";
    std::cout << "2. Display all Students Record\n";
    std::cout << "3. Display Specific Student Record\n";
    std::cout << "4. Delete Student Record\n";

    std::cout << "5. Enter Book Record\n";
    std::cout << "6. Display all Books\n";
    std::cout << "7. Display Specific Book\n";
    std::cout << "8. Delete Book\n";

    std::cout << "9. Back to Main Menu\n";
    std::cout << "Enter your choice\n";
    std::cin >> choice;

    switch(choice)
    {
        case 1: enter_stu();
                break;

        case 2: display_all_stu();
                break;

        case 3: {
                    std::string num;
                    std::cout << "Enter Roll No.\n";
                    std::cin.ignore();
                    std::getline(std::cin, num);
                    display_stu(num);
                }
                break;

        case 4: {
                    std::string num;
                    std::cout << "Delete Student\n";
                    std::cout << "Enter roll number of the student you want to delete : ";
                    std::cin.ignore();
                    std::getline(std::cin, num);
                    delete_stu(num);
                }
                break;

        case 5: enter_book();
                break;

        case 6: display_all_book();
                break;

        case 7: {
                    std::string num;
                    std::cout << "Enter Book No.\n";
                    std::cin.ignore();
                    std::getline(std::cin, num);
                    display_book(num);
                }
                break;

        case 8: delete_book();
                break;

        case 10: return;

        default: std::cout << "Please enter any of these choices\n";
                 break;
    }
}

main.cpp

#include <iostream>
#include <stdlib.h>
#include "library.h"

int main()
{
    Library lib;
    char choice;
    do
    {
        std::cout << "1. Book Issue\n";
        std::cout << "2. Book Deposit\n";
        std::cout << "3. Menu\n";
        std::cout << "4. Exit\n";

        std::cout << "Enter your option\n";
        std::cin >> choice;
        switch(choice)
        {
            case '1': lib.book_issue();
                      break;

            case '2': lib.book_deposit();
                      break;

            case '3': lib.menu();
                      break;

            case '4': exit(0) ;

            default: std::cout << "Enter one of these choice\n";
        }
    } while(choice != '4');

}