Пример #1
0
const std::string
StatementPrinter::generateStatementLine(const Transaction& transaction)
{
        std::stringstream ss;
        ss << transaction.date()
           << " | "
           << transaction.amount() << ".00"
           << " | "
           << this->runningBalance << ".00";

          return ss.str();
}
void processTransactions(const std::string& name, std::ifstream& in) {
    Transaction t;
    while(in >> t) {
        if(t.operation() == Operation::Deposit) {
            // Deposit routine.
            if(t.account() == AccountType::Checking) {
                std::lock_guard l(checkingLock);
                checking += t.amount();
                std::cout << name << " deposited $" << t.amount() << ". New "
                          << t.account() << " balance: $" << checking << std::endl;
            } else if(t.account() == AccountType::Savings) {
                std::lock_guard l(savingsLock);
                savings += t.amount();
                std::cout << name << " deposited $" << t.amount() << ". New "
                          << t.account() << " balance: $" << savings << std::endl;
            }
        } else if(t.operation() == Operation::Withdrawal) {
            // Withdraw routine.
            // Lock whichever account is being accessed.
            if(t.account() == AccountType::Checking) {
                std::lock_guard l(checkingLock);
                if(t.amount() > checking) {
                    // Lock both accounts if transfering.
                    std::lock_guard transferL(transferLock);
                    std::lock_guard savingsL(savingsLock);
                    if(savings > 0) {
                        savings += checking;
                        checking = 0;
                        savings -= t.amount();
                        std::cout << name << " withdrew $" << t.amount() << "." << std::endl;
                        printBalances();
                    } else {
                        std::cout << name << " cannot withdraw $" << t.amount()
                                  << ". Insufficient funds." << std::endl;
                    }
                } else {
                    checking -= t.amount();
                    std::cout << name << " withdrew $" << t.amount() << ". New "
                              << t.account() << " balance: $" << checking << std::endl;
                }
            } else if(t.account() == AccountType::Savings) {
                std::lock_guard l(savingsLock);
                if(savings > 0) {
                    savings -= t.amount();
                    std::cout << name << " withdrew $" << t.amount() << ". New"
                              << t.account() << " balance: $" << savings << std::endl;
                } else {
                    std::cout << name << " cannot withdraw $" << t.amount()
                              << ". Insufficient funds." << std::endl;
                }
            }
        } else if(t.operation() == Operation::Transfer) {
            // Transfer routine.
            // Lock both accounts when transfering.
            std::lock_guard tLock(transferLock);
            std::lock_guard cLock(checkingLock);
            std::lock_guard sLock(savingsLock);
            if(t.account() == AccountType::Checking) {
                if(savings > 0) {
                    checking += t.amount();
                    savings -= t.amount();
                    std::cout << name << " transferred $" << t.amount()
                              << " from savings to checking." << std::endl;
                    printBalances();
                } else {
                    std::cout << name << " cannot transfer $" << t.amount()
                              << ". Insufficient funds." << std::endl;
                }
            } else if(t.account() == AccountType::Savings) {
                if(checking > 0) {
                    checking -= t.amount();
                    savings += t.amount();
                    std::cout << name << " transferred $" << t.amount()
                              << " from checking to savings." << std::endl;
                    printBalances();
                } else {
                    std::cout << name << " cannot transfer $" << t.amount()
                              << ". Insufficient funds." << std::endl;
                }
            }
        }
    }
    return;
}