1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
// So, I can not understand or figure out how to get the payment amount like a
// decimal form. (EX: 752.62)
// Also, I can not figure out how to not allow letters to be inputted.
//Loan Payment Calculator
#include <chrono>
#include <cmath>
#include <exception>
#include <iomanip>
#include <iostream>
#include <random>
enum Credit { Excellent, Good, Fair, Bad };
std::mt19937& getRndEngine();
double getRndDouble( double min = std::uniform_real_distribution<>().min(),
double max = std::uniform_real_distribution<>().max() );
double askForNum( double min, double max, const std::string& );
int askForNum( int min, int max, const std::string& );
int askForLoanTerms();
Credit reckonCustomerCredit( int credit_score );
double reckonRndInterestRate( Credit par );
double calcPayment(double p, double interest_rate, int loan_term);
int main()
{
//Checking if entry is valid for principal
double principal_amount { askForNum( 0.0,
std::numeric_limits<double>::max(),
"Please enter the principal amount:" ) };
//Inputting creditscore and making sure it's a valid amount.
int credit_score { askForNum( 0, 850, "Please enter credit score:" ) };
//Error occurs if user types in credit score lower than 300
if (credit_score < 300) {
std::cout << "Sorry loan amount is not offered at this low of credit rate.\n";
return 0;
}
//Random interest rate based on the enums
double interest_rate {
reckonRndInterestRate( reckonCustomerCredit(credit_score) )
};
int loan_term { askForLoanTerms() };
//Calling functions used to find the monthly payment for the loan.
double monthly_payment {
calcPayment(principal_amount, interest_rate, loan_term)
};
std::cout << "\nYour monthly payments would come out to be: "
<< std::fixed << std::setprecision(2) << monthly_payment
<< " on your loan\n";
}
std::mt19937& getRndEngine()
{
static std::mt19937 eng {
static_cast<unsigned>(
std::chrono::high_resolution_clock::now().time_since_epoch().count()
)
};
return eng;
}
double getRndDouble( double min, double max )
{
std::uniform_real_distribution<> dst( min, max );
return dst( getRndEngine() );
}
double calcPayment(double p, double interest_rate, int loan_term)
{
double r { (interest_rate / 12) / 100 };
int n { loan_term * 12 };
// \/ what is the point of this '+' ?
double x { std::pow( +r, n ) };
std::cout << std::fixed << std::setprecision(4)
<< "r: " << r << "; n: " << n << "; x: " << x
<< "\n(p * r * x) / (x - 1): " << (p * r * x) / (x - 1) << '\n';
return (p * r * x) / (x - 1);
}
double askForNum( double min, double max, const std::string& str )
{
double ret_val;
std::cout << str << "\n>>> ";
for (std::string s; std::cin >> s; /**/) {
// or ... std::getline( std::cin, s) ...
try {
ret_val = std::stod( s );
} catch (const std::exception& e) {
std::cout << "Unknown value '" << s << "'. Please retry.\n"
<< str << "\n>>> ";
continue;
}
if (ret_val < min) {
std::cout << ret_val << " is below the minimum accepted value "
<< min << ".\nPlease retry.\n" << str << "\n>>> ";
continue;
}
if (max < ret_val) {
std::cout << ret_val << " is over the maximum accepted value "
<< max << ".\nPlease retry.\n" << str << "\n>>> ";
continue;
}
break;
}
std::cout << "Accepted value '" << ret_val << "'.\n"; // debug
return ret_val;
}
int askForNum( int min, int max, const std::string& str)
{
int ret_val;
std::cout << str << "\n>>> ";
for (std::string s; std::cin >> s; /**/) {
// or ... std::getline( std::cin, s) ...
try {
ret_val = std::stoi( s );
} catch (const std::exception& e) {
std::cout << "Unknown value '" << s << "'. Please retry.\n"
<< str << "\n>>> ";
continue;
}
if (ret_val < min) {
std::cout << ret_val << " is below the minimum accepted value "
<< min << ".\nPlease retry.\n" << str << "\n>>> ";
continue;
}
if (max < ret_val) {
std::cout << ret_val << " is over the maximum accepted value "
<< max << ".\nPlease retry.\n" << str << "\n>>> ";
continue;
}
break;
}
std::cout << "Accepted value '" << ret_val << "'.\n"; // debug
return ret_val;
}
int askForLoanTerms()
{
switch (askForNum( 1, 3, "Please enter the term of loan.\n"
"1) 10 years\n2) 15 years\n3) 30 years" ))
{
case 1: return 10;
case 2: return 15;
case 3: return 30;
}
return -1; // just to avoid compiler warnings
}
Credit reckonCustomerCredit( int credit_score )
{
// \/ check this value
if (credit_score < 689) { return Credit::Bad; }
if (credit_score <= 689) { return Credit::Fair; }
if (credit_score <= 719) { return Credit::Good; }
return Credit::Excellent;
}
double reckonRndInterestRate( Credit par )
{
switch ( par ) {
case Credit::Excellent: return getRndDouble( 2.75, 4 );
case Credit::Good: return getRndDouble( 4.01, 6.50 );
case Credit::Fair: return getRndDouble( 6.51, 8.75 );
case Credit::Bad: return getRndDouble( 8.76, 10.50 );
}
return 0.0; // just to avoid compiler warnings
}
|