0

嘿伙计们,我在这里使用两个类编写了一个贷款程序。我从我的 LoanOfficer 类中的“MortCalc”类中声明了一个对象。信贷员类是确定用户是否有资格获得贷款,用户输入贷款本金、月收入和月支出。然后,贷款官员类会进行计算并向用户报告贷款是否使用一条规则获得批准。规则是这样的:如果贷款的每月还款额(从我的 MortCalc 课程中获得)和每月费用总额大于月收入的 50%,则贷款不获批准。我遇到的问题是计算这个。我尝试将计算存储在“规则”变量中,但它始终等于 100,因此永远不会批准贷款!显然我在这里做错了什么,但我不知道是什么。

贷款官.h

class LoanOfficer
{
//private class variables
private:
MortCalc mc;
double intRate;
double monthlyIncome;
double term;
double monExpenses, principal;
double rule;
bool bLoanApprove, bOpen;
string userName, fileName,lenderName;
string loanOfficer, Welcome;
int counter;
void calculate();

贷款官.cpp

LoanOfficer::LoanOfficer()
{
//initializing variables;
intRate = 4.1;
term = 30;
counter=1;
principal=0;
lenderName="John's Bank";
Welcome ="";
calculate();
}
void LoanOfficer::calculate()
{

rule = ((mc.GetMonPymt() + monExpenses) / monthlyIncome)* 100;
    //i have a getter in my Mortcalc class which get's the monthly Payment.

}
bool LoanOfficer::isApproved()
{
if(rule>50)
{
    bLoanApprove = true;
}
else{
    bLoanApprove = false;
}
return bLoanApprove;
}
string LoanOfficer::getApproval()
 {
if(bLoanApprove==true)
{
  stringstream ss;
        ss<<"\n\nLoan Approval Status: Yes"
        <<"\nLoan amount: "
        <<principal
        <<"\nInterest Rate: "
        <<intRate
        <<"\nMonthly Payment: "
        <<monPayment
        <<"\nTotalLoan: "
        <<mc.GetTotalLoan()
        <<"\nTotal Interest"
        <<mc.GetTotalInt()
        <<"\n\nCongratulations We're looking to do business with you "
        <<userName<<"!";
    loanOfficer = ss.str();
}
else
{
    stringstream ss;
    ss<<"\n\nLoan Approval Status: No"
       <<"\n\n  Income vs Montly Payment and expenses does not meet "
       <<"\n  the 50% criteria net income that is necessary for this"
       <<"\n  institution to approve this loan";
    loanOfficer = ss.str();
}

return loanOfficer;
}
void LoanOfficer::setPrincipal(double p)
{
mc.setPrin(p);
principal = p;
}
bool LoanOfficer::isOpen()
{
return bOpen;
}
void LoanOfficer::setMonInc(double mi)
{
monthlyIncome = mi;
}
void LoanOfficer::setExpenses(double ex)
{
monExpenses = ex;
}

void LoanOfficer::setAppName(string n)
{
userName = n;
 }
string LoanOfficer::getFilename()
{
return fileName;
}
string LoanOfficer::getIntro()
{
stringstream ss;
    ss<<"Hi Welcome to " <<lenderName
    <<"\n Please enter your information below to see if you're approved for a loan."
    <<"\nWe have a fixed interest rate of 4.1 and term of loan is 30 years."
            <<"\nThe way we determine our loan approvals is by adding 
                loan payment and monthly expenses,"
            <<"\nand that is greater than 50% of your monthly income the loan 
                is notapproved.\n\n";
    Welcome = ss.str();
return Welcome;
}

void LoanOfficer::writeStatus()
{

stringstream ss;

    ss<<userName<<"_"<<counter<<".txt";
    fileName = ss.str();

    ofstream receiptOut;
    receiptOut.open(fileName.c_str());

//Writing report setting precision to 2 decimal places
//returning true if able to write receipt.
    receiptOut<<" CUSTOMER LOAN INFORMATION " 
        <<month+1<<"/"<<day<<"/"<<year+1900<<"\n\n"
        <<"********************************"
        <<"\n Your Loan Information: "
        << "\n\n Principal: "<<"$" << fixed << setprecision (2) 
        << principal
        << "\n\n Interest rate: "<< fixed << setprecision (2) 
        << intRate << "%"
        << "\n\n Monthly Payment: "<<"$" << fixed << setprecision (2) 
        << mc.GetMonPymt()
                    //here it obtains the correct monthly payment. I've checked through
                    //debugging.

        << "\n\n Total Interest paid: " <<"$"<< fixed << setprecision (2) 
        << mc.GetTotalInt()
        <<"\n\n Total Cost of the Loan: "<<"$" << fixed << setprecision (2) 
        << mc.GetTotalLoan()
        <<"\n*********************************"
        <<"\n\n\nThank You for using my calculator. Have a Nice Day."
        <<"\n****************************************************";
        receiptOut.close();
        counter++;
        }

主文件

double principal,monthlyIncome,monthlyExpenses;
string name, answer;
string fAnswer
//class object
LoanOfficer lo;

cout<<lo.getIntro();

cout<<"Please enter your name: ";
cin>>name;

//passing name to setName class method.
lo.setAppName(name);


//start of do loop
do
{
    //presenting the user a menu accessed from otherFunctions.cpp
    //checking which choice user entered with switch statements
      cout<<"\nPlease enter the amount you want to borrow: ";
       cin>>principal;
       cin.ignore();
       lo.setPrincipal(principal);
    cout<<"\nPlease enter your monthly income after taxes: ";
        cin>>monthlyIncome;
        cin.ignore();
        lo.setMonInc(monthlyIncome);
    cout<<"\nPlease enter your monthly expenses: ";
        cin>>monthlyExpenses;
        cin.ignore();
        lo.setExpenses(monthlyExpenses);
  cout<<lo.getApproval();

cout<<"\n\n Would you like to write a file? Enter y for yes and n for no\n";
        cin>>fAnswer;
        if(fAnswer =="y")
        {
            lo.writeStatus();
            cout<<"\n\nReport is located in: "
                <<lo.getFilename();
        }
        else
        {
            cout<<"\n\nNo report printed out.";
        }
    //ask if user would like to do another
cout<<"\n\nWould you like to do another loan? Enter y for yes and n for no\n";
    cin>>answer;
    cout<<"\n";
}while(answer =="y");
//end do/while

//Goodbye message
    {
    cout <<"\n Thanks for calculating. Goodbye!\n\n"; //when loop is done
    }

return 0;

}
4

1 回答 1

0

改变业务逻辑!
的想法与你的认识相矛盾
让我们仔细看看你的问题中的这个陈述:“规则是这样的:如果贷款的每月支付(从我的 MortCalc 课程中获得)和每月支出总额大于每月收入的 50%,那么贷款未获批准。”并在您的代码中找到实现此业务逻辑的位置,这里是:

bool LoanOfficer::isApproved()
{
if(rule>50)
{
    bLoanApprove = true;
}
else{
    bLoanApprove = false;
}

从代码提取中可以很容易地发现它与您的业务逻辑相矛盾。
解决方案:

bool LoanOfficer::isApproved()
    {
    if(rule>50)
    {
        return false;
    }
    return true;
于 2013-02-18T21:33:42.853 回答