Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.60 KB | None | 0 0
  1. // Compiled with: g++ -Wall -std=c++14 -pthread
  2.  
  3. #include <iostream>
  4. #include <memory>
  5. #include <sstream>
  6. #include <string>
  7. #include <vector>
  8. #include <fstream>
  9. #include <sstream>
  10. #include <algorithm>
  11.  
  12. enum class AccountType
  13. {
  14.     SavingAccount,
  15.     RegularAccount,
  16.     StudentAccount
  17. };
  18.  
  19.  
  20. //h
  21.  
  22. class IAccount
  23. {
  24. public:
  25.     IAccount() { std::cout << "Account created" << std::endl; }
  26.     virtual ~IAccount() { std::cout << "Account destroyed" << std::endl; }
  27.     virtual void deposit(double money) = 0;
  28.     virtual void withdraw(double money) = 0;
  29.     virtual void printInfo() const = 0;
  30.     virtual int Id() const = 0;
  31.     virtual int Id() = 0;
  32.  
  33. };
  34.  
  35.  
  36. class SavingAccount : public IAccount
  37. {
  38. private:
  39.     std::string name;
  40.     int ID;
  41.     double interest;
  42.     double savedMoney;
  43.  
  44.  
  45. public:
  46.     SavingAccount(const std::string& name, int ID, double interest, int money = 100)
  47.         : IAccount()
  48.         , name(name)
  49.         , ID(ID)
  50.         , interest(interest)
  51.         , savedMoney(money)
  52.     {
  53.     }
  54.  
  55.     void deposit(double money) override {
  56.         if (money >= 2000) {
  57.             money = money + money * interest / 100;
  58.         }
  59.         savedMoney += money;
  60.     }
  61.     void withdraw(double money) override {
  62.         int remaining = savedMoney - money;
  63.         if (remaining >= -1000) {
  64.             savedMoney = remaining;
  65.         }
  66.  
  67.     }
  68.     void applyInterest(double interest) {
  69.         savedMoney = savedMoney + savedMoney * interest / 100;
  70.     }
  71.     void printInfo() const override {
  72.         std::stringstream ss;
  73.         ss << "Saving account " << name << " " << ID << " " << savedMoney;
  74.         std::cout << ss.str() << std::endl;
  75.     }
  76.     int Id() const override { return ID; }
  77.     int Id() override { return ID; }
  78.  
  79.     bool operator==(const SavingAccount& rhs) { return ID == rhs.ID; }
  80.     bool operator!=(const SavingAccount& rhs) { return !operator==(rhs); }
  81.     bool operator< (const SavingAccount& rhs) { return savedMoney < rhs.savedMoney; }
  82.     bool operator> (const SavingAccount& rhs) { return operator< (rhs); }
  83.     bool operator<=(const SavingAccount& rhs) { return !operator> (rhs); }
  84.     bool operator>=(const SavingAccount& rhs) { return !operator< (rhs); }
  85.  
  86. };
  87.  
  88. class RegularAccount : public IAccount
  89. {
  90. private:
  91.     std::string name;
  92.     int ID;
  93.     double savedMoney;
  94.  
  95.  
  96.  
  97. public:
  98.     RegularAccount(const std::string& name, int ID, int money = 0)
  99.         : IAccount()
  100.         , name(name)
  101.         , ID(ID)
  102.         , savedMoney(money)
  103.     {
  104.     }
  105.  
  106.     void deposit(double money) override {
  107.         savedMoney += money;
  108.         if (money >= 2000) {
  109.             savedMoney += 50;
  110.         }
  111.     }
  112.     void withdraw(double money) override {
  113.         int remaining = savedMoney - money;
  114.         if (remaining > 0) {
  115.             savedMoney = remaining;
  116.         }
  117.     }
  118.     void printInfo() const override {
  119.         std::stringstream ss;
  120.         ss << "Regular account " << name << " " << ID << " " << savedMoney;
  121.         std::cout << ss.str() << std::endl;
  122.     }
  123.     int Id() const override { return ID; }
  124.     int Id() override { return ID; }
  125.  
  126.     bool operator==(const RegularAccount& rhs) { return ID == rhs.ID; }
  127.     bool operator!=(const RegularAccount& rhs) { return !operator==(rhs); }
  128.     bool operator< (const RegularAccount& rhs) { return savedMoney < rhs.savedMoney; }
  129.     bool operator> (const RegularAccount& rhs) { return operator< (rhs); }
  130.     bool operator<=(const RegularAccount& rhs) { return !operator> (rhs); }
  131.     bool operator>=(const RegularAccount& rhs) { return !operator< (rhs); }
  132. };
  133.  
  134. class StudentAccount : public IAccount
  135. {
  136. private:
  137.     std::string name;
  138.     int ID;
  139.     double savedMoney;
  140.     bool bonusApplied = false;
  141.  
  142.  
  143. public:
  144.     StudentAccount(const std::string& name, int ID, int money = 50)
  145.         : IAccount()
  146.         , name(name)
  147.         , ID(ID)
  148.         , savedMoney(money)
  149.     {
  150.     }
  151.  
  152.     void deposit(double money) override {
  153.         savedMoney += money;
  154.         if (savedMoney >= 1000 && !bonusApplied)
  155.         {
  156.             bonusApplied = true;
  157.             savedMoney += 50;
  158.         }
  159.     }
  160.     void withdraw(double money) override {
  161.         int remaining = savedMoney - money;
  162.         if (remaining > 0) {
  163.             savedMoney = remaining;
  164.         }
  165.     }
  166.     void printInfo() const override {
  167.         std::stringstream ss;
  168.         ss << "Student account " << name << " " << ID << " " << savedMoney;
  169.         std::cout << ss.str() << std::endl;
  170.     }
  171.     int Id() const override { return ID; }
  172.     int Id() override { return ID; }
  173.  
  174.     bool operator==(const StudentAccount& rhs) { return ID == rhs.ID; }
  175.     bool operator!=(const StudentAccount& rhs) { return !operator==(rhs); }
  176.     bool operator< (const StudentAccount& rhs) { return savedMoney < rhs.savedMoney; }
  177.     bool operator> (const StudentAccount& rhs) { return operator< (rhs); }
  178.     bool operator<=(const StudentAccount& rhs) { return !operator> (rhs); }
  179.     bool operator>=(const StudentAccount& rhs) { return !operator< (rhs); }
  180. };
  181.  
  182. struct AccountNotImplemented : public std::exception
  183. {
  184.     const char* what() const throw ()
  185.     {
  186.         return "This type of account is not implemented";
  187.     }
  188. };
  189.  
  190. //h
  191. class AccountFactory
  192. {
  193. public:
  194.     AccountFactory() {}
  195.     std::unique_ptr< IAccount > createAccount(AccountType accountType, std::string name, int Id, int money, double interest = 0.15f) const {        switch (accountType) {
  196.         case AccountType::SavingAccount:
  197.         {
  198.             return std::make_unique<SavingAccount>(name, Id, interest, money);
  199.         }
  200.         case AccountType::RegularAccount:
  201.         {
  202.             return std::make_unique<RegularAccount>(name, Id, money);
  203.         }
  204.         case AccountType::StudentAccount:
  205.         {
  206.             return std::make_unique<StudentAccount>(name, Id, money);
  207.         }
  208.         default:
  209.             throw new AccountNotImplemented();
  210.         }
  211.     }
  212. };
  213.  
  214.  
  215.  
  216. class AccountManager {
  217. private:
  218.     std::ifstream input;
  219.     std::vector <std::unique_ptr<IAccount>> accounts;
  220.  
  221. public:
  222.     AccountManager(std::string fileName) {
  223.         input.open(fileName);
  224.         accounts = readAccounts();
  225.     }
  226.     std::vector<std::unique_ptr<IAccount>> readAccounts() {
  227.         std::string accountType;
  228.         int Id;
  229.         std::string firstName;
  230.         std::string lastName;
  231.         int money;
  232.         double interest;
  233.  
  234.         AccountFactory factory;
  235.         std::vector < std::unique_ptr< IAccount >> accounts;
  236.         while (input >> accountType >> Id >> firstName >> lastName >> money) {
  237.             interest = 0;
  238.             if (accountType == "SA") {
  239.                 input >> interest;
  240.                 accounts.emplace_back(factory.createAccount(AccountType::SavingAccount, firstName + " " + lastName, Id, money, interest));
  241.             }
  242.             if (accountType == "ST") {
  243.                 accounts.emplace_back(factory.createAccount(AccountType::StudentAccount, firstName + " " + lastName, Id, money));
  244.             }
  245.             if (accountType == "RG") {
  246.                 accounts.emplace_back(factory.createAccount(AccountType::RegularAccount, firstName + " " + lastName, Id, money));
  247.             }
  248.         }
  249.         return accounts;
  250.     }
  251.  
  252.  
  253.     IAccount* find(int Id) {
  254.         auto value = std::find_if(accounts.begin(), accounts.end(), [Id](std::unique_ptr<IAccount>& account) { return Id == account->Id(); });
  255.         if (value == accounts.end()) {
  256.             return nullptr;
  257.         }
  258.         return value->get();
  259.     }
  260.  
  261.     void runProgram() {
  262.         int Id;
  263.         char operation;
  264.         double money;
  265.         std::cout << "Id: ";
  266.         std::cin >> Id;
  267.         IAccount* account = find(Id);
  268.         if (account == nullptr) { std::cout << "Account does not exist" << std::endl; return; }
  269.         while (true) {
  270.             std::cout << "Operations: i - getInfo, d - deposit, w - withdraw" << std::endl;
  271.             std::cin >> operation;
  272.             switch (operation) {
  273.             case 'i':
  274.                 account->printInfo();
  275.                 break;
  276.             case 'd':
  277.                 std::cout << "How much: ";
  278.                 std::cin >> money;
  279.                 account->deposit(money);
  280.                 break;
  281.             case 'w':
  282.                 std::cout << "How much: ";
  283.                 std::cin >> money;
  284.                 account->withdraw(money);
  285.                 break;
  286.             default:
  287.                 std::cout << "Operation not supported";
  288.             }
  289.         }
  290.     }
  291. };
  292.  
  293. int main() {
  294.     AccountManager manager("Accounts.txt");
  295.     manager.runProgram();
  296.     return 0;
  297. }
  298.  
  299.  
  300.  
  301.  
  302.  
  303. =============
  304.  
  305.  
  306. SA 1 Marek Jankovic 2339 4
  307. ST 2 Maria Cierna 435
  308. RG 3 Martin Polak 626
  309. SA 4 Martina Datolova 27953 6
  310. RG 5 Ivan Richret 217
  311. ST 6 Stano Polak 4241
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement