C++语言程序设计:图书收藏夹管理系统

1.课题简介

目前有一些著名的网上图书购买系统,比如当当网、亚马逊等,他们都有收藏夹、购物车以及书库管理的功能,通过收藏夹可以把你感兴趣的图书添加到收藏夹,也可以把想购买的图书添加到购物车,同时需要对书库中的图书进行管理。

2.课题预计目标

2.1【收藏新的图书】

  1. 首先显示现有收藏夹中的所有图书信息。
  2. 从键盘输入图书信息,把准备收藏的一本新的图书加入收藏夹。
  3. 如果收藏夹中已有相同图书号的图书,则不能重复收藏,并提示收藏失败的信息。
  4. 若可以收藏,则显示收藏到收藏夹的这本图书的信息。
  5. 最后显示收藏成功后收藏夹中的所有图书信息。

2.2【查询收藏夹图书】

  1. 首先显示现有收藏夹中的所有图书信息。
  2. 从键盘输入待查询的图书号,并按照指定图书号查询显示收藏夹中该图书号的图书信息。
  3. 若查询失败,则显示查询失败信息;否则显示查询成功信息,并显示该图书号的图书信息。

2.3【按收藏日期显示图书】

  1. 首先显示现有收藏夹中的所有图书信息。
  2. 对现有收藏夹的所有图书按照收藏日期的先后显示输出收藏夹中的所有图书信息。

2.4【按价格显示所有图书】

  1. 首先显示现有收藏夹中的所有图书信息。
  2. 对现有收藏夹的所有图书按照价格的从高到低显示输出收藏夹中的所有图书信息。
  3. 最后显示输出现有收藏夹中总共收藏了多少本图书。

2.5【移出收藏夹】

  1. 首先显示现有收藏夹中的所有图书信息。
  2. 从键盘输入待移出的图书号,并把该指定图书号的图书移出收藏夹。
  3. 若找不到待移出的图书,则显示移出图书操作失败的提示信息;否则显示该图书号的图书信息,然后移出该图书,并显示成功移出的提示信息,最后显示输出成功移出后收藏夹中的所有图书信息。

3.系统E-R图

在这里插入图片描述

4.编码设计

4.1 图书类和图书收藏夹类定义

// 图书类定义
class Book {
private:
    string bookNumber;//图书编号
    string title;//图书名字
    string author;//图书作者
    string publisher;//图书出版社
    string publicationDate;//图书出版日期
    double price;//图书价格
    string collectionDate;//图书收藏日期

public:
    // 构造函数
    Book(string  number, string bookTitle, string bookAuthor,
        string bookPublisher, string pubDate, double bookPrice,
        string collectDate)
        : bookNumber(number),
        title(bookTitle),
        author(bookAuthor),
        publisher(bookPublisher),
        publicationDate(pubDate),
        price(bookPrice),
        collectionDate(collectDate) {}
// 图书收藏夹类定义
class BookCollection {
private:
    vector<Book> books;//Book类型的vector容器,方便存储图书信息

4.2 实现添加图书

