Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. class Loan
  6. {
  7. private:
  8. double total(int i);
  9. double principal;
  10. double interestRate;
  11. double Balance(int i);
  12. double Instalment ();
  13. double Interest (int i);
  14. int numPayments;
  15.  
  16. public:
  17. Loan (double amount, double interestRate, int numPayments);
  18.  
  19. void displaySchedule(double amount, double interestRate, int numPayments);
  20.  
  21. };
  22.  
  23. void readData(double& principal, double& interestRate, int& numPayments)
  24. {
  25.  
  26. cout <<"Principal: ";
  27. cin >>principal;
  28.  
  29. cout <<"Interest rate: ";
  30. cin >>interestRate;
  31.  
  32. cout <<"Number of payments: ";
  33. cin >>numPayments;
  34.  
  35. cout <<endl;
  36.  
  37. }
  38.  
  39. Loan::Loan(double p, double ir, int np)
  40. {
  41. principal = p;
  42. interestRate= ir;
  43. numPayments= np;
  44. }
  45.  
  46. double Loan::Instalment()
  47. {
  48. return principal / numPayments;
  49. }
  50.  
  51. double Loan::total(int i)
  52. {
  53. return Instalment() + Interest(i);
  54. }
  55.  
  56. double Loan::Balance(int i)
  57. {
  58. return principal - (i-1) * Instalment();
  59.  
  60. }
  61.  
  62. double Loan::Interest(int i)
  63. {
  64. return Balance(i) * (interestRate/12) / 100;
  65.  
  66. }
  67.  
  68. void Loan::displaySchedule(double amount, double interestRate, int numPayments)
  69. {
  70. cout.setf(ios::fixed);
  71. cout.precision(2);
  72. cout.setf(ios::showpoint);
  73.  
  74. cout << "No."<<setw(15)<<"Balance"<<setw(15)<<"Instalment"<<setw(15)<<"Interest"<<setw(15)<<"Total payment" << endl;
  75. cout << "----------------------------------------------------------------"<<endl;
  76.  
  77.  
  78. for (int i=1; i<=numPayments; i++)
  79. {
  80. cout <<i<<setw(15)<< Balance(i)<<setw(15)<<Instalment()<<setw(15)<<Interest(i)<<setw(15)<<total(i)<<endl;
  81. }
  82.  
  83.  
  84. cout <<"----------------------------------------------------------------"<<endl;
  85.  
  86.  
  87.  
  88. double total_instalment;
  89. double total_interest;
  90.  
  91.  
  92. for (int i=1; i <=numPayments; i++)
  93. {
  94.  
  95. total_instalment += Instalment();
  96.  
  97. total_interest += Interest(i);
  98. }
  99. cout <<setw(33q)<<total_instalment <<setw(15)<< total_interest;
  100. }
  101.  
  102. int main()
  103. {
  104. double principle, interestRate;
  105. int numPayments;
  106.  
  107. readData(principle, interestRate, numPayments);
  108. Loan myLoan(principle, interestRate, numPayments);
  109. myLoan.displaySchedule(principle, interestRate, numPayments);
  110.  
  111. return 0;
  112. }
  113. /*
  114. afborgun = höfuðstóll / fjöldi afborgana
  115. instalment = principal / number of payments
  116.  
  117. vaxtaupphæð = eftirstöðvar höfuðstóls * (vaxtaprósenta/12) / 100
  118. interest = remainder of principal * (interest rate/12) / 100
  119.  
  120. eftirstöðvar höfuðstóls við greiðslu nr. x = höfuðstóll - (x-1)*afborgun
  121. balance = principal - (x-1) * instalment */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement