Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // BankAccountList.cpp
- #include "BankAccount.h"
- #include "BankAccountList.h"
- const string BankAccountList::toString() const
- {
- ostringstream os;
- for (int i = 0; i < num_elements; i++) {
- if (List[i].getAccountNumber() != "Account number is not set.") {
- os << fixed << showpoint << setprecision(2);
- os << "----------------------------------------------------------" << endl;
- os << "Full Name: " << List[i].getFirstName() << " " << List[i].getLastName() << endl;
- os << "Account Number: " << List[i].getAccountNumber() << endl;
- os << "Balance: $" << setprecision(2) << fixed << List[i].getBalance() << endl;
- os << "----------------------------------------------------------" << endl;
- }
- }
- return os.str();
- };
- bool BankAccountList::findAccount(const string & actNum, int & i) const
- {
- for (int x = 0; x < num_elements; x++) {
- if (List[x].getAccountNumber() == actNum) {
- i = x;
- return true;
- }
- }
- }
- void BankAccountList::addAccount(const BankAccount & BA)
- {
- List[num_elements] = BA;
- num_elements++;
- };
- bool BankAccountList::depositMoney(const string & actNum, double money)
- {
- for (int x = 0; x < num_elements; x++) {
- if (List[x].getAccountNumber() == actNum) {
- List[x].deposit(money);
- return true;
- }
- }
- };
- bool BankAccountList::withDrawMoney(const string & actNum, double money) {
- for (int x = 0; x < num_elements; x++) {
- if (List[x].getAccountNumber() == actNum) {
- List[x].withdraw(money);
- return true;
- }
- }
- };
- bool BankAccountList::deleteAccount(const string & actNum) {
- bool found;
- int whereFound = -1;
- found = findAccount(actNum, whereFound);
- if (found) {
- List[whereFound] = List[num_elements - 1];
- num_elements--;
- if (list_state == 1)
- {
- sort(1);
- }
- else if (list_state == 2)
- {
- sort(2);
- }
- else if (list_state == 3)
- {
- sort(3);
- }
- return true;
- }
- return false;
- };
- void BankAccountList::makeEmpty() {
- list_state = 0;
- num_elements = 0;
- };
- bool BankAccountList::updateAccount() {
- string accN;
- cout << "Please enter your account number: ";
- cin >> accN;
- for (int x = 0; x < num_elements; x++) {
- if (List[x].getAccountNumber() == accN) {
- string lName;
- cout << "Please enter a new last name: ";
- cin >> lName;
- List[x].setLastName(lName);
- list_state = 0;
- return true;
- }
- }
- cout << "Account was not found..." << endl;
- };
- void BankAccountList::setLastName(const string & lname, const string & actNum) {
- for (int x = 0; x < num_elements; x++) {
- if (List[x].getAccountNumber() == actNum) {
- List[x].setLastName(lname);
- }
- }
- };
- void BankAccountList::setAccountAt(BankAccount & BA, unsigned int index) {
- if (index < num_elements && index >= 0) {
- List[index] = BA;
- }
- else {
- throw "Index provided is out of bounds for this List.";
- }
- };
- void BankAccountList::sort(int flag) {
- if (flag == 1) {
- for (int i = 0; i < num_elements; i++) {
- for (int j = 0; j < num_elements - i - 1; j++) {
- if (List[j].getAccountNumber() > List[j + 1].getAccountNumber()) {
- BankAccount temp;
- temp = List[j];
- List[j] = List[j + 1];
- List[j + 1] = temp;
- list_state = 1;
- }
- }
- }
- }
- else if (flag == 2) {
- for (int i = 0; i < num_elements; i++) {
- for (int j = 0; j < num_elements - i - 1; j++) {
- if (List[j].getLastName() > List[j + 1].getLastName()) {
- BankAccount temp;
- temp = List[j];
- List[j] = List[j + 1];
- List[j + 1] = temp;
- list_state = 2;
- }
- }
- }
- }
- else if (flag == 3) {
- for (int i = 0; i < num_elements; i++) {
- for (int j = 0; j < num_elements - i - 1; j++) {
- if (List[j].getBalance() > List[j + 1].getBalance()) {
- BankAccount temp;
- temp = List[j];
- List[j] = List[j + 1];
- List[j + 1] = temp;
- list_state = 3;
- }
- }
- }
- }
- else {
- cout << "Invalid flag." << endl;
- }
- };
- bool BankAccountList::getAccountAt(BankAccount & BA, unsigned int & index) const {
- if (index < num_elements && index >= 0) {
- BA = List[index];
- return true;
- }
- else {
- throw "Index provided was not found.";
- return false;
- }
- };
- double BankAccountList::getBalance(const string & actNum) const {
- for (int x = 0; x < num_elements; x++) {
- if (List[x].getAccountNumber() == actNum) {
- return List[x].getBalance();
- }
- }
- }
- const string BankAccountList::getFirstName(const string & actNum) const {
- for (int x = 0; x < num_elements; x++) {
- if (List[x].getAccountNumber() == actNum) {
- return List[x].getFirstName();
- }
- }
- };
- const string BankAccountList::getFullName(const string & actNum) const {
- for (int x = 0; x < num_elements; x++) {
- if (List[x].getAccountNumber() == actNum) {
- return List[x].getFullName();
- }
- }
- };
- const string BankAccountList::getLastName(const string & actNum) const {
- for (int x = 0; x < num_elements; x++) {
- if (List[x].getAccountNumber() == actNum) {
- return List[x].getLastName();
- }
- }
- };
- int BankAccountList::getCapacity() {
- return MAX;
- };
- int BankAccountList::getListState() const {
- return list_state;
- };
- void BankAccountList::getInstance(BankAccountList & BAL) {
- string actNum, fName, lName;
- double bal;
- cout << "Account Number: ";
- cin >> actNum;
- cout << "First Name: ";
- cin >> fName;
- cout << "Last Name: ";
- cin >> lName;
- cout << "Balance: $";
- cin >> bal;
- BAL.addAccount(BankAccount(actNum, fName, lName, bal));
- };
- void BankAccountList::getInstance(BankAccountList & BAL, ifstream & in) {
- while (in.peek() != EOF) {
- string actNum, fName, lName;
- double bal;
- in >> actNum >> lName >> fName >> bal;
- BAL.addAccount(BankAccount(actNum, fName, lName, bal));
- }
- };
- int BankAccountList::getNumberOfElements() const {
- return num_elements;
- }
- bool BankAccountList::isEmpty() const {
- if (num_elements == 0) {
- return true;
- }
- else {
- return false;
- }
- };
- bool BankAccountList::isFull() const {
- if (num_elements == MAX) {
- return true;
- }
- else {
- return false;
- }
- };
- void BankAccountList::print() const {
- cout << "-------------------------------------------------------------" << endl;
- cout << left << setw(25) << "First Name" << setw(30) << "Last Name" << setw(30) << "Balance" << setw(30) << "Account Number" << endl;
- for (int i = 0; i < num_elements; i++) {
- cout << fixed << showpoint << setprecision(2);
- cout << left << setw(25) << List[i].getFirstName() << setw(30) << List[i].getLastName() << setw(30) << List[i].getBalance() << setw(30) << List[i].getAccountNumber() << endl;
- }
- }
- void BankAccountList::print(ostream & out) const {
- out << "-------------------------------------------------------------" << endl;
- out << left << setw(25) << "First Name" << setw(30) << "Last Name" << setw(30) << "Balance" << setw(30) << "Account Number" << endl;
- for (int i = 0; i < num_elements; i++) {
- out << fixed << showpoint << setprecision(2);
- out << left << setw(25) << List[i].getFirstName() << setw(30) << List[i].getLastName() << setw(30) << List[i].getBalance() << setw(30) << List[i].getAccountNumber() << endl;
- }
- }
- void BankAccountList::getSmallestBalanceAccount(BankAccount & BA) const {
- double money[30] = {};
- for (int i = 0; i < num_elements; i++) {
- money[i] = List[i].getBalance();
- }
- int index = 0;
- for (int x = 0; x < num_elements; x++) {
- if (money[x] < money[index]) {
- index = x;
- }
- }
- BA = List[index];
- };
- const string BankAccountList::listDescription() const {
- ostringstream os;
- if (getListState() == 0) {
- os << "-----------------------------------------------" << endl;
- os << "List is unsorted and has " << num_elements << " entries." << endl;
- os << "-----------------------------------------------" << endl;
- }
- else if (getListState() == 1) {
- os << "-----------------------------------------------" << endl;
- os << "List is sorted for account number and has " << num_elements << " entries." << endl;
- os << "-----------------------------------------------" << endl;
- }
- else if (getListState() == 2) {
- os << "-----------------------------------------------" << endl;
- os << "List is sorted for last name and has " << num_elements << " entries." << endl;
- os << "-----------------------------------------------" << endl;
- }
- else if (getListState() == 3) {
- os << "-----------------------------------------------" << endl;
- os << "List is sorted for balance and has " << num_elements << " entries." << endl;
- os << "-----------------------------------------------" << endl;
- }
- return os.str();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement