Skip to content
Snippets Groups Projects
Select Git revision
  • e247c0b375ec3e7eeeacecaf1ffc31370e0845b0
  • master default protected
2 results

Bank.hpp

Blame
  • Bank.hpp 1.75 KiB
    #ifndef BANK_HPP
    #define BANK_HPP
    
    #include "Account.hpp"
    #include <stdexcept>
    #include <iostream>
    
    class Bank
    {
      private:
        Account* account_;
        int pin_code_;
    
      public:
        // (*) References / Pointers as a parameters stay valid outside
        //     No copy -> attributes of classes can be modified from outside
        Bank(Account* account, int pin_code) : account_(new Account(*account)), pin_code_(pin_code)
        {
          // Checks in public methods that are called with parameters
          if (pin_code_ < 0x1000 || pin_code_ >= 0x10000)
            throw std::runtime_error("Der Pin-Code ist zu kurz / zu lang!");
        }
        Bank(const Bank&) = delete;
        Bank& operator=(const Bank&) = delete;
        ~Bank() { delete account_; }
    
        bool verifyPinCode() const
        {
          // Very bad using just std::cin
          int pin_code;
          std::cout << "Enter your pin: ";
          std::cin.unsetf(std::ios::dec);
          std::cin.unsetf(std::ios::hex);
          std::cin.unsetf(std::ios::oct);
          std::cin >> pin_code;
          return pin_code == pin_code_;
        }
    
        // (#) Methods that return references / pointers may screw up 
        //     the concept of encapsulation
        Account const* getAccount()
        {
          if (!verifyPinCode())
            throw std::runtime_error("Der Pin-Code ist nicht gültig!");
    
          return account_;
        }
    
        // These two methods are safe, as they do not return a reference / pointer
        int getAccountEuros() const
        {
          if (!verifyPinCode())
            throw std::runtime_error("Der Pin-Code ist nicht gültig!");
    
          return account_->getEuros();
        }
    
        void changeAccountEurosBy(int euros)
        {
          if (!verifyPinCode())
            throw std::runtime_error("Der Pin-Code ist nicht gültig!");
    
          account_->changeEurosBy(euros);
        }
    };
    
    #endif // BANK_HPP