Project Case 1: Library Management System Based on C++

Project Case 1: Library Management System Based on C++

Project ideas

This article will introduce you to a simple library management system, presented as a code example.

First, let's take a look at the functions of each module in the library management system.

Book class

In a library management system, Booka class represents a book object. It has the following properties:

  • title: Book title
  • author: author name
  • isAvailable: Borrowable state

BookThe class provides a constructor and multiple member functions for obtaining and setting book properties, and can display book information to the console.

Library class

A library is represented as Librarya class, which contains the following:

  • books: vector container holding all books

LibraryThe class provides a set of operation functions for managing the library:

  1. addBook: Adds a new book to the library.
  2. borrowBook: Borrow books with the specified title.
  3. returnBook: Return the book with the specified title.
  4. displayAllBooks: Displays information about all books in the library.

file manipulation function

In order to encapsulate file operations, this system provides two functions:

  • readDataFromFile: Reads data from the specified file and returns a string vector holding each row of data.
  • appendDataToFile: Append the given data to the end of the specified file.

These functions make it easy to save data to and read data from files.

main function

In mainthe function, we create a Libraryobject as the library instance. Then use readDataFromFilethe function to read the record of the previous operation and add the book to the library.

Next, interact with the user through a circular menu. The following options are provided:

  1. Add Book: Enter a book title and author name to add a new book to the library and append a record of this action to the file.
  2. Borrowing books: Enter the title of the book to be borrowed, if the book is available, set its non-borrowing status, and append this operation record to the file.
  3. Return book: Enter the title of the book to be returned, if the book is not available for borrowing, set it as available, and append this operation record to the file.
  4. Show All Books: Displays detailed information about all books in the library.
  5. Exit program: Ends the library management system.

With this simple library management system, you can easily add, borrow and return books, and keep records of related operations. In addition, you can conveniently view the detailed information of all books in the library.

Implementation code

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

// Book类表示图书对象
class Book {
    
    
private:
    string title;  // 图书标题
    string author;  // 作者
    bool isAvailable;  // 可借状态

public:
    Book(const string& title, const string& author)
        : title(title), author(author), isAvailable(true) {
    
    }

    string getTitle() const {
    
     return title; }  // 获取图书标题
    string getAuthor() const {
    
     return author; }  // 获取作者
    bool getAvailability() const {
    
     return isAvailable; }  // 获取可借状态
    void setAvailability(bool availability) {
    
     isAvailable = availability; }  // 设置可借状态

    // 输出图书信息到控制台
    void display() const {
    
    
        cout << "Title: " << title << endl;
        cout << "Author: " << author << endl;
        string availability = isAvailable ? "Available" : "Not Available";
        cout << "Availability: " << availability << endl;
    }
};

// Library类表示整个图书馆
class Library {
    
    
private:
    vector<Book> books;  // 图书集合

public:
    // 添加图书到图书馆
    void addBook(const Book& book) {
    
    
        books.push_back(book);
    }

    // 借阅图书
    void borrowBook(const string& title) {
    
    
        for (auto& book : books) {
    
    
            if (book.getTitle() == title && book.getAvailability()) {
    
    
                book.setAvailability(false);
                break;
            }
        }
    }

    // 归还图书
    void returnBook(const string& title) {
    
    
        for (auto& book : books) {
    
    
            if (book.getTitle() == title && !book.getAvailability()) {
    
    
                book.setAvailability(true);
                break;
            }
        }
    }

    // 显示所有图书信息
    void displayAllBooks() const {
    
    
        for (const auto& book : books) {
    
    
            book.display();
            cout << endl;
        }
    }
};

// 从文件中读取数据
vector<string> readDataFromFile(const string& filename) {
    
    
    ifstream file(filename);  // 打开文件流
    vector<string> data;

    if (file.is_open()) {
    
    
        string line;
        while (getline(file, line)) {
    
      // 逐行读取文件内容
            data.push_back(line);
        }
        file.close();  // 关闭文件流
    }

    return data;
}