 // 添加新的图书到收藏夹
    void addBook() {
        string number;
        string title, author, publisher;
        string pubDate;
        string collectDate;
        double price;

        cout << "请输入新图书的信息:" << endl;
        cout << "图书号: ";//我们规定图书号是4位数
        while (true) {
            cin >> number;
            if (number.size() != 4) {
                cout << "编号不规范,请你重新输入" << endl;
            }
            else {
                break;
            }
        }
        // 检查图书号是否已存在
        //checkDuplicateBook函数另外定义
        if (checkDuplicateBook(number)) {
            cout << endl;
            cout << "图书号已存在,请重新输入图书信息。" << endl;
            return;
        }
        cout << "书名: ";
        cin >> title;
        
        cout << "作者: ";
        cin >> author;

        cout << "出版社: ";
        cin >> publisher;

        cout << "出版日期 (YYYY-MM-DD): ";
        cin >> pubDate;

        cout << "价格: ";
        while (true)
        {
            cin >> price;
            if (price <= 0) {
                cout << "输入的价格不规范,请重新输入" << endl;
            }
            else{
                break;
            }
        }

        cout << "收藏日期 (YYYY-MM-DD): ";
        cin >> collectDate;

        // 创建新图书对象并添加到收藏夹
        Book newBook(number, title, author, publisher, pubDate, price, collectDate);
        //vector迭代器装的是对象
        books.push_back(newBook);
        cout << endl;
        cout << "成功添加图书到收藏夹,收藏夹中这本图书的信息为:" << endl;
        newBook.displayInfo();
    }

4.3 实现移出图书

void removeBook() {
        if (books.empty()) {
            cout << "收藏夹中暂无图书信息。" << endl;
            return;
        }
        string number;
        cout << "请输入要移出的图书号: ";
        cin >> number;
        auto it = find_if(books.begin(), books.end(), [&number](Book& book) {return book.getBookNumber() == number; });
        
        if (it != books.end()) {
            cout << "移出的图书信息如下:" << endl;
            it->displayInfo();
            cout << "成功移出指定图书。" << endl;
            books.erase(it);
        }
        else {
            cout << "移出图书操作失败,找不到指定图书号的图书。" << endl;
        }

        cout << "成功移出后收藏夹中的图书信息:" << endl;
        displayAllBooks();
    }

find_if()的解释如下:
使用Lambda表达式的find_if函数调用,它在books容器中查找带有指定编号的图书。Lambda表达式中的[number]表示将外部变量number捕获到Lambda表达式中,以便在Lambda表达式内部使用。Lambda表达式内部的函数返回一个布尔值,指示当前迭代器指向的Book对象是否与给定的number相等。最终,find_if函数返回一个迭代器,指向找到的图书对象(如果找到了)或books.end()(如果未找到)。

4.4 实现查询图书

 void searchBook() {

        if (books.empty()) {
            cout << "收藏夹中暂无图书信息。" << endl;
            return;
        }

        string number;
        cout << "请输入要查询的图书号: ";
        cin >> number;

        //auto可以自动推断返回值的类型
        auto it = find_if(books.begin(), books.end(), [&number](Book& book) {return book.getBookNumber() == number; });

        if (it != books.end()) {
            cout << "查找成功,该本图书信息如下:" << endl;
            it->displayInfo();
       }
       
        else {
            cout << "查找失败!" << endl;
        }
    }

4.5 实现按收藏日期升序排序图书

 void sortByCollectionDate() {
        if (books.empty()) {
            cout << "收藏夹中暂无图书信息。" << endl;
            return;
        }

        vector<Book>sortCollectionDatebook = books;
        sort(sortCollectionDatebook.begin(), 
        sortCollectionDatebook.end(), [](Book& b1, Book& b2) {
            return compareDates(b1.getCollectionDate(),
             b2.getCollectionDate());
            });

        cout << "图书已按收藏日期排序 :" << endl;
        for (auto& book : sortCollectionDatebook) {
            book.displayInfo();
            cout << endl;
        }

    }

这段代码使用 C++ 的 sort 函数对一个 Book 类型的集合进行排序,排序规则是按照每本书的 collectionDate 属性来进行比较。其中,compareDates() 函数用于比较两个日期的大小。

函数的第三个参数是一个 Lambda 表达式,表示一个匿名的比较函数。该函数接受两个 Book 对象引用作为参数,并返回一个 bool 值。当第一个参数 b1collectionDate 属性小于第二个参数 b2collectionDate 属性时,该函数返回 true,表示 b1 应该排在 b2 前面,否则返回 false,表示 b1 应该排在 b2 后面。

sort 函数执行完毕后,sortCollectionDatebook 集合中的元素就按照collectionDate 属性从早到晚排序完毕了。

4.6 实现按价格降序排序排序图书

