题目描述:
设计一个简单的图书管理系统,需要实现以下功能:
- 添加书籍:输入书名、作者、出版社、出版日期等信息,添加一本新书。
- 删除书籍:输入书号或书名,删除一本已有的书籍。
- 查询书籍:输入书号或书名,查询一本已有的书籍。
- 显示所有书籍:按照书号排序,输出图书馆中所有的书籍。
设计思路:
为了实现这个图书管理系统,需要设计以下几个类:
- Book类:用于储存书籍信息,包括书名、作者、出版社、出版日期等属性,以及一些方法,如获取与设置书籍信息。
- Library类:用于储存所有的书籍,包括添加书籍、删除书籍、查询书籍、显示所有书籍等方法。
- Menu类:用于显示菜单,接收用户输入,并调用相应的Library类中的方法。
程序流程图:
Copy Codestart --> displayMenu --> getUserChoice --> performAction --> end
| |
v v
addBook removeBook
| |
v v
search display
代码实现:
#include <bits/stdc++.h>标签:cout,title,Book,books,周二,打卡,书籍,string From: https://www.cnblogs.com/zeyangshuaige/p/17353543.html
using namespace std;
class Book {
private:
string title;
string author;
string publisher;
string publicationDate;
public:
Book(string t, string a, string p, string pd) :
title(t), author(a), publisher(p), publicationDate(pd) {}
string getTitle() { return title; }
string getAuthor() { return author; }
string getPublisher() { return publisher; }
string getPublicationDate() { return publicationDate; }
};
class Library {
private:
vector<Book> books;
public:
void addBook(Book book) {
books.push_back(book);
}
void removeBook(string bookTitle) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getTitle() == bookTitle) {
books.erase(books.begin() + i);
break;
}
}
}
Book searchBook(string bookTitle) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getTitle() == bookTitle) {
return books[i];
}
}
// 如果没找到书籍,则返回一个空的Book对象
return Book("", "", "", "");
}
void displayAllBooks() {
// 按照书名排序
sort(books.begin(), books.end(), [](Book a, Book b) {
return a.getTitle() < b.getTitle();
});
// 输出所有的书籍
for (Book book : books) {
cout << "Title: " << book.getTitle() << endl;
cout << "Author: " << book.getAuthor() << endl;
cout << "Publisher: " << book.getPublisher() << endl;
cout << "Publication Date: " << book.getPublicationDate() << endl << endl;
}
}
};
class Menu {
private:
Library library;
public:
Menu() {
library = Library();
}
void displayMenu() {
cout << "1. Add a book" << endl;
cout << "2. Remove a book" << endl;
cout << "3. Search for a book" << endl;
cout << "4. Display all books" << endl;
cout << "5. Quit" << endl << endl;
}
int getUserChoice() {
int choice;
cout << "Enter your choice: ";
cin >> choice;
cout << endl;
return choice;
}
void performAction(int choice) {
string title, author, publisher, publicationDate;
Book book("", "", "", "");
switch (choice) {
case 1:
cout << "Enter the title of the book: ";
getline(cin >> ws, title);
cout << "Enter the author of the book: ";
getline(cin >> ws, author);
cout << "Enter the publisher of the book: ";
getline(cin >> ws, publisher);
cout << "Enter the publication date of the book: ";
getline(cin >> ws, publicationDate);
book = Book(title, author, publisher, publicationDate);
library.addBook(book);
cout << title << " has been added to the library." << endl << endl;
break;
case 2:
cout << "Enter the title of the book you want to remove: ";
getline(cin >> ws, title);
library.removeBook(title);
cout << title << " has been removed from the library." << endl << endl;
break;
case 3:
cout << "Enter the title of the book you want to search for: ";
getline(cin >> ws, title);
book = library.searchBook(title);
if (book.getTitle() != "") {
cout << "Title: " << book.getTitle() << endl;
cout << "Author: " << book.getAuthor() << endl;
cout << "Publisher: " << book.getPublisher() << endl;
cout << "Publication Date: " << book.getPublicationDate() << endl << endl;
} else {
cout << title << " is not in the library." << endl << endl;
}
break;
case 4:
library.displayAllBooks();
break;
case 5:
exit(0);
break;
default:
cout << "Invalid choice. Please try again." << endl << endl;
}
}
};
int main() {
Menu menu = Menu();
int choice;
while (true) {
menu.displayMenu();
choice = menu.getUserChoice();
menu.performAction(choice);
}
return 0;
}