Advertisement
xhevatibraimi

Untitled

Oct 21st, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.92 KB | None | 0 0
  1. #include "pch.h"
  2. #include <fstream>
  3. #include <iostream>
  4. #include <stdexcept>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. class Account
  10. {
  11. public:
  12.     void setAccName(string name)
  13.     {
  14.         accName = name;
  15.     };
  16.  
  17.     void setAccType(bool type)
  18.     {
  19.         accType = type;
  20.     };
  21.  
  22.     void setAccBalance(double balance)
  23.     {
  24.         if (balance < 0.0 && !getAccType())
  25.         {
  26.             throw runtime_error("account cannot hold negative balance");
  27.         }
  28.  
  29.         accBalance = accBalance + balance;
  30.     };
  31.  
  32.     string getAccName()
  33.     {
  34.         return accName;
  35.     };
  36.  
  37.     bool getAccType()
  38.     {
  39.         return accType;
  40.     };
  41.  
  42.     double getAccBalance()
  43.     {
  44.         return accBalance;
  45.     };
  46.  
  47. private:
  48.     string accName;
  49.     double accBalance;
  50.     bool accType;
  51. };
  52.  
  53. class BankVector {
  54. private:
  55.     Account* entries;
  56.     // array of Account objects
  57.     unsigned int usedCapacity;
  58.     // number of elements in entries that is in use
  59.     // 0 <= usedCapacity <= maxCapacity
  60.     unsigned int maxCapacity;
  61.     // number of elements that can be stored in entries
  62. public:
  63.     BankVector() {
  64.         // creates BankVector with capacity to hold 0 Account elements
  65.         entries = new Account[0];
  66.         usedCapacity = 0;
  67.         maxCapacity = 0;
  68.     }
  69.     ~BankVector() {
  70.         delete[] entries;
  71.     }
  72.  
  73.     unsigned int size() const
  74.     {
  75.         return usedCapacity;
  76.     };
  77.  
  78.     Account& at(unsigned int index) const
  79.     {
  80.         if (index < 0 || index>usedCapacity - 1 || maxCapacity == 0)
  81.         {
  82.             throw new IndexError();
  83.         }
  84.         return entries[index];
  85.     }
  86.  
  87.     void push_back(Account a)
  88.     {
  89.         if (usedCapacity == maxCapacity)
  90.         {
  91.             Account* temporaryArray = new Account[maxCapacity + 1];
  92.             for (int i = 0; i < usedCapacity; i++)
  93.             {
  94.                 temporaryArray[i] = entries[i];
  95.             }
  96.             temporaryArray[usedCapacity] = a;
  97.             entries = temporaryArray;
  98.             maxCapacity++;
  99.         }
  100.         else
  101.         {
  102.             entries[usedCapacity] = a;
  103.         }
  104.         usedCapacity++;
  105.     }
  106.  
  107.     // removes Account at index
  108.     // decrements value of size()
  109.     // requires: 0 <= index < size
  110.     // throws: BankVector::IndexError
  111.     void erase(unsigned int index)
  112.     {
  113.         if (index < 0 || index > usedCapacity - 1)
  114.         {
  115.             throw new IndexError();
  116.         }
  117.         for (int i = index; i < usedCapacity; i++)
  118.         {
  119.             entries[i] = entries[i + 1];
  120.         }
  121.         usedCapacity--;
  122.     }
  123.  
  124.     class IndexError {};
  125.     // exception to be thrown
  126. };
  127.  
  128. class Registry
  129. {
  130. public:
  131.     bool exists(const string accName)
  132.     {
  133.         for (int i = 0; i < registry.size(); i++)
  134.         {
  135.             if (accName == registry.at(i).getAccName())
  136.             {
  137.                 return true;
  138.             }
  139.         }
  140.         return false;
  141.     };
  142.  
  143.     void transaction(const string accName, double accBalance)
  144.     {
  145.         fetchAccount(accName).setAccBalance(accBalance);
  146.     };
  147.  
  148.     void createAccount(const string accName, bool accType)
  149.     {
  150.         if (exists(accName))
  151.         {
  152.             throw runtime_error("account already exists");
  153.         }
  154.  
  155.         Account newAccount;
  156.         newAccount.setAccName(accName);
  157.         newAccount.setAccType(accType);
  158.         newAccount.setAccBalance(0);
  159.         registry.push_back(newAccount);
  160.     };
  161.  
  162.     void removeAccount(const string accName)
  163.     {
  164.         for (int i = 0; i < registry.size(); i++)
  165.         {
  166.             if (registry.at(i).getAccName() == accName)
  167.             {
  168.                 if (registry.at(i).getAccBalance() >= 0)
  169.                 {
  170.                     registry.erase(i);
  171.                     return;
  172.                 }
  173.                 else
  174.                 {
  175.                     throw runtime_error("account holds negative balance");
  176.                 }
  177.             }
  178.         }
  179.         throw runtime_error("account does not exist");
  180.     }
  181.  
  182.     void print()
  183.     {
  184.         for (int i = 0; i < registry.size(); i++)
  185.         {
  186.             string ownerName = registry.at(i).getAccName();
  187.  
  188.             if (registry.at(i).getAccBalance() < 0)
  189.             {
  190.                 double owedAmount = abs(registry.at(i).getAccBalance());
  191.                 cout << ownerName << " owes " << owedAmount << " euros " << endl;
  192.             }
  193.             else
  194.             {
  195.                 double ownedAmount = registry.at(i).getAccBalance();
  196.                 cout << ownerName << " owns " << ownedAmount << " euros " << endl;
  197.             }
  198.         }
  199.     }
  200.  
  201. private:
  202.     BankVector registry;
  203.     Account& fetchAccount(const string accName)
  204.     {
  205.         for (int i = 0; i < registry.size(); i++)
  206.         {
  207.             if (registry.at(i).getAccName() == accName)
  208.             {
  209.                 return registry.at(i);
  210.             }
  211.         }
  212.         throw runtime_error("account does not exist");
  213.     }
  214. };
  215.  
  216. int main()
  217. {
  218.     ifstream inFS;
  219.     string filename;
  220.  
  221.     cout << "Enter the name of the records file: ";
  222.     cin >> filename;
  223.  
  224.     inFS.open(filename);
  225.  
  226.     if (!inFS.is_open())
  227.     {
  228.         cout << "Could not open file " << filename << endl;
  229.     }
  230.  
  231.     Registry Records;
  232.     int line = 0;
  233.  
  234.     while (!inFS.eof())
  235.     {
  236.         try
  237.         {
  238.             line++;
  239.             char action;
  240.             string name;
  241.             char isCredit;
  242.             double amount;
  243.  
  244.             inFS >> action >> name;
  245.  
  246.             if (!inFS) break;
  247.  
  248.             switch (action)
  249.             {
  250.             case 'c':
  251.                 inFS >> isCredit;
  252.                 Records.createAccount(name, isCredit == 'y');
  253.                 break;
  254.  
  255.             case 't':
  256.                 inFS >> amount;
  257.                 Records.transaction(name, amount);
  258.                 break;
  259.  
  260.             case 'r':
  261.                 Records.removeAccount(name);
  262.                 break;
  263.  
  264.             default:
  265.                 break;
  266.             }
  267.         }
  268.         catch (runtime_error &excpt)
  269.         {
  270.             cout << "error on line " << line << ": " << excpt.what() << endl;
  271.         }
  272.     }
  273.     cout << endl;
  274.  
  275.     Records.print();
  276.  
  277.     inFS.close();
  278.  
  279.     return 0;
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement