//problem with registering new account number for the next person
//would be better to make a system that displays all accounts for verification
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
using namespace std;
struct Account {
string accountNumber;
string accountHolderName;
string password;
double balance;
string accountType;
double interestRate;//Annual interest rate in percentage
time_t lastInterestApplied;//Timestamp of when interest was last applied
};
// Function prototypes
void createAccount(vector<Account> &accounts);
/*std::vector is a container from the C++ Standard Library (defined in the <vector> header).
It represents a dynamic array, meaning it can grow or shrink in size as needed during runtime.
Unlike a traditional array, which has a fixed size, a vector can change its size
by adding or removing elements dynamically. It handles memory management internally.*/
/*<Account> - In your code, Account is a structure (or class) that represents a bank account,
containing fields like accountNumber, balance, accountType, etc.*/
/* & accounts - you are passing the vector by reference. This means that the function
will work with the original vector, not a copy of it. Any changes made to accounts
within the function will directly affect the original vector outside the function.
Without & (passing by value): The function works with a copy of the vector,
so changes inside the function don't affect the original vector.*/
void getCustomerList(const vector<Account> &accounts);
int login(const vector<Account> &accounts);
/*The const keyword ensures that the function does not modify the accounts vector.
It guarantees that no changes will be made to the vector inside the function,
providing a read-only view of the vector.*/
void checkBalance(const Account &account);
void depositMoney(Account &account);
void withdrawMoney(Account &account);
void transferMoney(vector<Account> &accounts, Account &sender);
/*The function does not return anything (void), it simply performs the transfer of money,
adjusting the balances of the sender and possibly the recipient accounts.*/
void applyInterest(Account &account);
int main() {
vector<Account> accounts;
/*vector<Account> means a vector (dynamic array) where each element is of type Account.
accounts is the name of the variable that holds the vector.*/
int choice;
do{
cout<< "\n - - - Bank System Menu - - - \n\n";
cout<<"1. Create Account\n2. Get Customer List\n3. Login\n4. Exit\n\nEnter your choice: \n";
cin>>choice;
switch(choice) {
case 1:{
createAccount(accounts);
break;
}
case 2:{
getCustomerList(accounts);
break;
}
case 3:{
int index=login(accounts);
if (index != -1){
Account ¤tAccount = accounts[index];
int subChoice;
do{
cout<<"\n - - - Account Menu - - - \n";
cout<<"1. Check Balance\n2. Deposit Money\n3. Withdraw Money\n4. Transfer Money\
n5. Apply Interest\n6. Logout\n\nEnter your choice: \n";
cin>>subChoice;
switch (subChoice){
case 1:
checkBalance(currentAccount);
break;
case 2:
depositMoney(currentAccount);
break;
case 3:
withdrawMoney(currentAccount);
break;
case 4:
transferMoney(accounts, currentAccount);
break;
case 5:
applyInterest(currentAccount);
break;
case 6:
cout<<"Logging out . . .\n";
break;
default:
cout<<"Invalid choice. Try again."<<endl;
} while (subChoice !=6);
}
else{
cout<<"Invalid account number or password."<<endl;
}
break;
}
case 4:
cout<< "Exiting the program. Thank you!\n";
break;
default:
cout<<"Invalid choice. Try again."<<endl;
} while (choice !=3);
return 0;
}
long int accountCounter;
void createAccount(vector<Account> &accounts){
Account newAccount;
newAccount.accountNumber = to_string(++accountCounter);
cin.ignore();
/*when you use cin >> to read something (like a number or a word),
the Enter key you press leaves a newline character (\n) in the input buffer.
If you don't clear this leftover newline, it can interfere with subsequent inputs,
like when you use getline() to read a full line of text.
cin.ignore() helps to clear that newline (or other unwanted characters),
allowing the next input operation to work as expected.*/
cout<<"Please Enter Your Name (It is recommended if you enter till your grandfather's name)\n";
getline(cin, newAccount.accountHolderName);
cout<<"Enter Account Type (Savings/Checking): ";
cin>>newAccount.accountType;
if (newAccount.accountType == "Savings"){
newAccount.interestRate = 6.0;
}
else{
newAccount.interestRate = 0.0;
}
cout<<"Enter Initial Balance: ";
cin>> newAccount.balance;
//Password input (hidden)
string password;
cout<<"Enter your Password: ";
cin>>password;
newAccount.password = password; //Store the password securely
time(&newAccount.lastInterestApplied); //Set current time as the last interest application
accounts.push_back(newAccount);//It adds newAccount to the end of the container called
accounts.
//The push_back function appends the element, expanding the container if necessary to
accomodate it.
cout<<"\n\nYour New Account Number is: "<<newAccount.accountNumber<<endl;
cout<<"\nAccount created successfully!\n";
while (true){
cout << "\nYou have created a new account successfully.\n";
cout << "1. Return to Home Page\n2. Exit\nEnter your choice: ";
int choice;
cin >> choice;
if (choice == 1) {
return; // Return to the home page (caller function handles this)
} else if (choice == 2) {
exit(0); // Exit the program
} else {
cout << "Invalid choice. Please try again.\n";
}
}
}
void getCustomerList(const vector<Account> &accounts) {
if (accounts.empty()) {
cout << "\nNo customers registered yet.\n\n";
return;
}
cout << "\n**********************************\n\n";
cout << "*** Customer List ***\n\n";
cout << "**********************************\n\n";
for (int i = 0; i < accounts.size(); ++i) {
cout << "\nCustomer " << i + 1 << ":";
cout << "\n----------------------------";
cout << "\nAccount Number: " << accounts[i].accountNumber;
cout << "\nFull Name: " << accounts[i].accountHolderName;
cout << "\nBalance: ETB " << accounts[i].balance;
cout << "\n----------------------------\n";
}
}
int login(const vector<Account> &accounts){
string accNo, pass;
cout<<"\nEnter Account Number: ";
cin>> accNo;
cout<< "Enter Password";
cin>>pass;
for(int i=0; i<accounts.size(); i++){
if(accounts[i].accountNumber==accNo && accounts[i].password==pass){
cout<<"Login successful!\n";
return i;//It allows the prograwm to identify and work with the specific account the user logged
into.
}
}
return -1; //indicates failure or invalid input.
}
void checkBalance(const Account &account){
cout<<"Your Current balance is: "<<account.balance<<" Birr"<<endl;
}
void depositMoney(Account &account){
double amount;
cout<< "Enter amount you want to deposit: ";
cin>> amount;
if (amount>0){
account.balance +=amount;
cout<<"The Deposit is successful. Your New Balance is: "<<account.balance<<" Birr"<<endl;
}
else{
cout<<"Invalid deposit amount."<<endl;
}
}
void withdrawMoney(Account &account){
double amount;
cout<<"Enter amount to withdraw: ";
cin>> amount;
if (amount>0 && amount<=account.balance){
account.balance -=amount;
cout<< "The Withdrawal is successful. Your new balance is: "<<account.balance<<" Birr"<<endl;
}
else if (amount<=0){
cout<<"Invalid withdrawal amount";
}
else{
cout<<"You have insufficient balance to carry out the operation."<<endl;
}
}
void transferMoney(vector<Account> &accounts, Account &sender){
string recAccNo;
double amount;
cout<<"Enter recipient account number: ";
cin>>recAccNo;
cout<<"Enter amount to transfer";
cin>>amount;
for(int i=0; i<accounts.size(); i++){
if(accounts[i].accountNumber==recAccNo){
if(amount>0 && amount<= sender.balance){
sender.balance -= amount;
accounts[i].balance += amount;
cout<< "The transfer is successful. Your new balance is: "<<sender.balance<<" Birr"<<endl;
}
else if (amount<=0){
cout<<"Invalid transfer amount";
}
else{
cout<<"You have insufficient balance to carry out the operation."<<endl;
}
return;// which is used to terminate the function and return control to the calling code.
}
}
cout<<"The recipient account is not found."<<endl;
}
void applyInterest(Account &account){
time_t currentTime;
time(¤tTime);
double secondsElapsed = difftime(currentTime, account.lastInterestApplied);
double monthsElapsed = secondsElapsed / (30.4375 * 24 * 60 * 60);
/*Over a 4-year period (including one leap year), the total number of days is:
3×365 + 1×366=1461days and Dividing this by 4 years gives: 362.25
and further dividing this by 12 months gives: 30.4375*/
if (monthsElapsed>=1){
for(int i=0; i<monthsElapsed; i++){
double monthlyInterestRate = account.interestRate / (12 * 100);
account.balance += account.balance * monthlyInterestRate;
}
account.lastInterestApplied = currentTime;
cout<<"The Interest is applied successfully! Your New balance is: "<<account.balance<<"
Birr"<<endl;
}
else {
cout<<"Not enough time has passed to apply interest."<<endl;
}
}