brass.cpp 3.1 KB

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