Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "BaseAccount.h"
- #include "BankingSystem.h"
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <regex>
- #include <string>
- using namespace std;
- void displayMenu()
- {
- cout << "1: Open a new account" << endl;
- cout << "2: View an account" << endl;
- cout << "3: Close an account" << endl;
- cout << "4: Exit the program" << endl;
- }
- void getInput()
- {
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- }
- void BankingSystem::Start()
- {
- loadAccounts();
- while (true)
- {
- system("cls");
- cin.clear();
- displayMenu();
- int selection = 0;
- cin >> selection;
- getInput();
- switch (selection) {
- case 1:
- addAccountMenu();
- break;
- case 2:
- viewAccount();
- break;
- case 3:
- closeAccount();
- break;
- case 4:
- exit(0);
- break;
- }
- }
- }
- void BankingSystem::viewAccount()
- {
- system("cls");
- cout << "Enter the account ID" << endl;
- int id = 0;
- cin >> id;
- system("cls");
- bankAccount* curAccount = getBankAccount(id);
- if (curAccount != nullptr)
- {
- curAccount->printDetails();
- }
- else
- {
- cout << "This account was not found. Try again" << endl;
- }
- getInput();
- cin.get();
- }
- bankAccount* BankingSystem::getBankAccount(int id)
- {
- for (int i = 0; i < bankAccountDatabase.size(); i++)
- {
- if (id == bankAccountDatabase.at(i).id)
- {
- return &bankAccountDatabase.at(i);
- }
- }
- return nullptr;
- }
- int BankingSystem::findNewBankAccountID()
- {
- int MAXID = 0;
- for (int i = 0; i < bankAccountDatabase.size(); i++)
- {
- if (MAXID < bankAccountDatabase.at(i).id)
- {
- MAXID = bankAccountDatabase.at(i).id;
- }
- }
- return MAXID;
- }
- int BankingSystem::getBankAccountIndex(int id)
- {
- for (int i = 0; i < bankAccountDatabase.size(); i++)
- {
- if (id == bankAccountDatabase.at(i).id)
- {
- return i;
- }
- }
- return -1;
- }
- void BankingSystem::addAccountMenu()
- {
- system("cls");
- string surName, foreName, address;
- int balance = 0, type = 0;
- while (type > 4 || type <= 0)
- {
- cout << "Enter the account type: " << endl;
- cout << "1: General account" << endl;
- cout << "2: Junior account" << endl;
- cout << "3: Savings account" << endl;
- cout << "4: Return to the main menu" << endl;
- cin >> type;
- getInput();
- if (type == 4) {
- return;
- }
- }
- system("cls");
- cout << "Enter your surname: " << endl;
- cin >> surName;
- getInput();
- system("cls");
- cout << "Enter your forename: " << endl;
- cin >> foreName;
- getInput();
- system("cls");
- cout << "Enter your address: " << endl;
- cin >> address;
- getInput();
- system("cls");
- cout << "Enter your balance: " << endl;
- cin >> balance;
- getInput();
- if (balance <= 0)
- {
- // Has entered a number below 0 or 0 we need to return out of this
- cout << "You need to enter a balance above 0";
- cin.get();
- return;
- }
- system("cls");
- int id = addAccount(surName, foreName, address, balance, type);
- cout << "The account has been created ID: " << id;
- cin.get();
- }
- int BankingSystem::addAccount(string surName, string foreName, string address, int balance, int type)
- {
- bankAccount curBankAccount;
- curBankAccount.surName = surName;
- curBankAccount.foreName = foreName;
- curBankAccount.address = address;
- curBankAccount.balance = balance;
- curBankAccount.accountType = type;
- curBankAccount.id = findNewBankAccountID() + 1;
- bankAccountDatabase.push_back(curBankAccount);
- saveAccounts();
- return curBankAccount.id;
- }
- void BankingSystem::closeAccount()
- {
- cout << "Enter the account ID" << endl;
- int id = 0;
- cin >> id;
- getInput();
- int index = getBankAccountIndex(id);
- if (index != -1)
- {
- bankAccountDatabase.erase(bankAccountDatabase.begin() + index, bankAccountDatabase.begin() + index + 1);
- saveAccounts();
- }
- else
- {
- cout << "You cannot close an account that does not exist" << endl;
- cin.get();
- }
- }
- void BankingSystem::loadAccounts()
- {
- fstream myfile;
- string line;
- myfile.open("bankAccount.txt");
- while (getline(myfile, line))
- {
- regex a("(.*)-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)");
- smatch matches;
- if (regex_match(line, matches, a)) {
- bankAccount curBankAccount;
- curBankAccount.id = stoi(matches[1]);
- curBankAccount.surName = matches[2];
- curBankAccount.foreName = matches[3];
- curBankAccount.address = matches[4];
- curBankAccount.balance = stoi(matches[5]);
- curBankAccount.interestRate = stof(matches[6]);
- curBankAccount.accountType = stoi(matches[7]);
- bankAccountDatabase.push_back(curBankAccount);
- }
- }
- }
- void BankingSystem::saveAccounts()
- {
- ofstream myfile;
- string line;
- int i = 0;
- myfile.open("bankAccount.txt", ios::out | ios::trunc);
- for (int i = 0; i < bankAccountDatabase.size(); ++i) {
- if (bankAccountDatabase[i].id == -1)
- continue;
- bankAccount curAccount = bankAccountDatabase.at(i);
- string str = to_string(curAccount.id) + "-" +
- curAccount.surName + "-" +
- curAccount.foreName + "-" +
- curAccount.address + "-" +
- to_string(curAccount.balance) + "-" +
- to_string(curAccount.interestRate) + "-" +
- to_string(curAccount.accountType) + "\n";
- myfile << str;
- }
- myfile.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement