#include <iostream> #include <vector> #include<fstream> #include <iomanip> #include <string> using namespace std; struct sClient { string accountNumber; string PinCode; string Name; string phone; double AccountBalance; bool MarkForDelete = false; }; enum enATMMainMenu { eQuickWithdraw = 1, eNormalWithdraw = 2, eDesposit = 3, eCheckBalance = 4, eLogOut = 5 }; enum enQuickWithdrawMenu { eTwenty = 1, eFifty = 2,eOnehundred = 3, eTwoHundred = 4, eFourhundred = 5, eSixHundred = 6 , eEighthundred = 7, eOneThousand = 8 ,eEixt = 9 }; const string ClientsFileName = "Clients.txt"; sClient CurrentClient; void Login(); void showATMMainMenuScreen(); void ShowQuickWithdraw(); vector<string> SplitString(string S1, string delimiter) { vector<string>Vstring; short pos = 0; string Token; while ((pos = S1.find(delimiter)) != std::string::npos) { Token = S1.substr(0, pos); if (Token != "") { Vstring.push_back(Token); S1.erase(0, pos + delimiter.length()); } } if (S1 != "") { Vstring.push_back(S1); } return Vstring; } sClient ConvertLineToRecordClient(string Line, string separator = "#//#") { sClient Client; vector<string>VClientData; VClientData = SplitString(Line, separator); Client.accountNumber = VClientData[0]; Client.PinCode = VClientData[1]; Client.Name = VClientData[2]; Client.phone = VClientData[3]; Client.AccountBalance = stod(VClientData[4]); return Client; } vector<sClient> LoadDataFormClientFileToVector(string FileName) { vector<sClient>VClients; fstream Myfile; Myfile.open(FileName, ios::in);//read mode if (Myfile.is_open()) { string Line; sClient Client; while (getline(Myfile, Line)) { Client = ConvertLineToRecordClient(Line); VClients.push_back(Client); } Myfile.close(); } return VClients; } bool FindClientByAccountNumberAndPincode(string AccountNumber,sClient& client ,string Pincode ) { vector<sClient>vClients = LoadDataFormClientFileToVector(ClientsFileName); for (sClient C : vClients) { if (C.accountNumber == AccountNumber && C.PinCode == Pincode) { client = C; return true; } } return false; } bool LoadAccountNumberInfo(string AccountNumber , string Pincode) { if (FindClientByAccountNumberAndPincode(AccountNumber,CurrentClient ,Pincode)) return true; else return false; } void BackToMainMenu() { cout << "\nPress any key to back to Main Menu........\n"; system("pause>0"); showATMMainMenuScreen(); } void LogOutScreen() { system("cls"); Login(); } short ReadATMMenuOption() { short ChosenNumber = 0; do { cout << "choose What do you want to do? [1 to 5]?"; cin >> ChosenNumber; } while (ChosenNumber < 1 || ChosenNumber > 5); return ChosenNumber; } short ReadQuickWithdrawMenuOption() { short ChosenNumber = 0; do { cout << "\nchoose What do you want to do? [1 to 9]?"; cin >> ChosenNumber; } while (ChosenNumber < 1 || ChosenNumber > 9); return ChosenNumber; } string ConvertRecordToOneLine(sClient Client, string Separator = "#//#") { string ClientRecord = ""; ClientRecord += Client.accountNumber + Separator; ClientRecord += Client.Name + Separator; ClientRecord += Client.PinCode + Separator; ClientRecord += Client.phone + Separator; ClientRecord += to_string(Client.AccountBalance); return ClientRecord; } vector<sClient> SaveVectorToFile(string FileName, vector<sClient>VClients) { fstream Myfile; Myfile.open(FileName, ios::out);//overwrite. string DataLine; if (Myfile.is_open()) { for (sClient& C : VClients) { if (C.MarkForDelete == false) { //we only write records that are not marked for delete. DataLine = ConvertRecordToOneLine(C); Myfile << DataLine << endl; } } Myfile.close(); } return VClients; } void BackToQuickWithdrawMenu() { cout << "\nPress any key to continue ........\n"; system("pause>0"); ShowQuickWithdraw(); } bool QuickWithdrawFromClientAccountBalance(short Amount , vector<sClient>&vClient) { char Answer = 'n'; if (CurrentClient.AccountBalance < Amount) { cout << "The amount exceeded your balance,make another choose" << endl; BackToQuickWithdrawMenu(); return false; } cout << "\nAre you sure you want to perform this transaction ?"; cin >> Answer; if(Answer == 'Y' || Answer == 'y') { for (sClient& A : vClient) { if (A.accountNumber == CurrentClient.accountNumber) { CurrentClient.AccountBalance -= Amount; SaveVectorToFile(ClientsFileName, vClient); //Refresh Clients vClient = LoadDataFormClientFileToVector(ClientsFileName); cout << "\n\nDone Successfully. New balance is "; cout << CurrentClient.AccountBalance; return true; } } } else { return false; } } void CalculateQickwithdraw(short QuickWithdrawAmount ) { vector<sClient>vClient = LoadDataFormClientFileToVector(ClientsFileName); QuickWithdrawFromClientAccountBalance(QuickWithdrawAmount , vClient); } void PerformanceQuickWithdrawMenu(enQuickWithdrawMenu Menu) { switch (Menu) { case enQuickWithdrawMenu::eTwenty: CalculateQickwithdraw(20); BackToMainMenu(); break; case enQuickWithdrawMenu::eFifty: CalculateQickwithdraw(50); BackToMainMenu(); break; case enQuickWithdrawMenu::eOnehundred: CalculateQickwithdraw(100); BackToMainMenu(); break; case enQuickWithdrawMenu::eTwoHundred: CalculateQickwithdraw(200); BackToMainMenu(); break; case enQuickWithdrawMenu::eFourhundred: CalculateQickwithdraw(400); BackToMainMenu(); break; case enQuickWithdrawMenu::eSixHundred: CalculateQickwithdraw(600); BackToMainMenu(); break; case enQuickWithdrawMenu::eEighthundred: CalculateQickwithdraw(800); BackToMainMenu(); break; case enQuickWithdrawMenu::eOneThousand: CalculateQickwithdraw(1000); BackToMainMenu(); break; case enQuickWithdrawMenu::eEixt: showATMMainMenuScreen(); break; } } void ShowQuickWithdraw() { system("cls"); cout << "======================================================\n"; cout << "\t\tQuick Withdraw Screen\n"; cout << "======================================================\n"; cout << "[1] 20 "; cout << "\t\t\t[2] 50 \n"; cout << "[3] 100 "; cout << "\t\t[4] 200 \n"; cout << "[5] 400 "; cout << "\t\t[6] 600 \n"; cout << "[7] 800 "; cout << "\t\t[8] 1000 \n"; cout << "[9] Exit \n"; cout << "======================================================\n"; cout << "\nYour Balance is " << CurrentClient.AccountBalance << endl; PerformanceQuickWithdrawMenu((enQuickWithdrawMenu)ReadQuickWithdrawMenuOption()); system("pause>0"); } void CheckBalanceMenu() { system("cls"); cout << "======================================================\n"; cout << "\t\tCheck Balance Screen\n"; cout << "======================================================\n"; cout << "\nYour Balance is " << CurrentClient.AccountBalance << endl; } bool NormalWithdraw(vector<sClient>& Vclients) { int WithdrawAmount = 0; char Answer = 'n'; do { cout << "Enter an amount of muliple 5's "; cin >> WithdrawAmount; } while (WithdrawAmount % 5 != 0); while (CurrentClient.AccountBalance < WithdrawAmount) { cout << "\nAmount Exceeds the balance, you can withdraw up to : " << CurrentClient.AccountBalance; cout << "\nPlease enter another amount ?\n"; cin >> WithdrawAmount; } cout << "\nAre you sure you want perform this transaction?y/n?"; cin >> Answer; if (Answer == 'y' || Answer == 'Y') { for (sClient& C : Vclients) { if (C.accountNumber == CurrentClient.accountNumber) { CurrentClient.AccountBalance -= WithdrawAmount; SaveVectorToFile(ClientsFileName, Vclients); //Refresh Clients Vclients = LoadDataFormClientFileToVector(ClientsFileName); cout << "\n\nDone Successfully. New balance is "; cout << CurrentClient.AccountBalance; return true; } } } else { return false; } } bool Despite(vector<sClient>& Vclients) { int DespiteOfAmount = 0; char Answer = 'n'; do { cout << "Enter a positive amount ? "; cin >> DespiteOfAmount; } while (DespiteOfAmount > 0); cout << "\nAre you sure you want perform this transaction?y/n?"; cin >> Answer; if (Answer == 'y' || Answer == 'Y') { for (sClient& C : Vclients) { if (C.accountNumber == CurrentClient.accountNumber) { CurrentClient.AccountBalance += DespiteOfAmount; SaveVectorToFile(ClientsFileName, Vclients); //Refresh Clients Vclients = LoadDataFormClientFileToVector(ClientsFileName); cout << "\n\nDone Successfully. New balance is "; cout << CurrentClient.AccountBalance; return true; } } } else { return false; } } void ShowNormalWithdraw() { system("cls"); cout << "======================================================\n"; cout << "\t\tNormal Withdraw Screen\n"; cout << "======================================================\n"; vector<sClient> Vclients = LoadDataFormClientFileToVector(ClientsFileName); NormalWithdraw(Vclients); } void ShowDespiteMenu() { system("cls"); cout << "======================================================\n"; cout << "\t\tDespite Screen\n"; cout << "======================================================\n"; vector<sClient> Vclients = LoadDataFormClientFileToVector(ClientsFileName); Despite(Vclients); } void PerformATMMainMenu(enATMMainMenu Menu) { switch (Menu) { case enATMMainMenu::eQuickWithdraw: ShowQuickWithdraw(); BackToMainMenu(); break; case enATMMainMenu::eNormalWithdraw: ShowNormalWithdraw(); BackToMainMenu(); break; case enATMMainMenu::eDesposit: ShowDespiteMenu(); BackToMainMenu(); break; case enATMMainMenu::eCheckBalance: CheckBalanceMenu(); BackToMainMenu(); break; case enATMMainMenu::eLogOut: LogOutScreen(); break; } } void showATMMainMenuScreen() { system("cls"); cout << "======================================================\n"; cout << "\t\tATM Main Menu Screen\n"; cout << "======================================================\n"; cout << "\t[1]Quick withdraw .\n"; cout << "\t[2]Normal Withdraw.\n"; cout << "\t[3]Deposit .\n"; cout << "\t[4]check Balance\n"; cout << "\t[5]Log out\n"; cout << "======================================================\n"; PerformATMMainMenu((enATMMainMenu)ReadATMMenuOption()); } void Login() { bool LoginFaild = false; string AccountNumber, Pincode; do { system("cls"); cout << "----------------------------------------------\n"; cout << "\tlogin screen :" << endl; cout << "----------------------------------------------\n"; if (LoginFaild) { cout << "Invalid AccountNumber / pincode !\n"; } cout << "Enter accountNumber ?\n"; cin >> AccountNumber; cout << "Enter pincode ?\n"; cin >> Pincode; LoginFaild = !LoadAccountNumberInfo(AccountNumber, Pincode); } while (LoginFaild); showATMMainMenuScreen(); } void WriteMode(string FileName) { fstream MyFile; MyFile.open(FileName, ios::out);//writeMode if (MyFile.is_open()) { MyFile << "A150#//#1234#//#Mohammed Abu-hadhoud#//#010243455#//#300000.000000" << endl; MyFile << "A152#//#1234#//#Maher Ahmed#//#3432454534#//#5000.000000\n"; MyFile << "A153#//#1234#//#Omar Mohammmed#//#0125567#//#1000.000000\n"; MyFile << "A154#//#1234#//#Ali Mohsan#//#0125567#//#544.000000\n"; MyFile << "A300#//#1234#//#Hilal Ahmed#//#0125567#//#6500.000000\n"; MyFile << "A151#//#1234#//#kholud Ahmed#//#01034556#//#80605.000000\n"; } MyFile.close(); } void PrintFileContent(string FileName) { fstream MyFile; MyFile.open(FileName, ios::in); if (MyFile.is_open()) { string line; while (getline(MyFile, line)) { cout << line << endl; } MyFile.close(); } } int main() { //Login(); WriteMode(ClientsFileName); PrintFileContent(ClientsFileName); system("Pause>0"); return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}