逆向软件设计和开发————购书系统

来源

为了完成逆向软件设计和开发,我要来了舍友大一时的c++程序大作业,代码是关于实现图书馆购物系统的功能

运行环境及运行结果截图

环境:Visual Studio 2022

点击查看代码
#include <iostream>
#include <vector>
#include <string>

using namespace std;

class User {
public:
    User(string name, string password) : name(name), password(password) {}
    string getName() { return name; }
    string getPassword() { return password; }
private:
    string name;
    string password;
};

class Member : public User {
public:
    Member(string name, string password, int level) : User(name, password), level(level) {}
    int getLevel() { return level; }
private:
    int level;
};

class BookInfo {
public:
    BookInfo(string title, double price) : title(title), price(price) {}
    string getTitle() { return title; }
    double getPrice() { return price; }
private:
    string title;
    double price;
};

class PaymentSystem {
public:
    bool pay(double amount) {
        return true; // 假设支付成功
    }
};

class Order {
public:
    Order(User* user, vector<BookInfo> books) : user(user), books(books) {}
    double calculateTotalPrice() {
        double total = 0;
        for (auto& book : books) {
            total += book.getPrice();
        }
        return total;
    }
private:
    User* user;
    vector<BookInfo> books;
};

class DiscountCalculator {
public:
    static double calculateDiscountedPrice(Order order, int level) {
        double totalPrice = order.calculateTotalPrice();
        double discount = 1 - 0.1 * level;
        return totalPrice * discount;
    }
};

int main() {
    vector<BookInfo> books = { BookInfo("C++编程   ", 90.0), BookInfo("Python编程", 80.0) };
    vector<Member> members = { Member("张三", "123456", 1), Member("李四", "123456", 2), Member("王五", "123456", 3) };
    int choice;
    cout << "欢迎来到图书商城!" << endl;
    while (true) {
        cout << "---------------------------------------------------------------------------" << endl;
        cout << "请选择操作:" << endl;
        cout << "1. 查看书籍信息" << endl;
        cout << "2. 登录" << endl;
        cout << "3. 购书" << endl;
        cout << "4. 退出登录" << endl;
        cout << "5. 退出" << endl;
        cout << "---------------------------------------------------------------------------" << endl;
        cin >> choice;
        User* currentUser = nullptr;
        switch (choice) {
        case 1: {
            cout << "书籍信息如下:" << endl;
            for (int i = 0; i < books.size(); i++) {
                cout << "编号:" << i + 1 << "     书名:" << books[i].getTitle() << "        价格:" << books[i].getPrice() << "元" << endl;
            }
            break;
        }
        case 2: {
            string name, password;
            cout << "请输入用户名:";
            cin >> name;
            cout << "请输入密码:";
            cin >> password;
            bool isLoggedIn = false;
            currentUser = new User(name, password);
            for (auto& member : members) {
                if (member.getName() == name && member.getPassword() == password) {
                    isLoggedIn = true;
                    break;
                }
            }
            if (isLoggedIn) {
                cout << "登录成功,欢迎 " << name << endl;
            }
            else {
                cout << "登录失败,请检查用户名和密码是否正确" << endl;
            }
            break;
        }
        case 3: {
            int bookIndex;
            cout << "请输入购买的书籍编号:";
            cin >> bookIndex;
            if (bookIndex < 1 || bookIndex > books.size()) {
                cout << "无效的书籍编号" << endl;
                break;
            }
            vector<BookInfo> selectedBooks = { books[bookIndex - 1] };
            Order order(currentUser, selectedBooks); // 使用当前用户信息创建订单
            double totalPrice = DiscountCalculator::calculateDiscountedPrice(order, members[0].getLevel());
            double originalPrice = books[bookIndex - 1].getPrice();
            double discountedPrice = totalPrice / selectedBooks.size();
            cout << "购买的书籍信息:" << endl;
            cout << "书名:" << books[bookIndex - 1].getTitle() << "       原价:" << originalPrice << "元     折扣价:" << discountedPrice << "元" << endl;
            cout << "总价:" << totalPrice << "元" << endl;
            PaymentSystem paymentSystem;
            bool isPaid = paymentSystem.pay(totalPrice);
            if (isPaid) {
                cout << "支付成功" << endl;
            }
            else {
                cout << "支付失败" << endl;
            }
            break;
        }
        case 4: {
            delete currentUser; // 清理当前用户信息
            currentUser = nullptr;
            cout << "已退出登录" << endl;
            break;
        }
        case 5: {
            cout << "感谢使用图书商城,再见!" << endl;
            return 0;
        }
        default:
            cout << "无效的操作" << endl;
            break;
        }
    }
    return 0;
}
运行结果:

主要问题

源代码登录逻辑错误,不进行登录也能继续购书,于是我更改代码添加登录验证,在购书时检查登录状态;并根据登录用户是否有会员进行DiscountCalculator重构,根据订单中的用户信息计算折扣,处理Member和非Member用户。

新代码:

点击查看代码
// 修改1:登录逻辑(case 2)
case 2: {
    // ... 输入验证...
    currentUser = nullptr;
    for (auto& member : members) {  // 直接使用存储的Member对象
        if (member.getName() == name && member.getPassword() == password) {
            currentUser = &member;  // 指向已存在的对象
            break;
        }
    }
    // ... 登录结果处理...
}

// 修改2:折扣计算逻辑
class DiscountCalculator {
public:
    static double calculateDiscountedPrice(const Order& order) {
        double totalPrice = order.calculateTotalPrice();
        User* user = order.getUser();
        if (auto member = dynamic_cast<Member*>(user)) {  // 运行时类型检查
            int level = member->getLevel();  // 获取实际用户等级
            double discount = 1 - 0.1 * level;
            return totalPrice * discount;
        }
        return totalPrice;  // 非会员无折扣
    }
};

// 修改3:购书前登录检查(case 3)
case 3: {
    if (!currentUser) {  // 新增登录检查
        cout << "请先登录!" << endl;
        break;
    }
    // ...后续逻辑...
}

修改前后的结果截图
修改前:

修改后:

总结
我认为本次改进的难点主要是分析理解懂其他人的代码并找出来不足,如:找出原代码未正确区分 User 和 Member 的继承关系,折扣计算硬编码 members[0].getLevel(),未动态识别用户类型,导致用户登录时会员类有时会错误,还有登陆时的验证问题:原代码未验证登录状态直接允许购书。
关于逆向软件工程我的思考是在进行一段新的代码时需要深入的理解,以便找出发现问题才能顺利进行下去,能增加对代码的理解与使用。

posted @ 2025-02-27 18:27  Simonyoi  阅读(37)  评论(0)    收藏  举报