  void displayBooksByPrice() {
        if (books.empty()) {
            cout << "收藏夹中暂无图书信息。" << endl;
            return;
        }

        cout << "现有收藏夹中的图书信息:" << endl;
        displayAllBooks();

        cout << "按价格从高到低显示图书信息:" << endl;

        vector<Book> sortedPriceBooks = books;
        sort(sortedPriceBooks.begin(), sortedPriceBooks.end(), [](Book& b1, Book& b2) {
            return b1.getBookPrice() > b2.getBookPrice();
            });

        for (auto& book : sortedPriceBooks) {
            book.displayInfo();
            cout << endl;
        }
        auto num = books.size();

        cout << "现有收藏夹总收藏了多少本图书 : " << num << endl;


    }

首先判断收藏夹是否为空,如果为空则输出相关提示信息并返回。
然后调用displayAllBooks()函数,把收藏夹中的所有图书信息输出一遍,作为原始的图书列表。
接着使用STL中的sort()函数对收藏夹中的图书按照价格从高到低排序,排序方法通过lambda表达式实现。将排序后的结果存储到名为sortedPriceBooks的vector容器中。
最后使用循环遍历sortedPriceBooks中的图书,调用每个图书的displayInfo()函数输出图书的详细信息,并在每个图书输出完后输出换行符。最后输出收藏夹中总共收藏的图书数量。

图书收藏夹管理系统源代码

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>

using namespace std;

// 图书类定义
class Book {
private:
    string bookNumber;//图书编号
    string title;//图书名字
    string author;//图书作者
    string publisher;//图书出版社
    string publicationDate;//图书出版日期
    double price;//图书价格
    string collectionDate;//图书收藏日期

public:
    // 构造函数
    Book(string  number, string bookTitle, string bookAuthor,
        string bookPublisher, string pubDate, double bookPrice,
        string collectDate)
        : bookNumber(number),
        title(bookTitle),
        author(bookAuthor),
        publisher(bookPublisher),
        publicationDate(pubDate),
        price(bookPrice),
        collectionDate(collectDate) {}

  /*  bool operator ==(const Book& other) {
        return bookNumber == other.bookNumber;
    }*/
    // 获取图书号
    string getBookNumber() {
        return bookNumber;
    }
    //获取图书价格
    double getBookPrice() {
        return price;
    }
    //获取图书收藏日期
    string getCollectionDate() {
        return collectionDate;
    }
    //获取图书作者
    string getPublisher() {
        return publisher;
    }

    // 显示图书信息
    void displayInfo() {
        cout << "图书号: " << bookNumber << endl;
        cout << "书名: " << title << endl;
        cout << "作者: " << author << endl;
        cout << "出版社: " << publisher << endl;
        cout << "出版日期: " << publicationDate << endl;
        cout << "价格: " << price << endl;
        cout << "收藏日期: " << collectionDate << endl;
    }
    bool comp(int num) {
        return num;
    }
};

bool compareDates(const string& date1, const string& date2) {
    // 提取日期字符串中的年、月、日
    stringstream ss1(date1);
    int year1, month1, day1;
    char delimiter;
    ss1 >> year1 >> delimiter >> month1 >> delimiter >> day1;

    stringstream ss2(date2);
    int year2, month2, day2;
    ss2 >> year2 >> delimiter >> month2 >> delimiter >> day2;

    // 比较年份
    if (year1 < year2) {
        return true;
    }
    else if (year1 > year2) {
        return false;
    }

    // 比较月份
    if (month1 < month2) {
        return true;
    }
    else if (month1 > month2) {
        return false;
    }

    // 比较日期
    if (day1 < day2) {
        return true;
    }
    else if (day1 > day2) {
        return false;
    }

    // 日期相等
    return false;
}

// 图书收藏夹类定义
class BookCollection {
private:
    vector<Book> books;

public:
    // 添加新的图书到收藏夹
    void addBook() {

        string number;
        string title, author, publisher;
        string pubDate;
        string collectDate;
        double price;

        cout << "请输入新图书的信息:" << endl;
        cout << "图书号: ";
        while (true) {
            cin >> number;
            if (number.size() != 4) {
                cout << "编号不规范,请你重新输入" << endl;
            }
            else {
                break;
            }
        }

        // 检查图书号是否已存在
        if (checkDuplicateBook(number)) {
            cout << endl;
            cout << "图书号已存在,请重新输入图书信息。" << endl;
            return;
        }


        cout << "书名: ";
        cin >> title;

        cout << "作者: ";
        cin >> author;

        cout << "出版社: ";
        cin >> publisher;

        cout << "出版日期 (YYYY-MM-DD): ";
        cin >> pubDate;

        cout << "价格: ";
        while (true)
        {
            cin >> price;
            if (price <= 0) {
                cout << "输入的价格不规范,请重新输入" << endl;
            }
            else{
                break;
            }
        }

        cout << "收藏日期 (YYYY-MM-DD): ";
        cin >> collectDate;

        // 创建新图书对象并添加到收藏夹
        Book newBook(number, title, author, publisher, pubDate, price, collectDate);
        //vector迭代器装的是对象
        books.push_back(newBook);
        cout << endl;
        cout << "成功添加图书到收藏夹,收藏夹中这本图书的信息为:" << endl;
        newBook.displayInfo();
    }

