2013-02-18 48 views
0

嘿傢伙我在這裏用兩個類來編寫貸款程序。我在My LoanOfficer Class中從我的「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

格式化你的代碼正確使得它更易於閱讀。另外,標題中的'string' w/o其pal'std ::'= no-no。即使在使用命名空間標準時也是如此, – ChiefTwoPencils 2013-02-18 20:52:36

+0

? @ C.Lang – 2013-02-18 20:56:47

+2

@JohnAcosta在頭文件中使用''聲明是邪惡的,並有各種奇怪的後果。在包含'algorithm'和'using namespace std;'之後,嘗試聲明一個類'count'。 – pmr 2013-02-18 20:59:40

回答

0

章ange 商業邏輯!
想法與你的實現矛盾。
讓我們仔細看看你在這個聲明中的問題: 「規則是這樣的:如果每月支付的貸款(從我的MortCalc類獲得)和每月支出總額大於月收入的50%然後貸款未獲批准「,並在你的代碼中找到的地方,這個業務邏輯被實現,那就是:

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

從代碼中提取一個可以很容易地找到它違揹你的業務邏輯。
解決方案:

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

對!但是我的規則變量仍然沒有做正確的計算。例如,如果我要爲委託人輸入1000,每月收入爲8000,費用爲200。這筆貸款的每月付款額爲4.83(這是在我的mortcalc類中以4.1固定利率和30年期限完成的),因此它將是4.83 + 200/8000 * 100,這相當於2.560375,所以顯然bool應該返回true,但它不會因爲它不能正確計算.rule會出現100.00000000000000 !!! @spin_eight – 2013-02-18 21:57:22

+0

@JohnAcosta:你有沒有用調試器看過它,看它何時出錯? – ChiefTwoPencils 2013-02-18 22:58:33

+0

@C。郎是我使用調試器。當我輸入這些數字時,所有正確的值都存儲在它們的適當變量中。 mc.GetMonPymt()是:4.8319837110249075;單一費用爲:200.00000000000000;每月收入是:8000.0000000000000,由於某種原因規則值是:100.00000000000000。 – 2013-02-18 23:33:57

相關問題