Skip to content

Instantly share code, notes, and snippets.

@DreamVB
Created July 18, 2021 20:47
Show Gist options
  • Save DreamVB/74b37f1d86c6c0077ece20ff8405bd18 to your computer and use it in GitHub Desktop.
Save DreamVB/74b37f1d86c6c0077ece20ff8405bd18 to your computer and use it in GitHub Desktop.
Example Of a Bank Account
// A simple bank account program made in Visual C++ 2013
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Bank{
private:
struct account_info
{
int acc_no;
std::string acc_holder;
std::string acc_type;
double acc_balance;
};
std::vector<account_info>accounts;
const int find_account(int id){
size_t x = 0;
int idx = (-1);
while (x < accounts.size()){
if (accounts[x].acc_no == id){
idx = x;
break;
}
x++;
}
return idx;
}
void acc_not_found(int n){
std::cout << "Sorry no information was found for that account number." << std::endl;
}
void acc_invaild_no(void){
std::cout << "The account number needs to be a positive value." << std::endl;
}
void acc_amount_positive(void){
std::cout << "The amount entered needs to be a positive value." << std::endl;
}
bool is_positive(double value){
if (value < 0)
return false;
return true;
}
int get_account(){
int account_num = 0;
std::cout << "Enter account number : ";
std::cin >> account_num;
return account_num;
}
std::string get_account_type(int account_id){
char ch = accounts[account_id].acc_type[0];
ch = toupper(ch);
switch (ch)
{
case 'B':
return "Basic account";
break;
case 'C':
return "Current account";
break;
case 'S':
return "Student account";
break;
default:
break;
}
return "Unknown";
}
public:
void clear_accounts(){
accounts.clear();
}
void display_accounts(){
size_t ids = 0;
int itr;
for (ids = 0; ids < accounts.size(); ids++){
std::cout << std::endl;
std::cout << " +---------------Account Details-----------------+" << std::endl;
std::cout << " Account Number : " << accounts[ids].acc_no << std::endl;
std::cout << " Account holder : " << accounts[ids].acc_holder << std::endl;
std::cout << " Account type : " << this->get_account_type(ids) << std::endl;
std::cout << " Account balance : " << accounts[ids].acc_balance << std::endl;
std::cout << " +--------------Bens Banking System--------------+" << std::endl;
std::cout << std::endl;
}
if (ids == 0){
std::cout << "Sorry there were no accounts found on this system." << std::endl;
}
}
void close_account(void){
int account_num = 0;
vector<account_info>temp;
account_info ai;
account_num = get_account();
if (!is_positive(account_num)){
acc_invaild_no();
}
else{
int ids = find_account(account_num);
if (ids == -1){
acc_not_found(account_num);
}
else{
for (size_t x = 0; x < accounts.size();x++){
ai = accounts[x];
if (ai.acc_no != account_num){
temp.push_back(ai);
}
}
accounts = temp;
temp.clear();
std::cout << "The account was successfully closed." << std::endl;
}
}
}
void new_account(void){
account_info ai;
int acc_tmp = 0;
ai.acc_no = get_account();
if (ai.acc_no < 0){
acc_invaild_no();
}
else if (find_account(ai.acc_no) != -1){
std::cout << "The account is already created." << std::endl <<
"Please try a different account number." << std::endl;
}
else{
cin.ignore();
std::cout << "Enter the account's holder name : ";
getline(cin, ai.acc_holder);
std::cout << std::endl;
std::cout << "+----- Account types -----+" << std::endl;
std::cout << "+ [B] Basic account +" << std::endl;
std::cout << "+ [C] Current account +" << std::endl;
std::cout << "+ [S] Student account +" << std::endl;
std::cout << "+-------------------------+" << std::endl;
std::cout << "Enter account type B, C or S : ";
std::cin >> ai.acc_type;
std::cout << "Enter the account of money to you wish to deposit to the new account : ";
std::cin >> ai.acc_balance;
if (!is_positive(ai.acc_balance)){
acc_amount_positive();
}
else{
accounts.push_back(ai);
std::cout << "The new account was successfully created." << std::endl;
}
}
}
void account_details(){
int account_num = 0;
account_num = get_account();
if (!is_positive(account_num)){
acc_invaild_no();
}
else{
int ids = find_account(account_num);
if (ids == -1){
acc_not_found(account_num);
}
else{
system("cls");
std::cout << std::endl;
std::cout << " +---------------Account Details-----------------+" << std::endl;
std::cout << " Account holder : " << accounts[ids].acc_holder << std::endl;
std::cout << " Account type : " << this->get_account_type(ids) << std::endl;
std::cout << " Account balance : " << accounts[ids].acc_balance << std::endl;
std::cout << " +--------------Bens Banking System--------------+" << std::endl;
std::cout << std::endl;
}
}
}
void Deposit(){
int account_num = 0;
double amount = 0;
account_num = get_account();
if (!is_positive(account_num)){
acc_invaild_no();
}
else{
int ids = find_account(account_num);
if (ids == -1){
acc_not_found(account_num);
}
else{
std::cout << "Enter the account of money to you wish to deposit to this account : ";
std::cin >> amount;
if (!is_positive(amount)){
acc_amount_positive();
}
else{
std::cout << amount << " has been deposited to your account." << std::endl;
accounts[ids].acc_balance += amount;
std::cout << "Your new balance is : " << accounts[ids].acc_balance << std::endl;
}
}
}
}
void withdraw(){
int account_num = 0;
double wd = 0;
account_num = get_account();
if (!is_positive(account_num)){
acc_invaild_no();
}
else{
int ids = find_account(account_num);
if (ids == -1){
acc_not_found(account_num);
}
else{
std::cout << "Enter amount to withdraw : ";
std::cin >> wd;
if (!is_positive(wd)){
acc_amount_positive();
}
else if ((accounts[ids].acc_balance - wd) < 0){
std::cout << "Sorry you do not have enough money to withdraw." << std::endl;
}
else{
accounts[ids].acc_balance -= wd;
std::cout << wd << " has been withdrawn from your account." << std::endl;
std::cout << "Your remaining balance is : " << accounts[ids].acc_balance << std::endl;
}
}
}
}
};
void show_menu(void){
system("cls");
std::cout << std::endl;
std::cout << " +---- Bens Bank System ---+" << std::endl;
std::cout << " + [1] New Account +" << std::endl;
std::cout << " + [2] Account details +" << std::endl;
std::cout << " + [3] Deposit +" << std::endl;
std::cout << " + [4] Withdraw +" << std::endl;
std::cout << " + [5] Show All Accounts +" << std::endl;
std::cout << " + [6] Close Account +" << std::endl;
std::cout << " + [7] Exit +" << std::endl;
std::cout << " +-------------------------+" << std::endl;
std::cout << " Enter Option : ";
}
int main(){
int ch = 0;
Bank bank;
do{
show_menu();
std::cin >> ch;
if (!ch >= 1 && ch <= 7){
break;
}
switch (ch)
{
case 1:
system("cls");
bank.new_account();
system("pause");
break;
case 2:
system("cls");
bank.account_details();
system("pause");
break;
case 3:
system("cls");
bank.Deposit();
system("pause");
break;
case 4:
system("cls");
bank.withdraw();
system("pause");
break;
case 5:
system("cls");
bank.display_accounts();
system("pause");
break;
case 6:
system("cls");
bank.close_account();
system("pause");
break;
default:
break;
}
cin.clear();
cin.ignore();
} while (ch != 7);
bank.clear_accounts();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment