网上购书结账系统的逆向软件工程

项目来源于室友上学期的C++项目,项目名为网上购书结账系统。
2.
运行环境vscode
以下是运行结果的截图






以下是伸缩代码

点击查看代码
#include <iostream>
#include <vector>
#include <string>
using namespace std;

// 基类 buyer
class buyer {
protected:
    string name;
    int id;
    string address;
    double pay;

public:
    buyer(const string& name, int id, const string& address, double pay)
            : name(name), id(id), address(address), pay(pay) {}

    virtual ~buyer() {}

    virtual void display() const = 0;
    virtual void setpay(double amount) = 0;

    int getid() const { return id; }
    const string& getbuyname() const { return name; }
    const string& getaddress() const { return address; }
    double getpay() const { return pay; }
};

// 派生类 layfolk
class layfolk : public buyer {
public:
    layfolk(const string& name, int id, const string& address, double pay)
            : buyer(name, id, address, pay) {}

    void display() const override {
        cout << "普通购书者: " << name << ", 编号: " << id << ", 地址: " << address << ", 需支付: " << pay << "\n";
    }

    void setpay(double amount) override {
        pay = amount;
    }
};

// 派生类 honoured_guest
class honoured_guest : public buyer {
private:
    double rate;

public:
    honoured_guest(const string& name, int id, double rate, const string& address, double pay)
            : buyer(name, id, address, pay), rate(rate) {}

    void display() const override {
        cout << "贵宾购书者: " << name << ", 编号: " << id << ", 地址: " << address << ", 折扣率: " << rate << ", 需支付: " << pay << "\n";
    }

    void setpay(double amount) override {
        pay = amount * rate;
    }
};

// 派生类 member
class member : public buyer {
private:
    int grade;

public:
    member(const string& name, int id, int grade, const string& address, double pay)
            : buyer(name, id, address, pay), grade(grade) {}

    void display() const override {
        cout << "会员购书者: " << name << ", 编号: " << id << ", 地址: " << address << ", 会员级别: " << grade << ", 需支付: " << pay << "\n";
    }

    void setpay(double amount) override {
        double discount = (grade == 1) ? 0.9 : ((grade == 2) ? 0.8 : 0.7);
        pay = amount * discount;
    }
};

// 图书类 book
class book {
private:
    string book_ID;
    string book_name;
    string author;
    string publishing;
    double price;

public:
    book(const string& book_ID, const string& book_name, const string& author, const string& publishing, double price)
            : book_ID(book_ID), book_name(book_name), author(author), publishing(publishing), price(price) {}

    void display() const {
        cout << "书号: " << book_ID << ", 书名: " << book_name << ", 作者: " << author << ", 出版社: " << publishing << ", 定价: " << price << "\n";
    }

    const string& getbook_ID() const { return book_ID; }
    const string& getbook_name() const { return book_name; }
    const string& getauthor() const { return author; }
    const string& getpublishing() const { return publishing; }
    double getprice() const { return price; }
};

// 系统类 System
class System {
private:
    vector<buyer*> buyers;
    vector<book*> books;

public:
    System() {
        // 初始化一些购书者和图书
        buyers.push_back(new layfolk("Alice", 1, "123 Main St", 0.0));
        buyers.push_back(new honoured_guest("Bob", 2, 0.8, "456 Elm St", 0.0));
        buyers.push_back(new member("Charlie", 3, 2, "789 Oak St", 0.0));

        books.push_back(new book("001", "Book One", "Author A", "Publisher X", 100.0));
        books.push_back(new book("002", "Book Two", "Author B", "Publisher Y", 150.0));
        books.push_back(new book("003", "Book Three", "Author C", "Publisher Z", 200.0));
    }

    ~System() {
        for (auto& b : buyers) {
            delete b;
        }
        for (auto& b : books) {
            delete b;
        }
    }

