acctabc.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // acctabc.cpp -- bank account class methods
  2. #include <iostream>
  3. #include "acctabc.h"
  4. using std::cout;
  5. using std::ios_base;
  6. using std::endl;
  7. using std::string;
  8. // Abstract Base Class
  9. AcctABC::AcctABC(const string & s, long an, double bal)
  10. {
  11. fullName = s;
  12. acctNum = an;
  13. balance = bal;
  14. }
  15. void AcctABC::Deposit(double amt)
  16. {
  17. if (amt < 0)
  18. cout << "Negative deposit not allowed; "
  19. << "deposit is cancelled.\n";
  20. else
  21. balance += amt;
  22. }
  23. void AcctABC::Withdraw(double amt)
  24. {
  25. balance -= amt;
  26. }
  27. // protected methods for formatting
  28. AcctABC::Formatting AcctABC::SetFormat() const
  29. {
  30. // set up ###.## format
  31. Formatting f;
  32. f.flag =
  33. cout.setf(ios_base::fixed, ios_base::floatfield);
  34. f.pr = cout.precision(2);
  35. return f;
  36. }
  37. void AcctABC::Restore(Formatting & f) const
  38. {
  39. cout.setf(f.flag, ios_base::floatfield);
  40. cout.precision(f.pr);
  41. }
  42. // Brass methods
  43. void Brass::Withdraw(double amt)
  44. {
  45. if (amt < 0)
  46. cout << "Withdrawal amount must be positive; "
  47. << "withdrawal canceled.\n";
  48. else if (amt <= Balance())
  49. AcctABC::Withdraw(amt);
  50. else
  51. cout << "Withdrawal amount of $" << amt
  52. << " exceeds your balance.\n"
  53. << "Withdrawal canceled.\n";
  54. }
  55. void Brass::ViewAcct() const
  56. {
  57. Formatting f = SetFormat();
  58. cout << "Brass Client: " << FullName() << endl;
  59. cout << "Account Number: " << AcctNum() << endl;
  60. cout << "Balance: $" << Balance() << endl;
  61. Restore(f);
  62. }
  63. // BrassPlus Methods
  64. BrassPlus::BrassPlus(const string & s, long an, double bal,
  65. double ml, double r) : AcctABC(s, an, bal)
  66. {
  67. maxLoan = ml;
  68. owesBank = 0.0;
  69. rate = r;
  70. }
  71. BrassPlus::BrassPlus(const Brass & ba, double ml, double r)
  72. : AcctABC(ba) // uses implicit copy constructor
  73. {
  74. maxLoan = ml;
  75. owesBank = 0.0;
  76. rate = r;
  77. }
  78. void BrassPlus::ViewAcct() const
  79. {
  80. Formatting f = SetFormat();
  81. cout << "BrassPlus Client: " << FullName() << endl;
  82. cout << "Account Number: " << AcctNum() << endl;
  83. cout << "Balance: $" << Balance() << endl;
  84. cout << "Maximum loan: $" << maxLoan << endl;
  85. cout << "Owed to bank: $" << owesBank << endl;
  86. cout.precision(3);
  87. cout << "Loan Rate: " << 100 * rate << "%\n";
  88. Restore(f);
  89. }
  90. void BrassPlus::Withdraw(double amt)
  91. {
  92. Formatting f = SetFormat();
  93. double bal = Balance();
  94. if (amt <= bal)
  95. AcctABC::Withdraw(amt);
  96. else if ( amt <= bal + maxLoan - owesBank)
  97. {
  98. double advance = amt - bal;
  99. owesBank += advance * (1.0 + rate);
  100. cout << "Bank advance: $" << advance << endl;
  101. cout << "Finance charge: $" << advance * rate << endl;
  102. Deposit(advance);
  103. AcctABC::Withdraw(amt);
  104. }
  105. else
  106. cout << "Credit limit exceeded. Transaction cancelled.\n";
  107. Restore(f);
  108. }