Untitled

 avatar
unknown
c_cpp
a month ago
6.2 kB
2
Indexable
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Employee {
protected:
    int id;
    string name;
    string birthDate;
    string address;
    float salaryRate;
    string startDate;

public:
    Employee(int id, string name, string birthDate, string address, float salaryRate, string startDate)
        : id(id), name(name), birthDate(birthDate), address(address), salaryRate(salaryRate), startDate(startDate) {}

    virtual float getMonthlySalary() = 0; // Hàm tính lương (đa hình)
    virtual void displayInfo() {
        cout << "ID: " << id << ", Name: " << name << ", Salary: " << getMonthlySalary() << endl;
    }

    virtual ~Employee() {}
};

class Manager : public Employee {
private:
    float baseSalary;

public:
    Manager(int id, string name, string birthDate, string address, float salaryRate, string startDate, float baseSalary)
        : Employee(id, name, birthDate, address, salaryRate, startDate), baseSalary(baseSalary) {}

    float getMonthlySalary() override {
        return baseSalary * salaryRate;
    }
};

class Programmer : public Employee {
private:
    float baseSalary;
    float overtime;

public:
    Programmer(int id, string name, string birthDate, string address, float salaryRate, string startDate, float baseSalary, float overtime)
        : Employee(id, name, birthDate, address, salaryRate, startDate), baseSalary(baseSalary), overtime(overtime) {}

    float getMonthlySalary() override {
        return baseSalary * salaryRate + overtime;
    }
};

class Designer : public Employee {
private:
    float baseSalary;
    float bonus;

public:
    Designer(int id, string name, string birthDate, string address, float salaryRate, string startDate, float baseSalary, float bonus)
        : Employee(id, name, birthDate, address, salaryRate, startDate), baseSalary(baseSalary), bonus(bonus) {}

    float getMonthlySalary() override {
        return baseSalary * salaryRate + bonus;
    }
};

class Tester : public Employee {
private:
    float baseSalary;
    int errorCount;

public:
    Tester(int id, string name, string birthDate, string address, float salaryRate, string startDate, float baseSalary, int errorCount)
        : Employee(id, name, birthDate, address, salaryRate, startDate), baseSalary(baseSalary), errorCount(errorCount) {}

    float getMonthlySalary() override {
        return baseSalary * salaryRate + errorCount * 200000;
    }
};

// Menu
void showMenu() {
    cout << "=== MENU ===\n";
    cout << "1. Nhập danh sách nhân viên\n";
    cout << "2. Tính tổng lương công ty\n";
    cout << "3. Sắp xếp danh sách nhân viên theo lương\n";
    cout << "4. Xuất danh sách nhân viên có lương > 10 triệu\n";
    cout << "5. Thoát\n";
}

int main() {
    vector<Employee*> employees;
    int choice;

    do {
        showMenu();
        cout << "Nhập lựa chọn: ";
        cin >> choice;

        switch (choice) {
        case 1: {
            int n;
            cout << "Nhập số nhân viên: ";
            cin >> n;
            for (int i = 0; i < n; ++i) {
                int type;
                cout << "Nhập loại nhân viên (1-Manager, 2-Programmer, 3-Designer, 4-Tester): ";
                cin >> type;

                int id;
                string name, birthDate, address, startDate;
                float salaryRate, baseSalary, overtime = 0, bonus = 0;
                int errorCount = 0;

                cout << "Nhập ID: "; cin >> id;
                cout << "Nhập tên: "; cin.ignore(); getline(cin, name);
                cout << "Nhập ngày sinh: "; getline(cin, birthDate);
                cout << "Nhập địa chỉ: "; getline(cin, address);
                cout << "Nhập hệ số lương: "; cin >> salaryRate;
                cout << "Nhập ngày bắt đầu: "; cin.ignore(); getline(cin, startDate);
                cout << "Nhập lương cơ bản: "; cin >> baseSalary;

                if (type == 1) {
                    employees.push_back(new Manager(id, name, birthDate, address, salaryRate, startDate, baseSalary));
                } else if (type == 2) {
                    cout << "Nhập tiền overtime: "; cin >> overtime;
                    employees.push_back(new Programmer(id, name, birthDate, address, salaryRate, startDate, baseSalary, overtime));
                } else if (type == 3) {
                    cout << "Nhập tiền bonus: "; cin >> bonus;
                    employees.push_back(new Designer(id, name, birthDate, address, salaryRate, startDate, baseSalary, bonus));
                } else if (type == 4) {
                    cout << "Nhập số lỗi tìm được: "; cin >> errorCount;
                    employees.push_back(new Tester(id, name, birthDate, address, salaryRate, startDate, baseSalary, errorCount));
                }
            }
            break;
        }
        case 2: {
            float totalSalary = 0;
            for (auto emp : employees) {
                totalSalary += emp->getMonthlySalary();
            }
            cout << "Tổng lương công ty phải trả: " << totalSalary << endl;
            break;
        }
        case 3: {
            sort(employees.begin(), employees.end(), [](Employee* a, Employee* b) {
                return a->getMonthlySalary() < b->getMonthlySalary();
            });
            cout << "Danh sách nhân viên sau khi sắp xếp:\n";
            for (auto emp : employees) {
                emp->displayInfo();
            }
            break;
        }
        case 4: {
            cout << "Danh sách nhân viên có lương > 10 triệu:\n";
            for (auto emp : employees) {
                if (emp->getMonthlySalary() > 10000000) {
                    emp->displayInfo();
                }
            }
            break;
        }
        case 5:
            cout << "Thoát chương trình.\n";
            break;
        default:
            cout << "Lựa chọn không hợp lệ.\n";
        }
    } while (choice != 5);

    // Giải phóng bộ nhớ
    for (auto emp : employees) {
        delete emp;
    }

    return 0;
}
Leave a Comment