AdrianMadajewski

wdi bank

Nov 27th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <utility> // for std::pair, std::make_pair
  3. #include <string>
  4.  
  5. std::pair<int, int> handleString(std::string given) {
  6.    
  7.     int zl{}, gr{};
  8.     size_t found = given.find('.');
  9.     if(found == std::string::npos) {
  10.         zl = std::stoi(given);
  11.     }
  12.     else {
  13.         std::string s_zl = "";
  14.         for(int i = 0; i < found; ++i) {
  15.             s_zl += given[i];
  16.         }
  17.         zl = std::stoi(s_zl);
  18.         std::string s_gr = "";
  19.         for(int i = found + 1; i < given.length(); ++i) {
  20.             s_gr += given[i];
  21.         }
  22.         gr = std::stoi(s_gr);
  23.     }
  24.     return std::make_pair(zl ,gr);
  25. }
  26.  
  27. class Bank {
  28. private:
  29.     std::pair<int, int> balance{};
  30. public:
  31.     void deposit(std::string money) {
  32.         std::pair<int, int> adds = handleString(money);
  33.        
  34.         // Test for gr >= 100
  35.         balance.first += adds.first;
  36.         balance.second += adds.second;
  37.         if(balance.second >= 100) {
  38.             balance.second -= 100;
  39.             balance.first += 1;
  40.         }
  41.        
  42.         std::cout << "You've succesfuly deposit " << adds.first << " zl";
  43.         std::cout << " and " << adds.second << " gr\n";
  44.        
  45.     }
  46.    
  47.     void printBalance() {
  48.         std::cout << "Your current balance is " << balance.first << " zl and ";
  49.         std::cout << balance.second << " gr\n";
  50.     }
  51.    
  52.     std::pair<int,int> withdrawBank(std::string money_to_withdraw) {
  53.         std::pair<int, int> out = handleString(money_to_withdraw);
  54.        
  55.         bool can_withdraw = true;
  56.         if(out.first >= balance.first or out.second >= balance.second) {
  57.             can_withdraw = false;
  58.         }
  59.        
  60.         if(can_withdraw) {
  61.             std::cout << "Withdrawing " << out.first << " zl ";
  62.             std::cout << "and " << out.second << " gr\n";
  63.             balance.first -= out.first;
  64.             balance.second -= out.second;
  65.             return out;
  66.         }
  67.         else {
  68.             std::cout << "You can't withdraw " <<  out.first << " zl ";
  69.             std::cout << "and " << out.second << " gr\n";
  70.             return std::make_pair<int, int>(0, 0);
  71.         }
  72.            
  73.            
  74.     }
  75.     int withdrawATM(std::string money_to_withdraw) {
  76.        
  77.         std::pair<int, int> out = handleString(money_to_withdraw);
  78.        
  79.         bool can_withdraw = true;
  80.         if(out.first >= balance.first or out.second >= balance.second) {
  81.             can_withdraw = false;
  82.         }
  83.        
  84.         if(can_withdraw) {
  85.             std::cout << "Withdrawing " << out.first << " zl ";
  86.             std::cout << "and " << out.second << " gr\n";
  87.             balance.first -= out.first;
  88.             balance.second -= out.second;
  89.             return out;
  90.         }
  91.         else {
  92.             std::cout << "You can't withdraw " <<  out.first << " zl ";
  93.             std::cout << "and " << out.second << " gr\n";
  94.             return std::make_pair<int, int>(0, 0);
  95.         }
  96.        
  97.     }
  98.    
  99.     Bank(std::string start_money) {
  100.         std::cout << "Starting balance created with ";
  101.         balance = handleString(start_money);
  102.         std::cout << balance.first << " zl and " << balance.second << " gr\n";
  103.     }
  104.     ~Bank() {
  105.         std::cout << "Destroying bank instance of an object\n";
  106.     }
  107.    
  108. };
  109.  
  110.  
  111. int main()
  112. {
  113.     // std::pair<int, int> x = handleString("123.124");
  114.     // std::cout << x.first << " " << x.second << '\n';
  115.    
  116.     Bank bank("100");
  117.     bank.deposit("23.45");
  118.     bank.printBalance();
  119.     bank.deposit("25");
  120.     bank.printBalance();
  121.     bank.withdrawBank("120.24");
  122.     bank.printBalance();
  123.     bank.withdrawBank("2");
  124.     bank.printBalance();
  125.     bank.withdrawBank("120");
  126.    
  127.     return 0;
  128. }
Add Comment
Please, Sign In to add comment