    void displayMenu() const {
        cout << "欢迎来到购书结账系统!\n";
        cout << "请选择操作:\n";
        cout << "1. 查看购书人信息\n";
        cout << "2. 查看图书信息\n";
        cout << "3. 查询购书金额\n";
        cout << "4. 购买图书\n";
        cout << "5. 退出\n";
        cout << "请输入选择:";
    }

    void displayBuyers() const {
        cout << "购书人信息:\n\n";
        for (auto& b : buyers) {
            b->display();
        }
    }

    void displayBooks() const {
        cout << "\n图书信息:\n";
        for (auto& b : books) {
            b->display();
        }
    }

    void queryPay() {
        int buyerid;
        cout << "\n\n请输入购书人编号:";
        cin >> buyerid;
        bool found = false;

        for (auto& b : buyers) {
            if (b->getid() == buyerid) {
                found = true;
                double totalPay = 0.0; // 这里可以根据需要计算总金额
                cout << endl << "购书人需要付费:" << totalPay << "\n\n";
                break;
            }
        }

        if (!found) {
            cout << "未找到购书人编号:" << buyerid << "\n";
        }
    }

    void purchaseBook() {
        int buyerid;
        cout << "请输入购书人编号:";
        cin >> buyerid;
        bool buyerFound = false;
        buyer* currentBuyer = nullptr;

        for (auto& b : buyers) {
            if (b->getid() == buyerid) {
                buyerFound = true;
                currentBuyer = b;
                break;
            }
        }

        if (!buyerFound) {
            cout << "未找到购书人编号:" << buyerid << "\n";
            return;
        }

        string bookID;
        cout << "请输入要购买的书号:";
        cin >> bookID;
        bool bookFound = false;
        double bookPrice = 0.0;

        for (auto& b : books) {
            if (b->getbook_ID() == bookID) {
                bookFound = true;
                bookPrice = b->getprice();
                break;
            }
        }

        if (!bookFound) {
            cout << "未找到书号:" << bookID << "\n";
            return;
        }

        // 计算应支付金额
        currentBuyer->setpay(bookPrice);
        cout << "购书人 " << currentBuyer->getbuyname() << " 购买书籍 " << bookID << ",需支付金额:" << currentBuyer->getpay() << "\n";
    }

    void run() {
        int choice;

        while (true) {
            displayMenu();
            cin >> choice;

            switch (choice) {
                case 1:
                    displayBuyers();
                    break;

                case 2:
                    displayBooks();
                    break;

                case 3:
                    queryPay();
                    break;

                case 4:
                    purchaseBook();
                    break;

                case 5:
                    cout << "程序退出。\n";
                    return;

                default:
                    cout << "无效的选择,请重新输入。\n";
                    break;
            }
        }
    }
};

int main() {
    System system;
    system.run();
    return 0;
}
3. 主要问题列表 (1)在用户购买书籍时,无法支持多本书的购买,灵活性不够,用户需要重复输入书籍信息进行多次购买,而不是一次性处理所有书籍。 (2)用户购买体验不好,用户无法在购买书籍前确认他们的选择,可能会导致误购买或不满意的情况。 (3)代码难以拓展功能,扩展功能可能需要在多个地方进行修改。 (4)代码功能分散,缺乏统一的管理 针对问题(1)(2)添加了购物车功能 针对问题(3)(4)添加了一个系统类System 4. 以下是更改后的伸缩代码
点击查看代码
#include <iostream>
#include <vector>
#include <string>
using namespace std;

// 基类 buyer
class buyer {
protected:
    string name;
    int id;
    string address;
    double pay;

public:
    buyer(const string& name, int id, const string& address, double pay)
            : name(name), id(id), address(address), pay(pay) {}

    virtual ~buyer() {}

    virtual void display() const = 0;
    virtual void setpay(double amount) = 0;

    int getid() const { return id; }
    const string& getbuyname() const { return name; }
    const string& getaddress() const { return address; }
    double getpay() const { return pay; }
};

// 派生类 layfolk
class layfolk : public buyer {
public:
    layfolk(const string& name, int id, const string& address, double pay)
            : buyer(name, id, address, pay) {}

    void display() const override {
        cout << "普通购书者: " << name << ", 编号: " << id << ", 地址: " << address << ", 需支付: " << pay << "\n";
    }

    void setpay(double amount) override {
        pay = amount;
    }
};

// 派生类 honoured_guest
class honoured_guest : public buyer {
private:
    double rate;

public:
    honoured_guest(const string& name, int id, double rate, const string& address, double pay)
            : buyer(name, id, address, pay), rate(rate) {}

    void display() const override {
        cout << "贵宾购书者: " << name << ", 编号: " << id << ", 地址: " << address << ", 折扣率: " << rate << ", 需支付: " << pay << "\n";
    }

    void setpay(double amount) override {
        pay = amount * rate;
    }
};

// 派生类 member
class member : public buyer {
private:
    int grade;

public:
    member(const string& name, int id, int grade, const string& address, double pay)
            : buyer(name, id, address, pay), grade(grade) {}

    void display() const override {
        cout << "会员购书者: " << name << ", 编号: " << id << ", 地址: " << address << ", 会员级别: " << grade << ", 需支付: " << pay << "\n";
    }

    void setpay(double amount) override {
        double discount = (grade == 1) ? 0.9 : ((grade == 2) ? 0.8 : 0.7);
        pay = amount * discount;
    }
};

// 图书类 book
class book {
private:
    string book_ID;
    string book_name;
    string author;
    string publishing;
    double price;

public:
    book(const string& book_ID, const string& book_name, const string& author, const string& publishing, double price)
            : book_ID(book_ID), book_name(book_name), author(author), publishing(publishing), price(price) {}

    void display() const {
        cout << "书号: " << book_ID << ", 书名: " << book_name << ", 作者: " << author << ", 出版社: " << publishing << ", 定价: " << price << "\n";
    }

    const string& getbook_ID() const { return book_ID; }
    const string& getbook_name() const { return book_name; }
    const string& getauthor() const { return author; }
    const string& getpublishing() const { return publishing; }
    double getprice() const { return price; }
};

// 购物车类 ShoppingCart
class ShoppingCart {
private:
    vector<book*> cart;
public:
    void addBook(book* b) {
        cart.push_back(b);
    }

    void removeBook(const string& bookID) {
        for (auto it = cart.begin(); it != cart.end(); ++it) {
            if ((*it)->getbook_ID() == bookID) {
                cart.erase(it);
                cout << "图书已从购物车中删除。\n";
                return;
            }
        }
        cout << "未找到书号:" << bookID << "\n";
    }

    double calculateTotal() const {
        double total = 0.0;
        for (auto& b : cart) {
            total += b->getprice();
        }
        return total;
    }

    void displayCart() const {
        cout << "购物车中的图书:\n";
        for (auto& b : cart) {
            b->display();
        }
    }
};

// 系统类 System
class System {
private:
    vector<buyer*> buyers;
    vector<book*> books;
    ShoppingCart cart;

public:
    System() {
        // 初始化一些购书者和图书
        buyers.push_back(new layfolk("Alice", 1, "123 Main St", 0.0));
        buyers.push_back(new honoured_guest("Bob", 2, 0.8, "456 Elm St", 0.0));
        buyers.push_back(new member("Charlie", 3, 2, "789 Oak St", 0.0));

        books.push_back(new book("001", "Book One", "Author A", "Publisher X", 100.0));
        books.push_back(new book("002", "Book Two", "Author B", "Publisher Y", 150.0));
        books.push_back(new book("003", "Book Three", "Author C", "Publisher Z", 200.0));
    }

    ~System() {
        for (auto& b : buyers) {
            delete b;
        }
        for (auto& b : books) {
            delete b;
        }
    }

