Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. //Name: Gary Hesselbrock
  2. //Date: 01-31-2016
  3. //Assignment: HW#2 Mortgage Calculator
  4. //Description: This program gets loan, rate, and years from the user and then outputs the monthly and total due.
  5. //Version 3.0
  6.  
  7. #include <iostream>
  8. #include <cmath>
  9. #include <iomanip>
  10. #include <Windows.h>
  11. using namespace std;
  12.  
  13. class MortgagePayment
  14. {
  15. private:
  16. double loan, rate, years, term, InterestPaid, PrincipalPaid, balance;
  17.  
  18. public:
  19. //default constructor
  20. MortgagePayment()
  21. {
  22. loan = 0; rate = 0; years = 0; term = 0;
  23. }
  24.  
  25. //set and get functions
  26. void setLoan(double l) { loan = l; }
  27. void setRate(double r) { rate = r; }
  28. void setYears(double y) { years = y; }
  29. void setTerm(double t) { term = t; }
  30. double getMonthlyDue();
  31. double getTotalDue();
  32. double getInterestPaid();
  33. double getPrincipalPaid();
  34. double getBalance();
  35. };
  36.  
  37. double MortgagePayment::getMonthlyDue()
  38. {
  39. //monthly amount due is calculated
  40. return ((loan * (rate / 12) * term) / (term - 1));
  41. }
  42.  
  43. double MortgagePayment::getTotalDue()
  44. {
  45. //total amount due is calculated
  46. return (getMonthlyDue() * 12 * years);
  47. }
  48.  
  49. double MortgagePayment::getInterestPaid()
  50. {
  51. //interest paid is calculated
  52. return (balance * ((rate * 100) / 1200));
  53. }
  54.  
  55. double MortgagePayment::getPrincipalPaid()
  56. {
  57. //principal paid is calculated
  58. return (getMonthlyDue() - getInterestPaid());
  59. }
  60.  
  61. double MortgagePayment::getBalance()
  62. {
  63. //balance is calculated
  64. return (balance - getPrincipalPaid());
  65. }
  66.  
  67. int main()
  68. {
  69. MortgagePayment mortgage;
  70. double loan, rate, years, term, balance;
  71. int monthCounter = 1;
  72.  
  73. //the loan amount is taken from the user
  74. cout << "Please enter the loan amount: ";
  75. while (!(cin >> loan) || loan < 0)
  76. {
  77. cout << "The loan amount cannot be negative. Please enter a positive value: ";
  78. }
  79. mortgage.setLoan(loan);
  80.  
  81. //the interest rate is taken from the user
  82. cout << "Please enter the interest rate: ";
  83. while (!(cin >> rate) || rate < 0)
  84. {
  85. cout << "The interest rate cannot be negative. Please enter a positive value: ";
  86. }
  87. //input rate value is converted
  88. rate = rate / 100;
  89. mortgage.setRate(rate);
  90.  
  91. //the year amount is taken from the user
  92. cout << "Please enter the year amount: ";
  93. while (!(cin >> years) || years < 0)
  94. {
  95. cout << "The year amount cannot be negative. Please enter a positive value: ";
  96. }
  97. mortgage.setYears(years);
  98.  
  99. //term is calculated
  100. term = pow((1 + (rate / 12)), (12 * years));
  101. mortgage.setTerm(term);
  102.  
  103.  
  104. //output calculations
  105. cout << endl << "The monthly payment amount is $" << fixed << setprecision(2) << mortgage.getMonthlyDue() << endl;
  106. cout << "The total payment amount is $" << mortgage.getTotalDue() << endl;
  107.  
  108. balance = loan;
  109.  
  110. for (double i = 12 * years; i > 0; --i)
  111. {
  112. double x = mortgage.getInterestPaid();
  113. double y = mortgage.getPrincipalPaid();
  114. double z = mortgage.getBalance();
  115. cout << monthCounter++ << setw(10) << "$" << x << setw(10) << "$" << y << setw(10) << "$" << z << endl;
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement