プロジェクト事例 1: C++ ベースのライブラリ管理システム
プロジェクトのアイデア
この記事では、シンプルなライブラリ管理システムをコード例として紹介します。
まず、ライブラリ管理システムの各モジュールの機能を見てみましょう。
書籍クラス
図書館管理システムでは、Book
クラスは書籍オブジェクトを表します。次のような特性があります。
title
:書籍名author
:著者名isAvailable
:貸出可能な状態
Book
このクラスは、書籍のプロパティを取得および設定するためのコンストラクターと複数のメンバー関数を提供し、書籍の情報をコンソールに表示できます。
図書館クラス
ライブラリはLibrary
クラスとして表され、次のものが含まれます。
books
: すべての本を保持するベクトル コンテナー
Library
このクラスは、ライブラリを管理するための一連の操作関数を提供します。
addBook
: 新しい本をライブラリに追加します。borrowBook
:指定したタイトルの本を借ります。returnBook
: 指定されたタイトルの本を返します。displayAllBooks
: ライブラリ内のすべての書籍に関する情報を表示します。
ファイル操作機能
ファイル操作をカプセル化するために、このシステムは 2 つの機能を提供します。
readDataFromFile
: 指定されたファイルからデータを読み取り、データの各行を保持する文字列ベクトルを返します。appendDataToFile
: 指定されたデータを指定されたファイルの末尾に追加します。
これらの関数を使用すると、ファイルへのデータの保存とファイルからのデータの読み取りが簡単になります。
メイン機能
main
関数では、Library
オブジェクトをライブラリ インスタンスとして作成します。次に、readDataFromFile
関数を使用して、以前の操作の記録を読み取り、書籍をライブラリに追加します。
次に、円形のメニューを通じてユーザーと対話します。次のオプションが提供されます。
- 本を追加:本のタイトルと著者名を入力して、新しい本をライブラリに追加し、このアクションの記録をファイルに追加します。
- 本を借りる: 借りる本のタイトルを入力し、本が利用可能な場合は非貸出ステータスを設定し、この操作記録をファイルに追加します。
- 本の返却: 返却する本のタイトルを入力します。本が貸出できない場合は、貸出可能に設定し、この操作記録をファイルに追加します。
- すべての書籍を表示: ライブラリ内のすべての書籍に関する詳細情報を表示します。
- プログラムの終了: ライブラリ管理システムを終了します。
本の追加・貸出・返却や、その操作の記録が簡単にできる、シンプルな図書館管理システムです。さらに、図書館内のすべての書籍の詳細情報を簡単に表示できます。
実装コード
#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;
}
}
操作結果:
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