    void displayMenu() const {
        cout << "欢迎来到购书结账系统!\n";
        cout << "请选择操作:\n";
        cout << "1. 查看购书人信息\n";
        cout << "2. 查看图书信息\n";
        cout << "3. 查询购书金额\n";
        cout << "4. 添加图书到购物车\n";
        cout << "5. 查看购物车\n";
        cout << "6. 计算购物车金额\n";
        cout << "7. 退出\n";
        cout << "请输入选择:";
    }

    void displayBuyers() const {
        cout << "购书人信息:\n\n";
        for (auto& b : buyers) {
            b->display();
        }
    }

    void displayBooks() const {
        cout << "\n图书信息:\n";
        for (auto& b : books) {
            b->display();
        }
    }

    void queryPay() {
        int buyerid;
        cout << "\n\n请输入购书人编号:";
        cin >> buyerid;
        bool found = false;

        for (auto& b : buyers) {
            if (b->getid() == buyerid) {
                found = true;
                double totalPay = cart.calculateTotal();
                b->setpay(totalPay);
                cout << endl << "购书人需要付费:" << b->getpay() << "\n\n";
                break;
            }
        }

        if (!found) {
            cout << "未找到购书人编号:" << buyerid << "\n";
        }
    }

    void addBookToCart() {
        string bookID;
        cout << "请输入要添加到购物车的书号:";
        cin >> bookID;
        bool found = false;

        for (auto& b : books) {
            if (b->getbook_ID() == bookID) {
                found = true;
                cart.addBook(b);
                cout << "图书已添加到购物车。\n";
                break;
            }
        }

        if (!found) {
            cout << "未找到书号:" << bookID << "\n";
        }
    }

    void displayCart() const {
        cart.displayCart();
    }

    void calculateCartAmount() {
        int memberGrade;
        cout << "请输入会员级别(0 - 普通购书者, 1 - 一级会员, 2 - 二级会员, 3 - 三级会员, 4 - 贵宾购书者):";
        cin >> memberGrade;

        double totalAmount = cart.calculateTotal();
        double finalAmount = totalAmount;

        switch (memberGrade) {
            case 0:
                // 普通购书者,无折扣
                break;
            case 1:
                finalAmount = totalAmount * 0.9;
                break;
            case 2:
                finalAmount = totalAmount * 0.8;
                break;
            case 3:
                finalAmount = totalAmount * 0.7;
                break;
            case 4:
                finalAmount = totalAmount * 0.8; // 假设贵宾购书者折扣率为0.8
                break;
            default:
                cout << "无效的会员级别,按原价计算。\n";
                break;
        }

        cout << "购物车总金额:" << totalAmount << ",折扣后金额:" << finalAmount << "\n";
    }

    void run() {
        int choice;

        while (true) {
            displayMenu();
            cin >> choice;

            switch (choice) {
                case 1:
                    displayBuyers();
                    break;

                case 2:
                    displayBooks();
                    break;

                case 3:
                    queryPay();
                    break;

                case 4:
                    addBookToCart();
                    break;

                case 5:
                    displayCart();
                    break;

                case 6:
                    calculateCartAmount();
                    break;

                case 7:
                    cout << "程序退出。\n";
                    return;

                default:
                    cout << "无效的选择,请重新输入。\n";
                    break;
            }
        }
    }
};

int main() {
    System system;
    system.run();
    return 0;
}

5. 以下是测试截图








总结:
难点在于实现系统类System时,要将购书人、图书和购物车的管理逻辑有效地模块化,为了确保各个功能之间的良好协作。需要设计清晰的接口和方法。当然通过合理的设计和实现,可以有效克服这些难点,提升系统的整体性能和用户满意度。逆向软件工程是对既有软件系统的深度剖析。它能让我们从代码和系统表象深入挖掘隐藏的设计逻辑与架构信息。在实际应用中,有助于理解遗留系统,发现潜在问题以优化,也能为复用代码、移植功能提供支撑,像是给软件研究与开发提供了一把“反向之匙”,打开未知的知识宝库。

posted @ 2025-02-27 21:17  抹抹抹抹抹茶*  阅读(18)  评论(0)    收藏  举报