Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Name: Gary Hesselbrock
- //Date: 01-31-2016
- //Assignment: HW#2 Mortgage Calculator
- //Description: This program gets loan, rate, and years from the user and then outputs the monthly and total due.
- //Version 3.0
- #include <iostream>
- #include <cmath>
- #include <iomanip>
- #include <Windows.h>
- using namespace std;
- class MortgagePayment
- {
- private:
- double loan, rate, years, term, InterestPaid, PrincipalPaid, balance;
- public:
- //default constructor
- MortgagePayment()
- {
- loan = 0; rate = 0; years = 0; term = 0;
- }
- //set and get functions
- void setLoan(double l) { loan = l; }
- void setRate(double r) { rate = r; }
- void setYears(double y) { years = y; }
- void setTerm(double t) { term = t; }
- double getMonthlyDue();
- double getTotalDue();
- double getInterestPaid();
- double getPrincipalPaid();
- double getBalance();
- };
- double MortgagePayment::getMonthlyDue()
- {
- //monthly amount due is calculated
- return ((loan * (rate / 12) * term) / (term - 1));
- }
- double MortgagePayment::getTotalDue()
- {
- //total amount due is calculated
- return (getMonthlyDue() * 12 * years);
- }
- double MortgagePayment::getInterestPaid()
- {
- //interest paid is calculated
- return (balance * ((rate * 100) / 1200));
- }
- double MortgagePayment::getPrincipalPaid()
- {
- //principal paid is calculated
- return (getMonthlyDue() - getInterestPaid());
- }
- double MortgagePayment::getBalance()
- {
- //balance is calculated
- return (balance - getPrincipalPaid());
- }
- int main()
- {
- MortgagePayment mortgage;
- double loan, rate, years, term, balance;
- int monthCounter = 1;
- //the loan amount is taken from the user
- cout << "Please enter the loan amount: ";
- while (!(cin >> loan) || loan < 0)
- {
- cout << "The loan amount cannot be negative. Please enter a positive value: ";
- }
- mortgage.setLoan(loan);
- //the interest rate is taken from the user
- cout << "Please enter the interest rate: ";
- while (!(cin >> rate) || rate < 0)
- {
- cout << "The interest rate cannot be negative. Please enter a positive value: ";
- }
- //input rate value is converted
- rate = rate / 100;
- mortgage.setRate(rate);
- //the year amount is taken from the user
- cout << "Please enter the year amount: ";
- while (!(cin >> years) || years < 0)
- {
- cout << "The year amount cannot be negative. Please enter a positive value: ";
- }
- mortgage.setYears(years);
- //term is calculated
- term = pow((1 + (rate / 12)), (12 * years));
- mortgage.setTerm(term);
- //output calculations
- cout << endl << "The monthly payment amount is $" << fixed << setprecision(2) << mortgage.getMonthlyDue() << endl;
- cout << "The total payment amount is $" << mortgage.getTotalDue() << endl;
- balance = loan;
- for (double i = 12 * years; i > 0; --i)
- {
- double x = mortgage.getInterestPaid();
- double y = mortgage.getPrincipalPaid();
- double z = mortgage.getBalance();
- cout << monthCounter++ << setw(10) << "$" << x << setw(10) << "$" << y << setw(10) << "$" << z << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement