1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
#include <iostream> #include <vector> #include <iomanip> class Transaction { public: enum class Type { DEPOSIT, WITHDRAWAL }; Transaction(Type type, double amount) : type(type), amount(amount) {} Type getType() const { return type; } double getAmount() const { return amount; } private: Type type; double amount; }; class FinancialManager { public: FinancialManager(double initialBalance) : balance(initialBalance) {} void deposit(double amount) { balance += amount; transactions.emplace_back(Transaction::Type::DEPOSIT, amount); } bool withdraw(double amount) { if (amount > balance) { std::cerr << "Insufficient funds.\n"; return false; } balance -= amount; transactions.emplace_back(Transaction::Type::WITHDRAWAL, amount); return true; } void printStatement() const { std::cout << "Balance: $" << std::fixed << std::setprecision(2) << balance << "\n"; std::cout << "Transactions:\n"; for (const auto& transaction : transactions) { std::cout << (transaction.getType() == Transaction::Type::DEPOSIT ? "Deposit" : "Withdrawal") << ": $" << std::fixed << std::setprecision(2) << transaction.getAmount() << "\n"; } } private: double balance; std::vector<Transaction> transactions; }; int main() { double initialBalance; std::cout << "Enter initial balance: $"; std::cin >> initialBalance; FinancialManager manager(initialBalance); while (true) { int choice; std::cout << "\n1. Deposit\n2. Withdraw\n3. Print Statement\n4. Exit\n"; std::cout << "Enter your choice: "; std::cin >> choice; if (choice == 1) { double amount; std::cout << "Enter amount to deposit: $"; std::cin >> amount; manager.deposit(amount); } else if (choice == 2) { double amount; std::cout << "Enter amount to withdraw: $"; std::cin >> amount; manager.withdraw(amount); } else if (choice == 3) { manager.printStatement(); } else if (choice == 4) { break; } else { std::cout << "Invalid choice. Please try again.\n"; } } return 0; } |
Explanation
- Class Definition:
Transaction
Class:- Represents a financial transaction with type (
DEPOSIT
orWITHDRAWAL
) and amount. - Constructor: Initializes the type and amount of the transaction.
- Getters: Retrieve the type and amount of the transaction.
- Represents a financial transaction with type (
FinancialManager
Class:- Manages account balance and records transactions.
- Private Members:
balance
: Current balance of the account.transactions
: Vector storing all transactions.
- Public Methods:
deposit(double amount)
: Increases the balance and records a deposit transaction.withdraw(double amount)
: Decreases the balance if sufficient funds are available and records a withdrawal transaction. Returnsfalse
if there are insufficient funds.printStatement()
: Prints the current balance and a list of all transactions.
Advertisement
main
Function:- Prompts the user for the initial balance and creates a
FinancialManager
object. - Provides a menu to:
- Deposit money.
- Withdraw money.
- Print the account statement.
- Exit the program.
- Handles user input and performs the corresponding actions.
- Prompts the user for the initial balance and creates a