juinda

ATM.cpp

Jun 14th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Account
  6. {
  7. public:
  8. Account(double initialDeposit = 0) :balance(initialDeposit) {}
  9. double getBalance()const { return balance; }
  10. double deposit(double amount);
  11. double withdraw(double amount);
  12. private:
  13. double balance;
  14. };
  15. class NegativeDeposit
  16. {
  17.  
  18. };
  19. class InsufficientFunds
  20. {
  21.  
  22. };
  23. double Account::deposit(double amount)
  24. {
  25. if (amount > 0)
  26. balance += amount;
  27. else
  28. throw NegativeDeposit();
  29. return balance;
  30. }
  31. double Account::withdraw(double amount)
  32. {
  33. if ((amount > balance) || (amount < 0))
  34. throw InsufficientFunds();
  35. else
  36. balance -= amount;
  37. return balance;
  38.  
  39. }
  40.  
  41. int main()
  42. {
  43. Account a(100);
  44. try
  45. {
  46. cout << "Depositing 50" << endl;
  47. cout << "New balance: " << a.deposit(50) << endl;
  48. //cout << "Depositing -25" << endl;
  49. //cout << "New balance: " << a.deposit(-25) << endl;
  50. cout << "Withdraw 25" << endl;
  51. cout << "New balance: " << a.withdraw(25) << endl;
  52. cout << "Withdraw 250" << endl;
  53. cout << "New balance: " << a.withdraw(250) << endl;
  54. }
  55. catch (InsufficientFunds) // InsufficientFunds: a class name
  56. {
  57. cout << "Not enough money to withdraw that amount." << endl;
  58. }
  59. catch (NegativeDeposit) // NegativeDeposit: a class name
  60. {
  61. cout << "You may only deposit a positive amount." << endl;
  62. }
  63. cout << "Enter a character to exit" << endl;
  64. char wait;
  65. cin >> wait;
  66. system("pause");
  67. return 0;
  68. }
  69. // note that
  70. // class NegativeDeposit {…};
  71. // class InsufficientFunds {…};
Advertisement
Add Comment
Please, Sign In to add comment