Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.50 KB | None | 0 0
  1. // BankAccountList.cpp
  2.  
  3. #include "BankAccount.h"
  4. #include "BankAccountList.h"
  5.  
  6. const string BankAccountList::toString() const
  7. {
  8.     ostringstream os;
  9.  
  10.     for (int i = 0; i < num_elements; i++) {
  11.         if (List[i].getAccountNumber() != "Account number is not set.") {
  12.             os << fixed << showpoint << setprecision(2);
  13.             os << "----------------------------------------------------------" << endl;
  14.             os << "Full Name: " << List[i].getFirstName() << " " << List[i].getLastName() << endl;
  15.             os << "Account Number: " << List[i].getAccountNumber() << endl;
  16.             os << "Balance: $" << setprecision(2) << fixed << List[i].getBalance() << endl;
  17.             os << "----------------------------------------------------------" << endl;
  18.         }
  19.     }
  20.  
  21.     return os.str();
  22. };
  23.  
  24. bool BankAccountList::findAccount(const string & actNum, int & i) const
  25. {
  26.     for (int x = 0; x < num_elements; x++) {
  27.         if (List[x].getAccountNumber() == actNum) {
  28.             i = x;
  29.             return true;
  30.         }
  31.     }
  32. }
  33.  
  34. void BankAccountList::addAccount(const BankAccount & BA)
  35. {
  36.     List[num_elements] = BA;
  37.     num_elements++;
  38. };
  39.  
  40. bool BankAccountList::depositMoney(const string & actNum, double money)
  41. {
  42.     for (int x = 0; x < num_elements; x++) {
  43.         if (List[x].getAccountNumber() == actNum) {
  44.             List[x].deposit(money);
  45.             return true;
  46.         }
  47.     }
  48. };
  49.  
  50. bool BankAccountList::withDrawMoney(const string & actNum, double money) {
  51.     for (int x = 0; x < num_elements; x++) {
  52.         if (List[x].getAccountNumber() == actNum) {
  53.             List[x].withdraw(money);
  54.             return true;
  55.         }
  56.     }
  57. };
  58.  
  59. bool BankAccountList::deleteAccount(const string & actNum) {
  60.  
  61.     bool found;
  62.     int whereFound = -1;
  63.     found = findAccount(actNum, whereFound);
  64.  
  65.     if (found) {
  66.         List[whereFound] = List[num_elements - 1];
  67.         num_elements--;
  68.  
  69.         if (list_state == 1)
  70.         {
  71.             sort(1);
  72.         }
  73.         else if (list_state == 2)
  74.         {
  75.             sort(2);
  76.         }
  77.         else if (list_state == 3)
  78.         {
  79.             sort(3);
  80.         }
  81.  
  82.         return true;
  83.     }
  84.  
  85.     return false;
  86. };
  87.  
  88. void BankAccountList::makeEmpty() {
  89.     list_state = 0;
  90.     num_elements = 0;
  91. };
  92.  
  93. bool BankAccountList::updateAccount() {
  94.     string accN;
  95.     cout << "Please enter your account number: ";
  96.     cin >> accN;
  97.  
  98.     for (int x = 0; x < num_elements; x++) {
  99.         if (List[x].getAccountNumber() == accN) {
  100.             string lName;
  101.             cout << "Please enter a new last name: ";
  102.             cin >> lName;
  103.             List[x].setLastName(lName);
  104.             list_state = 0;
  105.             return true;
  106.         }
  107.     }
  108.  
  109.     cout << "Account was not found..." << endl;
  110. };
  111.  
  112. void BankAccountList::setLastName(const string & lname, const string & actNum) {
  113.     for (int x = 0; x < num_elements; x++) {
  114.         if (List[x].getAccountNumber() == actNum) {
  115.             List[x].setLastName(lname);
  116.         }
  117.     }
  118. };
  119.  
  120. void BankAccountList::setAccountAt(BankAccount & BA, unsigned int index) {
  121.     if (index < num_elements && index >= 0) {
  122.         List[index] = BA;
  123.     }
  124.     else {
  125.         throw "Index provided is out of bounds for this List.";
  126.     }
  127. };
  128.  
  129. void BankAccountList::sort(int flag) {
  130.     if (flag == 1) {
  131.         for (int i = 0; i < num_elements; i++) {
  132.             for (int j = 0; j < num_elements - i - 1; j++) {
  133.                 if (List[j].getAccountNumber() > List[j + 1].getAccountNumber()) {
  134.                     BankAccount temp;
  135.                     temp = List[j];
  136.                     List[j] = List[j + 1];
  137.                     List[j + 1] = temp;
  138.                     list_state = 1;
  139.                 }
  140.             }
  141.         }
  142.     }
  143.     else if (flag == 2) {
  144.         for (int i = 0; i < num_elements; i++) {
  145.             for (int j = 0; j < num_elements - i - 1; j++) {
  146.                 if (List[j].getLastName() > List[j + 1].getLastName()) {
  147.                     BankAccount temp;
  148.                     temp = List[j];
  149.                     List[j] = List[j + 1];
  150.                     List[j + 1] = temp;
  151.                     list_state = 2;
  152.                 }
  153.             }
  154.         }
  155.     }
  156.     else if (flag == 3) {
  157.         for (int i = 0; i < num_elements; i++) {
  158.             for (int j = 0; j < num_elements - i - 1; j++) {
  159.                 if (List[j].getBalance() > List[j + 1].getBalance()) {
  160.                     BankAccount temp;
  161.                     temp = List[j];
  162.                     List[j] = List[j + 1];
  163.                     List[j + 1] = temp;
  164.                     list_state = 3;
  165.                 }
  166.             }
  167.         }
  168.     }
  169.     else {
  170.         cout << "Invalid flag." << endl;
  171.     }
  172. };
  173.  
  174. bool BankAccountList::getAccountAt(BankAccount & BA, unsigned int & index) const {
  175.     if (index < num_elements && index >= 0) {
  176.         BA = List[index];
  177.         return true;
  178.     }
  179.     else {
  180.         throw "Index provided was not found.";
  181.         return false;
  182.     }
  183. };
  184.  
  185. double BankAccountList::getBalance(const string & actNum) const {
  186.     for (int x = 0; x < num_elements; x++) {
  187.         if (List[x].getAccountNumber() == actNum) {
  188.             return List[x].getBalance();
  189.         }
  190.     }
  191. }
  192.  
  193. const string BankAccountList::getFirstName(const string & actNum) const {
  194.     for (int x = 0; x < num_elements; x++) {
  195.         if (List[x].getAccountNumber() == actNum) {
  196.             return List[x].getFirstName();
  197.         }
  198.     }
  199. };
  200.  
  201. const string BankAccountList::getFullName(const string & actNum) const {
  202.     for (int x = 0; x < num_elements; x++) {
  203.         if (List[x].getAccountNumber() == actNum) {
  204.             return List[x].getFullName();
  205.         }
  206.     }
  207. };
  208.  
  209. const string BankAccountList::getLastName(const string & actNum) const {
  210.     for (int x = 0; x < num_elements; x++) {
  211.         if (List[x].getAccountNumber() == actNum) {
  212.             return List[x].getLastName();
  213.         }
  214.     }
  215. };
  216.  
  217. int BankAccountList::getCapacity() {
  218.     return MAX;
  219. };
  220.  
  221. int BankAccountList::getListState() const {
  222.     return list_state;
  223. };
  224.  
  225. void BankAccountList::getInstance(BankAccountList & BAL) {
  226.     string actNum, fName, lName;
  227.     double bal;
  228.     cout << "Account Number: ";
  229.     cin >> actNum;
  230.     cout << "First Name: ";
  231.     cin >> fName;
  232.     cout << "Last Name: ";
  233.     cin >> lName;
  234.     cout << "Balance: $";
  235.     cin >> bal;
  236.  
  237.     BAL.addAccount(BankAccount(actNum, fName, lName, bal));
  238. };
  239.  
  240. void BankAccountList::getInstance(BankAccountList & BAL, ifstream & in) {
  241.     while (in.peek() != EOF) {
  242.         string actNum, fName, lName;
  243.         double bal;
  244.         in >> actNum >> lName >> fName >> bal;
  245.  
  246.         BAL.addAccount(BankAccount(actNum, fName, lName, bal));
  247.     }
  248. };
  249.  
  250. int BankAccountList::getNumberOfElements() const {
  251.     return num_elements;
  252. }
  253.  
  254. bool BankAccountList::isEmpty() const {
  255.     if (num_elements == 0) {
  256.         return true;
  257.     }
  258.     else {
  259.         return false;
  260.     }
  261. };
  262.  
  263. bool BankAccountList::isFull() const {
  264.     if (num_elements == MAX) {
  265.         return true;
  266.     }
  267.     else {
  268.         return false;
  269.     }
  270. };
  271.  
  272. void BankAccountList::print() const {
  273.     cout << "-------------------------------------------------------------" << endl;
  274.     cout << left << setw(25) << "First Name" << setw(30) << "Last Name" << setw(30) << "Balance" << setw(30) << "Account Number" << endl;
  275.     for (int i = 0; i < num_elements; i++) {
  276.         cout << fixed << showpoint << setprecision(2);
  277.         cout << left << setw(25) << List[i].getFirstName() << setw(30) << List[i].getLastName() << setw(30) << List[i].getBalance() << setw(30) << List[i].getAccountNumber() << endl;
  278.     }
  279. }
  280.  
  281. void BankAccountList::print(ostream & out) const {
  282.     out << "-------------------------------------------------------------" << endl;
  283.     out << left << setw(25) << "First Name" << setw(30) << "Last Name" << setw(30) << "Balance" << setw(30) << "Account Number" << endl;
  284.     for (int i = 0; i < num_elements; i++) {
  285.         out << fixed << showpoint << setprecision(2);
  286.         out << left << setw(25) << List[i].getFirstName() << setw(30) << List[i].getLastName() << setw(30) << List[i].getBalance() << setw(30) << List[i].getAccountNumber() << endl;
  287.     }
  288. }
  289.  
  290. void BankAccountList::getSmallestBalanceAccount(BankAccount & BA) const {
  291.     double money[30] = {};
  292.     for (int i = 0; i < num_elements; i++) {
  293.         money[i] = List[i].getBalance();
  294.     }
  295.  
  296.     int index = 0;
  297.     for (int x = 0; x < num_elements; x++) {
  298.         if (money[x] < money[index]) {
  299.             index = x;
  300.         }
  301.     }
  302.  
  303.     BA = List[index];
  304.  
  305. };
  306.  
  307. const string BankAccountList::listDescription() const {
  308.     ostringstream os;
  309.     if (getListState() == 0) {
  310.         os << "-----------------------------------------------" << endl;
  311.         os << "List is unsorted and has " << num_elements << " entries." << endl;
  312.         os << "-----------------------------------------------" << endl;
  313.     }
  314.     else if (getListState() == 1) {
  315.         os << "-----------------------------------------------" << endl;
  316.         os << "List is sorted for account number and has " << num_elements << " entries." << endl;
  317.         os << "-----------------------------------------------" << endl;
  318.     }
  319.     else if (getListState() == 2) {
  320.         os << "-----------------------------------------------" << endl;
  321.         os << "List is sorted for last name and has " << num_elements << " entries." << endl;
  322.         os << "-----------------------------------------------" << endl;
  323.     }
  324.     else if (getListState() == 3) {
  325.         os << "-----------------------------------------------" << endl;
  326.         os << "List is sorted for balance and has " << num_elements << " entries." << endl;
  327.         os << "-----------------------------------------------" << endl;
  328.     }
  329.     return os.str();
  330. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement