使用不同类中的类对象获取不正确的数据

时间:2013-02-18 20:49:26

标签: c++

嘿伙计们我在这里用两个班写贷款计划。我在My LoanOfficer类中的“MortCalc”类中声明了一个对象。信贷员级别用于确定用户是否有资格获得贷款,用户输入贷款本金,月收入和月度开支。然后贷款官员进行计算,并在使用一个规则批准贷款时向用户报告。规则如下:如果每月支付的贷款(从我的MortCalc类获得)和每月支出总额大于月收入的50%那么贷款就不会被批准。我遇到的问题是计算这个问题。我尝试将计算存储在“规则”变量中,但它总是等于100,因此永远不会批准贷款!显然我在这里做错了但我无法弄清楚是什么。这是我的代码:

LoanOfficer.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();

LoanOfficer.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++;
        }

的main.cpp

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;

}

1 个答案:

答案 0 :(得分:0)

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

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

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

bool LoanOfficer::isApproved()
    {
    if(rule>50)
    {
        return false;
    }
    return true;