// 将数据追加到文件末尾
void appendDataToFile(const string& filename, const string& data) {
    
    
    ofstream file(filename, ios::app);  // 打开文件流,并将写入位置设为文件末尾
    if (file.is_open()) {
    
    
        file << data << endl;  // 写入数据
        file.close();  // 关闭文件流
    }
}

int main() {
    
    
    Library library;  // 创建 Library 对象

    vector<string> previousData = readDataFromFile("library.txt");  // 读取之前的操作记录

    // 添加之前的图书到图书馆
    for (const auto& line : previousData) {
    
    
        istringstream iss(line);  // 将每行数据拆分为标题和作者
        string title, author;
        getline(iss, title, ',');
        getline(iss, author);
        library.addBook(Book(title, author));
    }

    int choice;
    while (true) {
    
    
        cout << "Library Management System" << endl;
        cout << "1. Add Book" << endl;
        cout << "2. Borrow Book" << endl;
        cout << "3. Return Book" << endl;
        cout << "4. Display All Books" << endl;
        cout << "5. Exit" << endl;
        cout << "Enter your choice: ";
        cin >> choice;

        switch (choice) {
    
    
            case 1: {
    
    
                cout << "Enter book title: ";
                cin.ignore();  // 忽略之前的输入缓冲区
                string title;
                getline(cin, title);

                cout << "Enter author name: ";
                string author;
                getline(cin, author);

                library.addBook(Book(title, author));
                appendDataToFile("library.txt", "Added Book - Title: " + title + ", Author: " + author);
                break;
            }
            case 2: {
    
    
                cout << "Enter book title to borrow: ";
                cin.ignore();
                string title;
                getline(cin, title);

                library.borrowBook(title);
                appendDataToFile("library.txt", "Borrowed Book - Title: " + title);
                break;
            }
            case 3: {
    
    
                cout << "Enter book title to return: ";
                cin.ignore();
                string title;
                getline(cin, title);

                library.returnBook(title);
                appendDataToFile("library.txt", "Returned Book - Title: " + title);
                break;
            }
            case 4:
                library.displayAllBooks();
                break;
            case 5:
                return 0;
            default:
                cout << "Invalid choice. Please try again." << endl;
        }

        cout << endl;
    }
}

operation result:

Library Management System
1. Add Book
2. Borrow Book
3. Return Book
4. Display All Books
5. Exit
Enter your choice: 1
Enter book title: Harry Potter and the Philosopher's Stone
Enter author name: J.K. Rowling

Library Management System
1. Add Book
2. Borrow Book
3. Return Book
4. Display All Books
5. Exit
Enter your choice: 1
Enter book title: To Kill a Mockingbird
Enter author name: Harper Lee

Library Management System
1. Add Book
2. Borrow Book
3. Return Book
4. Display All Books
5. Exit
Enter your choice: 2
Enter book title to borrow: To Kill a Mockingbird

Library Management System
1. Add Book
2. Borrow Book
3. Return Book
4. Display All Books
5. Exit
Enter your choice: 4
Title: Harry Potter and the Philosopher's Stone
Author: J.K. Rowling
Availability: Available

Title: To Kill a Mockingbird
Author: Harper Lee
Availability: Not Available

Library Management System
1. Add Book
2. Borrow Book
3. Return Book
4. Display All Books
5. Exit
Enter your choice: 3
Enter book title to return: To Kill a Mockingbird

Library Management System
1. Add Book
2. Borrow Book
3. Return Book
4. Display All Books
5. Exit
Enter your choice: 4
Title: Harry Potter and the Philosopher's Stone
Author: J.K. Rowling
Availability: Available

Title: To Kill a Mockingbird
Author: Harper Lee
Availability: Available

Library Management System
1. Add Book
2. Borrow Book
3. Return Book
4. Display All Books
5. Exit
Enter your choice: 5

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/132241551