    // 显示收藏夹中的所有图书信息
    void displayAllBooks() {
        if (books.empty()) {
            cout << "收藏夹中暂无图书信息。" << endl;
        }
        else {
            cout << "收藏夹中的图书信息如下:" << endl;
            for (Book& book : books) {
                book.displayInfo();
                cout << endl;
            }
        }
    }
    void searchBook() {

        if (books.empty()) {
            cout << "收藏夹中暂无图书信息。" << endl;
            return;
        }

        string number;
        cout << "请输入要查询的图书号: ";
        cin >> number;

        //auto可以自动推断返回值的类型
        auto it = find_if(books.begin(), books.end(), [&number](Book& book) {return book.getBookNumber() == number; });

        if (it != books.end()) {
            cout << "查找成功,该本图书信息如下:" << endl;
            it->displayInfo();
       }
        else {
            cout << "查找失败!" << endl;
        }
    }

    // 按收藏日期排序图书,sort类似以快排,第三个参数是排序准则,这里使用匿名函数做排序准则
    // 如果没有匿名函数做排序准则,则sort函数默认元素从小到大排序
    void sortByCollectionDate() {
        if (books.empty()) {
            cout << "收藏夹中暂无图书信息。" << endl;
            return;
        }

        vector<Book>sortCollectionDatebook = books;
        sort(sortCollectionDatebook.begin(), sortCollectionDatebook.end(), [](Book& b1, Book& b2) {
            return compareDates(b1.getCollectionDate(), b2.getCollectionDate());
            });

        cout << "图书已按收藏日期排序 :" << endl;
        for (auto& book : sortCollectionDatebook) {
            book.displayInfo();
            cout << endl;
        }

    }
    // 按价格从高到低显示图书信息
    void displayBooksByPrice() {
        if (books.empty()) {
            cout << "收藏夹中暂无图书信息。" << endl;
            return;
        }

        cout << "现有收藏夹中的图书信息:" << endl;
        displayAllBooks();

        cout << "按价格从高到低显示图书信息:" << endl;

        vector<Book> sortedPriceBooks = books;
        sort(sortedPriceBooks.begin(), sortedPriceBooks.end(), [](Book& b1, Book& b2) {
            return b1.getBookPrice() > b2.getBookPrice();
            });

        for (auto& book : sortedPriceBooks) {
            book.displayInfo();
            cout << endl;
        }
        auto num = books.size();

        cout << "现有收藏夹总收藏了多少本图书 : " << num << endl;


    }
    void removeBook() {
        if (books.empty()) {
            cout << "收藏夹中暂无图书信息。" << endl;
            return;
        }
        string number;
        cout << "请输入要移出的图书号: ";
        cin >> number;

        auto it = find_if(books.begin(), books.end(), [&number](Book& book) {return book.getBookNumber() == number; });


        if (it != books.end()) {
            cout << "移出的图书信息如下:" << endl;
            it->displayInfo();
            cout << "成功移出指定图书。" << endl;
          
            books.erase(it);
        }
        else {
            cout << "移出图书操作失败,找不到指定图书号的图书。" << endl;
        }

        cout << "成功移出后收藏夹中的图书信息:" << endl;
        displayAllBooks();
    }
    // 检查图书号是否已存在
    bool checkDuplicateBook(string bookNumber) {
        for (Book& book : books) {
            if (book.getBookNumber() == bookNumber) {
                return true;
            }
        }
        return false;
    }


};


int main() {
    BookCollection collection;

    int choice;
    do {
        cout << "<***** 图书收藏夹管理系统 *****>" << endl;
        cout << "1. 添加新图书" << endl;
        cout << "2. 查询收藏夹图书" << endl;
        cout << "3. 按收藏日期排序图书" << endl;
        cout << "4. 按价格显示所有图书" << endl;
        cout << "5. 移出收藏夹" << endl;
        cout << "0. 退出" << endl;
        cout << "请选择操作: ";
        cin >> choice;
        switch (choice) {
        case 1:
            collection.addBook();
            break;
        case 2:
            collection.searchBook();
            break;
        case 3:
            collection.sortByCollectionDate();
            break;
        case 4:
            collection.displayBooksByPrice();
            break;
        case 5:
            collection.removeBook();
            break;
        case 0:
            cout << "程序已退出。" << endl;
            break;
        default:
            cout << "无效的选择,请重新选择操作。" << endl;
            break;
        }

        cout << endl;
    } while (choice != 0);

    return 0;
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值