Добавил:
Рад, если кому-то помог Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
1
Добавлен:
01.11.2025
Размер:
3.55 Кб
Скачать
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include <windows.h>
using namespace std;

class Person {
private:
    string firstName;
    string lastName;
    
public:
    Person(string first = "", string last = "") : firstName(first), lastName(last) {}
    
    string getFullName() { return firstName + " " + lastName; }
};

class Operation {
private:
    string date;
    string type;
    double amount;
    
public:
    Operation(string d, string t, double a) : date(d), type(t), amount(a) {}
    
    void print() {
        cout << "  " << date << ": " << type << " " << amount << " руб." << endl;
    }
};

class BankAccount {
private:
    string creationDate;
    double balance;
    Person owner;
    vector<Operation> operations;
    
    void addOperation(const string& type, double amount) {
        time_t now = time(0);
        string date = ctime(&now);
        date.pop_back(); // удаляем символ новой строки
        operations.push_back(Operation(date, type, amount));
        
        if (operations.size() > 10) {
            operations.erase(operations.begin());
        }
    }
    
public:
    BankAccount(string date, double initialBalance, Person own) 
        : creationDate(date), balance(initialBalance), owner(own) {
        cout << "Создан банковский счет для " << owner.getFullName() << endl;
    }
    
    void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            addOperation("ПОПОЛНЕНИЕ", amount);
            cout << "Счет пополнен на " << amount << " руб." << endl;
        } else {
            cout << "Ошибка: недопустимая сумма для пополнения" << endl;
        }
    }
    
    bool withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
            addOperation("СНЯТИЕ", amount);
            cout << "Со счета снято " << amount << " руб." << endl;
            return true;
        }
        cout << "Ошибка: недопустимая сумма для снятия" << endl;
        return false;
    }
    
    void printOperations() {
        cout << "=== ПОСЛЕДНИЕ ОПЕРАЦИИ ===" << endl;
        if (operations.empty()) {
            cout << "Операций нет" << endl;
        } else {
            for (auto& op : operations) {
                op.print();
            }
        }
        cout << "==========================" << endl;
    }
    
    void printBalance() {
        cout << "Текущий баланс: " << balance << " руб." << endl;
    }
    
    void printAccountInfo() {
        cout << "=== ИНФОРМАЦИЯ О СЧЕТЕ ===" << endl;
        cout << "Владелец: " << owner.getFullName() << endl;
        cout << "Дата создания: " << creationDate << endl;
        cout << "Баланс: " << balance << " руб." << endl;
        cout << "==========================" << endl;
    }
};

int main() {
    SetConsoleOutputCP(65001);
    
    Person person("Иван", "Иванов");
    BankAccount account("01.01.2024", 1000, person);
    
    account.printAccountInfo();
    
    account.deposit(500);
    account.withdraw(200);
    account.withdraw(2000); // ошибка
    
    account.printBalance();
    account.printOperations();
    
    return 0;
}
Соседние файлы в папке Лаба2