Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.26 KB | None | 0 0
  1. #include "BaseAccount.h"
  2. #include "BankingSystem.h"
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <fstream>
  7. #include <regex>
  8. #include <string>
  9.  
  10. using namespace std;
  11.  
  12. void displayMenu()
  13. {
  14.  
  15.     cout << "1: Open a new account" << endl;
  16.     cout << "2: View an account" << endl;
  17.     cout << "3: Close an account" << endl;
  18.     cout << "4: Exit the program" << endl;
  19.  
  20.  
  21. }
  22.  
  23. void getInput()
  24. {
  25.     cin.clear();
  26.     cin.ignore(numeric_limits<streamsize>::max(), '\n');
  27.  
  28. }
  29.  
  30. void BankingSystem::Start()
  31. {
  32.     loadAccounts();
  33.  
  34.     while (true)
  35.     {
  36.         system("cls");
  37.         cin.clear();
  38.  
  39.         displayMenu();
  40.         int selection = 0;
  41.         cin >> selection;
  42.  
  43.         getInput();
  44.  
  45.         switch (selection) {
  46.  
  47.             case 1:
  48.                 addAccountMenu();
  49.                 break;
  50.  
  51.             case 2:
  52.                 viewAccount();
  53.                 break;
  54.  
  55.             case 3:
  56.                 closeAccount();
  57.                 break;
  58.  
  59.             case 4:
  60.                 exit(0);
  61.                 break;
  62.                
  63.  
  64.         }
  65.  
  66.     }
  67.  
  68. }
  69.  
  70.  
  71. void BankingSystem::viewAccount()
  72. {
  73.     system("cls");
  74.     cout << "Enter the account ID" << endl;
  75.     int id = 0;
  76.     cin >> id;
  77.  
  78.     system("cls");
  79.  
  80.     bankAccount* curAccount = getBankAccount(id);
  81.  
  82.     if (curAccount != nullptr)
  83.     {
  84.         curAccount->printDetails();
  85.     }
  86.     else
  87.     {
  88.         cout << "This account was not found. Try again" << endl;
  89.     }
  90.  
  91.     getInput();
  92.     cin.get();
  93. }
  94.  
  95.  
  96. bankAccount* BankingSystem::getBankAccount(int id)
  97. {
  98.     for (int i = 0; i < bankAccountDatabase.size(); i++)
  99.     {
  100.  
  101.         if (id == bankAccountDatabase.at(i).id)
  102.         {
  103.             return &bankAccountDatabase.at(i);
  104.         }
  105.  
  106.     }
  107.  
  108.     return nullptr;
  109. }
  110.  
  111. int BankingSystem::findNewBankAccountID()
  112. {
  113.     int MAXID = 0;
  114.  
  115.     for (int i = 0; i < bankAccountDatabase.size(); i++)
  116.     {
  117.  
  118.         if (MAXID < bankAccountDatabase.at(i).id)
  119.         {
  120.             MAXID = bankAccountDatabase.at(i).id;
  121.         }
  122.  
  123.     }
  124.  
  125.     return MAXID;
  126. }
  127.  
  128. int BankingSystem::getBankAccountIndex(int id)
  129. {
  130.     for (int i = 0; i < bankAccountDatabase.size(); i++)
  131.     {
  132.  
  133.         if (id == bankAccountDatabase.at(i).id)
  134.         {
  135.             return i;
  136.         }
  137.  
  138.     }
  139.  
  140.     return -1;
  141. }
  142.  
  143.  
  144. void BankingSystem::addAccountMenu()
  145. {
  146.     system("cls");
  147.  
  148.     string surName, foreName, address;
  149.     int balance = 0, type = 0;
  150.  
  151.     while (type > 4 || type <= 0)
  152.     {
  153.  
  154.         cout << "Enter the account type: " << endl;
  155.         cout << "1: General account" << endl;
  156.         cout << "2: Junior account" << endl;
  157.         cout << "3: Savings account" << endl;
  158.         cout << "4: Return to the main menu" << endl;
  159.  
  160.         cin >> type;
  161.  
  162.         getInput();
  163.  
  164.         if (type == 4) {
  165.             return;
  166.         }
  167.  
  168.     }
  169.  
  170.     system("cls");
  171.     cout << "Enter your surname: " << endl;
  172.     cin >> surName;
  173.  
  174.     getInput();
  175.     system("cls");
  176.  
  177.     cout << "Enter your forename: " << endl;
  178.     cin >> foreName;
  179.  
  180.     getInput();
  181.     system("cls");
  182.  
  183.     cout << "Enter your address: " << endl;
  184.     cin >> address;
  185.  
  186.     getInput();
  187.     system("cls");
  188.  
  189.     cout << "Enter your balance: " << endl;
  190.     cin >> balance;
  191.  
  192.     getInput();
  193.  
  194.     if (balance <= 0)
  195.     {
  196.         // Has entered a number below 0 or 0 we need to return out of this
  197.        
  198.         cout << "You need to enter a balance above 0";
  199.         cin.get();
  200.         return;
  201.     }
  202.  
  203.     system("cls");
  204.  
  205.     int id = addAccount(surName, foreName, address, balance, type);
  206.  
  207.     cout << "The account has been created ID: " << id;
  208.     cin.get();
  209.  
  210. }
  211.  
  212. int BankingSystem::addAccount(string surName, string foreName, string address, int balance, int type)
  213. {
  214.  
  215.     bankAccount curBankAccount;
  216.     curBankAccount.surName = surName;
  217.     curBankAccount.foreName = foreName;
  218.     curBankAccount.address = address;
  219.     curBankAccount.balance = balance;
  220.     curBankAccount.accountType = type;
  221.     curBankAccount.id = findNewBankAccountID() + 1;
  222.  
  223.     bankAccountDatabase.push_back(curBankAccount);
  224.     saveAccounts();
  225.  
  226.     return curBankAccount.id;
  227. }
  228.  
  229. void BankingSystem::closeAccount()
  230. {
  231.  
  232.     cout << "Enter the account ID" << endl;
  233.     int id = 0;
  234.     cin >> id;
  235.  
  236.     getInput();
  237.  
  238.     int index = getBankAccountIndex(id);
  239.  
  240.     if (index != -1)
  241.     {
  242.         bankAccountDatabase.erase(bankAccountDatabase.begin() + index, bankAccountDatabase.begin() + index + 1);
  243.         saveAccounts();
  244.     }
  245.     else
  246.     {
  247.         cout << "You cannot close an account that does not exist" << endl;
  248.         cin.get();
  249.     }
  250.  
  251.  
  252. }
  253.  
  254. void BankingSystem::loadAccounts()
  255. {
  256.     fstream myfile;
  257.     string line;
  258.  
  259.     myfile.open("bankAccount.txt");
  260.  
  261.     while (getline(myfile, line))
  262.     {
  263.         regex a("(.*)-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)");
  264.         smatch matches;
  265.  
  266.         if (regex_match(line, matches, a)) {
  267.  
  268.             bankAccount curBankAccount;
  269.             curBankAccount.id = stoi(matches[1]);
  270.             curBankAccount.surName = matches[2];
  271.             curBankAccount.foreName = matches[3];
  272.             curBankAccount.address = matches[4];
  273.             curBankAccount.balance = stoi(matches[5]);
  274.             curBankAccount.interestRate = stof(matches[6]);
  275.             curBankAccount.accountType = stoi(matches[7]);
  276.  
  277.             bankAccountDatabase.push_back(curBankAccount);
  278.  
  279.         }
  280.  
  281.     }
  282.  
  283. }
  284.  
  285. void BankingSystem::saveAccounts()
  286. {
  287.     ofstream myfile;
  288.     string line;
  289.  
  290.     int i = 0;
  291.  
  292.     myfile.open("bankAccount.txt", ios::out | ios::trunc);
  293.  
  294.     for (int i = 0; i < bankAccountDatabase.size(); ++i) {
  295.  
  296.         if (bankAccountDatabase[i].id == -1)
  297.             continue;
  298.  
  299.         bankAccount curAccount = bankAccountDatabase.at(i);
  300.  
  301.         string str = to_string(curAccount.id) + "-" +
  302.             curAccount.surName + "-" +
  303.             curAccount.foreName + "-" +
  304.             curAccount.address + "-" +
  305.             to_string(curAccount.balance) + "-" +
  306.             to_string(curAccount.interestRate) + "-" +
  307.             to_string(curAccount.accountType) + "\n";
  308.        
  309.         myfile << str;
  310.     }
  311.  
  312.     myfile.close();
  313.  
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement