Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <utility> // for std::pair, std::make_pair
- #include <string>
- std::pair<int, int> handleString(std::string given) {
- int zl{}, gr{};
- size_t found = given.find('.');
- if(found == std::string::npos) {
- zl = std::stoi(given);
- }
- else {
- std::string s_zl = "";
- for(int i = 0; i < found; ++i) {
- s_zl += given[i];
- }
- zl = std::stoi(s_zl);
- std::string s_gr = "";
- for(int i = found + 1; i < given.length(); ++i) {
- s_gr += given[i];
- }
- gr = std::stoi(s_gr);
- }
- return std::make_pair(zl ,gr);
- }
- class Bank {
- private:
- std::pair<int, int> balance{};
- public:
- void deposit(std::string money) {
- std::pair<int, int> adds = handleString(money);
- // Test for gr >= 100
- balance.first += adds.first;
- balance.second += adds.second;
- if(balance.second >= 100) {
- balance.second -= 100;
- balance.first += 1;
- }
- std::cout << "You've succesfuly deposit " << adds.first << " zl";
- std::cout << " and " << adds.second << " gr\n";
- }
- void printBalance() {
- std::cout << "Your current balance is " << balance.first << " zl and ";
- std::cout << balance.second << " gr\n";
- }
- std::pair<int,int> withdrawBank(std::string money_to_withdraw) {
- std::pair<int, int> out = handleString(money_to_withdraw);
- bool can_withdraw = true;
- if(out.first >= balance.first or out.second >= balance.second) {
- can_withdraw = false;
- }
- if(can_withdraw) {
- std::cout << "Withdrawing " << out.first << " zl ";
- std::cout << "and " << out.second << " gr\n";
- balance.first -= out.first;
- balance.second -= out.second;
- return out;
- }
- else {
- std::cout << "You can't withdraw " << out.first << " zl ";
- std::cout << "and " << out.second << " gr\n";
- return std::make_pair<int, int>(0, 0);
- }
- }
- int withdrawATM(std::string money_to_withdraw) {
- std::pair<int, int> out = handleString(money_to_withdraw);
- bool can_withdraw = true;
- if(out.first >= balance.first or out.second >= balance.second) {
- can_withdraw = false;
- }
- if(can_withdraw) {
- std::cout << "Withdrawing " << out.first << " zl ";
- std::cout << "and " << out.second << " gr\n";
- balance.first -= out.first;
- balance.second -= out.second;
- return out;
- }
- else {
- std::cout << "You can't withdraw " << out.first << " zl ";
- std::cout << "and " << out.second << " gr\n";
- return std::make_pair<int, int>(0, 0);
- }
- }
- Bank(std::string start_money) {
- std::cout << "Starting balance created with ";
- balance = handleString(start_money);
- std::cout << balance.first << " zl and " << balance.second << " gr\n";
- }
- ~Bank() {
- std::cout << "Destroying bank instance of an object\n";
- }
- };
- int main()
- {
- // std::pair<int, int> x = handleString("123.124");
- // std::cout << x.first << " " << x.second << '\n';
- Bank bank("100");
- bank.deposit("23.45");
- bank.printBalance();
- bank.deposit("25");
- bank.printBalance();
- bank.withdrawBank("120.24");
- bank.printBalance();
- bank.withdrawBank("2");
- bank.printBalance();
- bank.withdrawBank("120");
- return 0;
- }
Add Comment
Please, Sign In to add comment