Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <fstream>
- #include <iostream>
- #include <stdexcept>
- #include <string>
- using namespace std;
- class Account
- {
- public:
- void setAccName(string name)
- {
- accName = name;
- };
- void setAccType(bool type)
- {
- accType = type;
- };
- void setAccBalance(double balance)
- {
- if (balance < 0.0 && !getAccType())
- {
- throw runtime_error("account cannot hold negative balance");
- }
- accBalance = accBalance + balance;
- };
- string getAccName()
- {
- return accName;
- };
- bool getAccType()
- {
- return accType;
- };
- double getAccBalance()
- {
- return accBalance;
- };
- private:
- string accName;
- double accBalance;
- bool accType;
- };
- class BankVector {
- private:
- Account* entries;
- // array of Account objects
- unsigned int usedCapacity;
- // number of elements in entries that is in use
- // 0 <= usedCapacity <= maxCapacity
- unsigned int maxCapacity;
- // number of elements that can be stored in entries
- public:
- BankVector() {
- // creates BankVector with capacity to hold 0 Account elements
- entries = new Account[0];
- usedCapacity = 0;
- maxCapacity = 0;
- }
- ~BankVector() {
- delete[] entries;
- }
- unsigned int size() const
- {
- return usedCapacity;
- };
- Account& at(unsigned int index) const
- {
- if (index < 0 || index>usedCapacity - 1 || maxCapacity == 0)
- {
- throw new IndexError();
- }
- return entries[index];
- }
- void push_back(Account a)
- {
- if (usedCapacity == maxCapacity)
- {
- Account* temporaryArray = new Account[maxCapacity + 1];
- for (int i = 0; i < usedCapacity; i++)
- {
- temporaryArray[i] = entries[i];
- }
- temporaryArray[usedCapacity] = a;
- entries = temporaryArray;
- maxCapacity++;
- }
- else
- {
- entries[usedCapacity] = a;
- }
- usedCapacity++;
- }
- // removes Account at index
- // decrements value of size()
- // requires: 0 <= index < size
- // throws: BankVector::IndexError
- void erase(unsigned int index)
- {
- if (index < 0 || index > usedCapacity - 1)
- {
- throw new IndexError();
- }
- for (int i = index; i < usedCapacity; i++)
- {
- entries[i] = entries[i + 1];
- }
- usedCapacity--;
- }
- class IndexError {};
- // exception to be thrown
- };
- class Registry
- {
- public:
- bool exists(const string accName)
- {
- for (int i = 0; i < registry.size(); i++)
- {
- if (accName == registry.at(i).getAccName())
- {
- return true;
- }
- }
- return false;
- };
- void transaction(const string accName, double accBalance)
- {
- fetchAccount(accName).setAccBalance(accBalance);
- };
- void createAccount(const string accName, bool accType)
- {
- if (exists(accName))
- {
- throw runtime_error("account already exists");
- }
- Account newAccount;
- newAccount.setAccName(accName);
- newAccount.setAccType(accType);
- newAccount.setAccBalance(0);
- registry.push_back(newAccount);
- };
- void removeAccount(const string accName)
- {
- for (int i = 0; i < registry.size(); i++)
- {
- if (registry.at(i).getAccName() == accName)
- {
- if (registry.at(i).getAccBalance() >= 0)
- {
- registry.erase(i);
- return;
- }
- else
- {
- throw runtime_error("account holds negative balance");
- }
- }
- }
- throw runtime_error("account does not exist");
- }
- void print()
- {
- for (int i = 0; i < registry.size(); i++)
- {
- string ownerName = registry.at(i).getAccName();
- if (registry.at(i).getAccBalance() < 0)
- {
- double owedAmount = abs(registry.at(i).getAccBalance());
- cout << ownerName << " owes " << owedAmount << " euros " << endl;
- }
- else
- {
- double ownedAmount = registry.at(i).getAccBalance();
- cout << ownerName << " owns " << ownedAmount << " euros " << endl;
- }
- }
- }
- private:
- BankVector registry;
- Account& fetchAccount(const string accName)
- {
- for (int i = 0; i < registry.size(); i++)
- {
- if (registry.at(i).getAccName() == accName)
- {
- return registry.at(i);
- }
- }
- throw runtime_error("account does not exist");
- }
- };
- int main()
- {
- ifstream inFS;
- string filename;
- cout << "Enter the name of the records file: ";
- cin >> filename;
- inFS.open(filename);
- if (!inFS.is_open())
- {
- cout << "Could not open file " << filename << endl;
- }
- Registry Records;
- int line = 0;
- while (!inFS.eof())
- {
- try
- {
- line++;
- char action;
- string name;
- char isCredit;
- double amount;
- inFS >> action >> name;
- if (!inFS) break;
- switch (action)
- {
- case 'c':
- inFS >> isCredit;
- Records.createAccount(name, isCredit == 'y');
- break;
- case 't':
- inFS >> amount;
- Records.transaction(name, amount);
- break;
- case 'r':
- Records.removeAccount(name);
- break;
- default:
- break;
- }
- }
- catch (runtime_error &excpt)
- {
- cout << "error on line " << line << ": " << excpt.what() << endl;
- }
- }
- cout << endl;
- Records.print();
- inFS.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement