withdraw and deposit money into account CPP Program
Problem Statement :Write a class for Account. Let it store account data (private) and public functions to withdraw and deposit money into that account.Overview
This program implements a robust bank account management system that allows users to perform common banking operations like deposits, withdrawals, and balance inquiries. The implementation includes error handling, input validation, and proper object-oriented programming practices.
Features
- Account creation with customizable account numbers
- Secure deposit and withdrawal operations
- Balance inquiry with transaction history
- Input validation and error handling
- Transaction limits and overdraft protection
- Proper memory management and encapsulation
Code:
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <limits>
class Transaction {
private:
std::string type;
double amount;
std::string timestamp;
public:
Transaction(const std::string& t, double amt)
: type(t), amount(amt) {
// In a real system, we would get actual timestamp
timestamp = "2024-01-05";
}
std::string getType() const { return type; }
double getAmount() const { return amount; }
std::string getTimestamp() const { return timestamp; }
};
class Account {
private:
int accountNumber;
double balance;
std::vector<Transaction> transactionHistory;
const double WITHDRAWAL_LIMIT = 10000.0;
const double MIN_BALANCE = 0.0;
bool isValidAmount(double amount) const {
return amount > 0 && amount <= std::numeric_limits<double>::max();
}
void recordTransaction(const std::string& type, double amount) {
transactionHistory.emplace_back(type, amount);
}
public:
Account(int accNum) : accountNumber(accNum), balance(0.0) {}
bool deposit(double amount) {
if (!isValidAmount(amount)) {
std::cout << "\nError: Invalid deposit amount." << std::endl;
return false;
}
balance += amount;
recordTransaction("DEPOSIT", amount);
std::cout << "\nSuccessfully deposited $" << std::fixed
<< std::setprecision(2) << amount << std::endl;
return true;
}
bool withdraw(double amount) {
if (!isValidAmount(amount)) {
std::cout << "\nError: Invalid withdrawal amount." << std::endl;
return false;
}
if (amount > WITHDRAWAL_LIMIT) {
std::cout << "\nError: Amount exceeds withdrawal limit of $"
<< WITHDRAWAL_LIMIT << std::endl;
return false;
}
if (balance - amount < MIN_BALANCE) {
std::cout << "\nError: Insufficient funds." << std::endl;
return false;
}
balance -= amount;
recordTransaction("WITHDRAWAL", amount);
std::cout << "\nSuccessfully withdrew $" << std::fixed
<< std::setprecision(2) << amount << std::endl;
return true;
}
void displayBalance() const {
std::cout << "\nAccount Number: " << accountNumber << std::endl;
std::cout << "Current Balance: $" << std::fixed
<< std::setprecision(2) << balance << std::endl;
}
void displayTransactionHistory() const {
std::cout << "\nTransaction History:" << std::endl;
std::cout << std::setw(12) << "Type" << std::setw(15) << "Amount"
<< std::setw(20) << "Date" << std::endl;
std::cout << std::string(47, '-') << std::endl;
for (const auto& transaction : transactionHistory) {
std::cout << std::setw(12) << transaction.getType()
<< std::setw(15) << std::fixed << std::setprecision(2)
<< transaction.getAmount()
<< std::setw(20) << transaction.getTimestamp() << std::endl;
}
}
int getAccountNumber() const { return accountNumber; }
};
class BankSystem {
private:
std::vector<Account> accounts;
Account* findAccount(int accountNumber) {
for (auto& account : accounts) {
if (account.getAccountNumber() == accountNumber) {
return &account;
}
}
return nullptr;
}
public:
void createAccount(int accountNumber) {
if (findAccount(accountNumber) != nullptr) {
std::cout << "\nError: Account number already exists." << std::endl;
return;
}
accounts.emplace_back(accountNumber);
std::cout << "\nAccount created successfully!" << std::endl;
}
void performTransaction() {
int accountNumber;
std::cout << "\nEnter account number: ";
std::cin >> accountNumber;
Account* account = findAccount(accountNumber);
if (account == nullptr) {
std::cout << "\nError: Account not found." << std::endl;
return;
}
char choice;
std::cout << "\nSelect operation:"
<< "\n1. Deposit"
<< "\n2. Withdraw"
<< "\n3. Check Balance"
<< "\n4. View Transaction History"
<< "\nEnter choice (1-4): ";
std::cin >> choice;
double amount;
switch (choice) {
case '1':
std::cout << "Enter deposit amount: $";
std::cin >> amount;
account->deposit(amount);
break;
case '2':
std::cout << "Enter withdrawal amount: $";
std::cin >> amount;
account->withdraw(amount);
break;
case '3':
account->displayBalance();
break;
case '4':
account->displayTransactionHistory();
break;
default:
std::cout << "\nInvalid choice." << std::endl;
}
}
};
int main() {
BankSystem bank;
char choice;
do {
std::cout << "\nBank Account Management System"
<< "\n1. Create New Account"
<< "\n2. Perform Transaction"
<< "\n3. Exit"
<< "\nEnter choice (1-3): ";
std::cin >> choice;
switch (choice) {
case '1': {
int accountNumber;
std::cout << "Enter new account number: ";
std::cin >> accountNumber;
bank.createAccount(accountNumber);
break;
}
case '2':
bank.performTransaction();
break;
case '3':
std::cout << "\nThank you for using our banking system!" << std::endl;
break;
default:
std::cout << "\nInvalid choice. Please try again." << std::endl;
}
} while (choice != '3');
return 0;
}
Usage
- Compile the program using a C++ compiler (C++11 or later)
- Run the executable
- Follow the on-screen menu to:
- Create new accounts
- Make deposits
- Make withdrawals
- Check balances
- View transaction history
Future Improvements
- Add user authentication
- Implement file persistence for account data
- Add interest calculation
- Support different account types
- Add multiple currency support
- Implement transaction categories
- Add date-range filtering for transaction history
#COFPROG
Sign up here with your email
1 comments:
Write comments.
ReplyConversionConversion EmoticonEmoticon