书店管理系统

本文介绍了一种使用C++编程语言实现的书店管理系统。该系统涵盖了数学建模的原理,通过算法优化实现了书籍库存管理、销售记录和客户查询等功能,旨在提高书店运营效率。
摘要由CSDN通过智能技术生成
#include <iostream>
#include <fstream>
#include <cstring>
#include <ctime>
#include <cstdlib>

using namespace std;

class Book {
private:
    string title, author, publisher, time;
    int pages, quantity;
    float price;

public:
    Book() {
        // 初始化
        title = "";
        author = "";
        publisher = "";
        time = "";
        pages = 0;
        quantity = 0;
        price = 0.0;
    }

    void setTitle(string title) 
    { 
        this->title = title; 
    }
    void setAuthor(string author) 
    {
        this->author = author; 
    }
    void setPublisher(string publisher)
    { 
        this->publisher = publisher; 
    }
    void setTime(string time) 
    { 
        this->time = time;
    }
    void setPages(int pages) 
    { 
        this->pages = pages;
    }
    void setQuantity(int quantity)
    { 
        this->quantity = quantity;
    }
    void setPrice(float price) 
    { 
        this->price = price;
    }

    string getTitle() 
    { 
        return title;
    }
    string getAuthor() 
    {
        return author;
    }
    string getPublisher()
    { 
        return publisher; 
    }
    string getTime()
    { 
        return time;
    }
    int getPages()
    {
        return pages;
    }
    int getQuantity() 
    { 
        return quantity;
    }
    float getPrice() 
    { 
        return price; 
    }
};

class BookStore {
private:
    Book books[1000];
    int count;

public:
    BookStore() {
        // 初始化
        count = 0;
    }

    void addBook() {
        // 添加书籍
        Book b;
        string title, author, publisher, time;
        int pages, quantity;
        float price;
        cout << "\n请输入书籍信息:" << endl;
        cout << "书名:";
        cin >> title;
        cout << "作者:";
        cin >> author;
        cout << "出版社:";
        cin >> publisher;
        cout << "页数:";
        cin >> pages;
        cout << "单价:";
        cin >> price;
        cout << "入库时间:";
        cin >> time;
        cout << "库存量:";
        cin >> quantity;
        b.setTitle(title);
        b.setAuthor(author);
        b.setPublisher(publisher);
        b.setPages(pages);
        b.setPrice(price);
        b.setTime(time);
        b.setQuantity(quantity);
        books[count++] = b;
        // 将书籍信息写入文件中
        ofstream out("books.txt", ios::app);
        out << b.getTitle() << "," << b.getAuthor() << "," << b.getPublisher() << "," << b.getPages() << "," << b.getPrice() << "," << b.getTime() << "," << b.getQuantity() << endl;
        cout << "添加成功!" << endl;
    }

    void sellBook() {
        // 出售书籍
        string title;
        cout << "\n请输入出售的书籍名称:";
        cin >> title;
        for (int i = 0; i < count; i++) {
            if (books[i].getTitle() == title) {
                int quantity = books[i].getQuantity() - 1;
                if (quantity < 0) {
                    cout << "库存不足,无法出售!" << endl;
                    return;
                }
                books[i].setQuantity(quantity);
                // 将销售信息写入文件中
                ofstream out("sales.txt", ios::app);
                time_t now = time(0);
                char* dt = ctime(&now);
                out << dt << " " << books[i].getTitle() << "," << books[i].getAuthor() << "," << books[i].getPublisher() << "," << books[i].getPrice() << endl;
                cout << "出售成功!" << endl;
                return;
            }
        }
        cout << "未找到该书籍!" << endl;
    }

    void queryBook() {
        // 查询书籍信息
        int option;
        string keyword;
        cout << "\n请选择查询方式:" << endl;
        cout << "1. 书名" << endl;
        cout << "2. 作者" << endl;
        cout << "3. 出版社" << endl;
        cout << "4. 价格" << endl;
        cin >> option;
        if (option <= 0 || option > 4) {
            cout << "无效的选择!" << endl;
            return;
        }
        cout << "请输入关键词:";
        cin >> keyword;
        cout << "查询结果:" << endl;
        for (int i = 0; i < count; i++) {
            Book b = books[i];
            bool match = false;
            switch (option) {
            case 1:
                if (b.getTitle() == keyword) 
                    match = true;
                break;
            case 2:
                if (b.getAuthor() == keyword) 
                    match = true;
                break;
            case 3:
                if (b.getPublisher() == keyword) 
                    match = true;
                break;
            case 4:
                if (b.getPrice() == atof(keyword.c_str()))
                    match = true;
                break;
            }

            if (match) {
                cout << "书名:" << b.getTitle() << endl;
                cout << "作者:" << b.getAuthor() << endl;
                cout << "出版社:" << b.getPublisher() << endl;
                cout << "页数:" << b.getPages() << endl;
                cout << "单价:" << b.getPrice() << endl;
                cout << "入库时间:" << b.getTime() << endl;
                cout << "库存量:" << b.getQuantity() << endl;
                cout << endl;
            }
        }
    }

    void checkStock() {
        // 自动预警提示
        bool warning = false;
        for (int i = 0; i < count; i++) {
            if (books[i].getQuantity() == 1) {
                cout << "警告:书籍\"" << books[i].getTitle() << "\"的库存仅剩1本!" << endl;
                warning = true;
            }
        }
        if (!warning) {
            cout << "当前无需预警。" << endl;
        }
    }
};

int main() {
    BookStore bs;
    int option;
    while (1) {
        cout << "\n请选择以下操作:" << endl;
        cout << "1. 进货" << endl;
        cout << "2. 销售" << endl;
        cout << "3. 查询" << endl;
        cout << "4. 自动预警提示" << endl;
        cout << "5. 退出" << endl;
        cin >> option;
        switch (option) {
        case 1:
            bs.addBook();
            break;
        case 2:
            bs.sellBook();
            break;
        case 3:
            bs.queryBook();
            break;
        case 4:
            bs.checkStock();
            break;
        case 5:
            cout << "谢谢使用,再见!" << endl;
            return 0;
        default:
            cout << "无效的选择!" << endl;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linzaii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值