diff --git a/main.cpp b/main.cpp index a4d477a..b267569 100644 --- a/main.cpp +++ b/main.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include #ifdef _WIN32 #include #include @@ -16,7 +18,8 @@ using namespace std; -void clear_screen() { +void clear_screen() +{ #ifdef __linux__ cout << "\033[2J\033[1;1H"; #endif @@ -25,6 +28,38 @@ void clear_screen() { #endif } +void pause_screen() +{ +#ifdef __linux__ + system("read -p 'Press Enter to continue...' var"); +#endif +#ifdef _WIN32 + system("pause"); +#endif +} + +void loadingAnimation(int durationSeconds) +{ + int progressBarWidth = 40; + + std::cout << "Loading: "; + for (int i = 0; i <= progressBarWidth; ++i) + { + std::cout << "\r["; + for (int j = 0; j < progressBarWidth; ++j) + { + if (j <= i) + std::cout << "="; + else + std::cout << " "; + } + std::cout << "] " << static_cast((static_cast(i) / progressBarWidth) * 100) << "%"; + std::cout.flush(); + std::this_thread::sleep_for(std::chrono::milliseconds(durationSeconds * 1000 / progressBarWidth)); + } + std::cout << std::endl; +} + class account { private: @@ -48,11 +83,7 @@ class account cout << "-----------------------------------" << endl; cout << "\nAccount has Already been created" << endl; cout << "\n---------------------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); } else @@ -96,6 +127,11 @@ class account #else srand(time(NULL)); AccountNumber = rand() % random + 1; + if (AccountNumber < 1000000) + { + srand(time(NULL)); + AccountNumber = rand() % random + 1; + } #endif @@ -135,11 +171,7 @@ class account cout << "-------------------------------------------------" << endl; cout << "\n\nPlease Continue to View your Account Details" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); clear_screen(); cout << "---------------------------------------\n"; @@ -147,11 +179,7 @@ class account cout << "\nName :- " << name << endl; cout << "\nBalance :- " << balance << endl; cout << "\n---------------------------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); } } @@ -172,11 +200,7 @@ class account cout << "---------------------------------------------------------" << endl; cout << "No Account has been created yet First Create one account" << endl; cout << "---------------------------------------------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); } else if (name == "null") { @@ -217,11 +241,7 @@ class account file.close(); cout << "---------------------------------------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); } else { @@ -232,11 +252,7 @@ class account cout << "------------------------------------" << endl; cout << "Pin does not match please try again" << endl; cout << "------------------------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); } } else if (!found) @@ -245,11 +261,7 @@ class account cout << "\nAccount does not exist under this Acoount Number" << endl; file.close(); cout << "---------------------------------------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); } } else if (name != "null") @@ -258,11 +270,7 @@ class account cout << "-----------------------------" << endl; cout << "Already An Account is Opened" << endl; cout << "-----------------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); } } @@ -390,11 +398,7 @@ class account cout << "-------------------------" << endl; cout << "Your Answer is Incorrect" << endl; cout << "-------------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); } } else if (!found) @@ -403,11 +407,7 @@ class account cout << "\nAccount does not exist under this Account Number" << endl; file.close(); cout << "\n--------------------------------------------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); } } else if (name != "null") @@ -416,11 +416,7 @@ class account cout << "-----------------------------" << endl; cout << "Already An Account is Opened" << endl; cout << "-----------------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); } } @@ -540,11 +536,7 @@ class account cout << "\n ***** PIN Changed Succesfully *****" << endl; cout << "\n-------------------------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); extpin = ""; } else @@ -554,11 +546,7 @@ class account cout << "------------------------------------" << endl; cout << "Pin does not match please try again" << endl; cout << "------------------------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); } } else @@ -567,11 +555,7 @@ class account cout << "----------------------------------------" << endl; cout << "No Account has been Logged in yet \nFirst login with an account to change pin" << endl; cout << "----------------------------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); } } @@ -630,6 +614,7 @@ class account } inputFile.close(); + tempFile.close(); if (remove("data.txt") != 0) { @@ -673,11 +658,7 @@ class account cout << "---------------------------------------------------------" << endl; cout << "No Account has been created yet First Create one account" << endl; cout << "---------------------------------------------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); } else @@ -817,11 +798,7 @@ class account cout << "-----------------------------------" << endl; cout << "Please Login with an account first" << endl; cout << "-----------------------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); } else { @@ -834,11 +811,7 @@ class account cout << "\nBalance :- " << balance << endl; cout << "\n---------------------------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); } } @@ -884,20 +857,10 @@ class account inputFile.close(); exportFile.close(); - for (int c = 0; c <= 100; c++) - { - clear_screen(); - cout << "--------------------------------\n"; - cout << "Exporting Data File " << c << "%"; - cout << "\nPlease Wait..."; - cout << "\n--------------------------------\n"; + clear_screen(); -#ifdef _WIN32 - Sleep(25); -#else - usleep(10000); -#endif - } + cout << "Exporting Data...\nPlease Wait..."; + loadingAnimation(3); clear_screen(); @@ -905,11 +868,213 @@ class account cout << "\nData Exported Successfully" << endl; cout << "\n-----------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); + } + + void TransferMoney() + { + int transferAccountNumber = 0; + int transferBalance = 0; + int transferAmount = 0; + + if (AccountNumber == 1 || AccountNumber == 0) + { + clear_screen(); + cout << "------------------------------------" << endl; + cout << "Please Login with an account first" << endl; + cout << "------------------------------------" << endl; + + pause_screen(); + } + + else + { + clear_screen(); + cout << "Enter the account number of the person you want to transfer money to: "; + cin >> transferAccountNumber; + + if (transferAccountNumber == AccountNumber) + { + clear_screen(); + cout << "You cannot transfer money to yourself" << endl; + pause_screen(); + return; + } + + if (transferAccountNumber < 1000000) + { + clear_screen(); + cout << "Invalid account number" << endl; + pause_screen(); + return; + } + + ifstream inputFile("data.txt"); + if (!inputFile) + { + cout << "Data File not Present" << endl; + return; + } + string line; + string transferName = ""; + bool found = false; + + string str_transferAccountNumber = to_string(transferAccountNumber); + inputFile.seekg(0, ios::beg); + while (getline(inputFile, line)) + { + string AccountNumber = line.substr(0, line.find("^")); + if (AccountNumber == str_transferAccountNumber) + { + transferName = line.substr(line.find("^") + 1, line.find("%") - line.find("^") - 1); + transferBalance = stoi(line.substr(line.find("*") + 1, line.find("$") - line.find("*") - 1)); + found = true; + break; + } + } + + cout << "Finding Account Number... \nPlease Wait." << endl; + loadingAnimation(2); + + if (!found) + { + clear_screen(); + cout << "Account not found" << endl; + pause_screen(); + return; + } + if (found) + { + clear_screen(); + cout << "----------------------------------------------------" << endl; + cout << "Account Found" << endl; + + cout << "\nAccount Holder Name: " << transferName << endl; + cout << "Account Number: " << transferAccountNumber << endl; + + cout << "\n----------------------------------------------------" << endl; + cout << "\nEnter the amount you want to transfer = "; + cin >> transferAmount; + + if (transferAmount > balance) + { + clear_screen(); + cout << "----------------------------------------------------" << endl; + cout << "You do not have enough balance to transfer" << endl; + cout << "----------------------------------------------------" << endl; + + pause_screen(); + return; + } + else + { + balance = balance - transferAmount; + transferBalance = transferBalance + transferAmount; + + ifstream inputFile("data.txt"); + if (!inputFile) + { + cout << "Error: Unable to open data.txt" << endl; + return; + } + + ofstream tempFile("temp.txt"); + if (!tempFile) + { + cout << "Error: Unable to open temp.txt" << endl; + inputFile.close(); + return; + } + + string line; + string str_transferAccountNumber = to_string(transferAccountNumber); + inputFile.seekg(0, ios::beg); + while (getline(inputFile, line)) + { + string file_AccountNumber = line.substr(0, line.find("^")); + if (file_AccountNumber == str_transferAccountNumber) + { + tempFile << str_transferAccountNumber << "^" << transferName << "%" << line.substr(line.find("%") + 1, line.find("*") - line.find("%") - 1) << "*" << transferBalance << "$" << line.substr(line.find("$") + 1) << endl; + } + + else + { + tempFile << line << endl; + } + } + + inputFile.close(); + tempFile.close(); + + if (remove("data.txt") != 0) + { + cout << "Error: Unable to delete data.txt" << endl; + return; + } + if (rename("temp.txt", "data.txt") != 0) + { + cout << "Error: Unable to rename temp.txt" << endl; + return; + } + + ifstream inputfile("data.txt"); + if (!inputfile) + { + cout << "Error: Unable to open data.txt" << endl; + return; + } + + ofstream tempfile("temp.txt"); + if (!tempfile) + { + cout << "Error: Unable to open temp.txt" << endl; + inputfile.close(); + return; + } + + inputfile.seekg(0, ios::beg); + while (getline(inputfile, line)) + { + string file_AccountNumber = line.substr(0, line.find("^")); + + if (file_AccountNumber == to_string(AccountNumber)) + { + tempfile << to_string(AccountNumber) << "^" << name << "%" << line.substr(line.find("%") + 1, line.find("*") - line.find("%") - 1) << "*" << balance << "$" << line.substr(line.find("$") + 1) << endl; + } + + else + { + tempfile << line << endl; + } + } + + inputfile.close(); + tempfile.close(); + + if (remove("data.txt") != 0) + { + cout << "Error: Unable to delete data.txt" << endl; + return; + } + if (rename("temp.txt", "data.txt") != 0) + { + cout << "Error: Unable to rename temp.txt" << endl; + return; + } + + cout << "Transferring Money... \nPlease Wait." << endl; + loadingAnimation(2); + + clear_screen(); + cout << "\n----------------------------------------------------" << endl; + cout << "Transfer Successful" << endl; + cout << "Your new balance is: " << balance << endl; + cout << "----------------------------------------------------" << endl; + pause_screen(); + return; + } + } + } } }; @@ -974,7 +1139,8 @@ class operationmode : public account cout << "5. Change Pin of your Savings Account" << endl; cout << "6. Add Money to Savings Account" << endl; cout << "7. Withdraw Money form Savings Account" << endl; - cout << "8. Logout" << endl; + cout << "8. Transfer Money to another Account" << endl; + cout << "9. Logout" << endl; cout << "---------------------------------------------------" << endl; cout << "\n>> "; @@ -1017,10 +1183,15 @@ class operationmode : public account } if (u == 8) + { + obj.TransferMoney(); + } + + if (u == 9) { obj.Logout(); } - } while (u != 8); + } while (u != 9); } }; @@ -1073,27 +1244,19 @@ int main() cout << "-----------------------" << endl; cout << "Incorrect Password" << endl; cout << "-----------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); } } if (u == 3) { -// BYE BYE MESSAGE + // BYE BYE MESSAGE clear_screen(); cout << "----------------------------------" << endl; cout << "Thank you for trying this program" << endl; cout << "----------------------------------" << endl; -#ifdef _WIN32 - system("pause"); -#else - system("read -p 'Press Enter to continue...' var"); -#endif + pause_screen(); } } while (u != 3);