Sunday, June 30, 2024

Bank Account in C++

 




Program Listing


#include <iostream>

#include <iomanip>


class BankAccount {

private:

    double balance;


public:

    BankAccount() : balance(0.0) {}


    void deposit(double amount) {

        if (amount > 0) {

            balance += amount;

            std::cout << "\tDeposited PHP" <<std::fixed <<std::setprecision(2) << amount << ". New balance: PHP" << balance << std::endl;

        } else {

            std::cout << "\tInvalid deposit amount." << std::endl;

        }

    }


    void withdraw(double amount) {

        if (amount > 0 && amount <= balance) {

            balance -= amount;

            std::cout << "\tWithdrawn PHP" <<std::fixed <<std::setprecision(2)<< amount << ". New balance: PHP" << balance << std::endl;

        } else {

            std::cout << "\tInvalid withdrawal amount or insufficient funds." << std::endl;

        }

    }


    double getBalance() const {

        return balance;

    }

};


int main() {

    BankAccount account;

    int choice=0;

    double amount=0.00;


    while (true) {

        std::cout << "\n\tBank Account Main Menu" << std::endl;

        std::cout << "\t[1] Deposit Money" << std::endl;

        std::cout << "\t[2] Withdraw Money" << std::endl;

        std::cout << "\t[3] Check Balance" << std::endl;

        std::cout << "\t[4] Quit Program" << std::endl;

        std::cout << "\n\tEnter your choice: ";

        std::cin >> choice;


        switch (choice) {

            case 1:

                std::cout << "\tEnter the deposit amount: PHP";

                std::cin >> amount;

                account.deposit(amount);

                break;

            case 2:

                std::cout << "\tEnter the withdrawal amount: PHP";

                std::cin >> amount;

                account.withdraw(amount);

                break;

            case 3:

                std::cout << "\tCurrent balance: PHP" <<std::fixed <<std::setprecision(2)<< account.getBalance() << std::endl;

                break;

            case 4:

                std::cout << "\tExiting. Thank you for using this program." << std::endl;

                return 0;

            default:

                std::cout << "\tInvalid choice. Please try again." << std::endl;

        }

    }


    return 0;

}


No comments:

Post a Comment