void setup() {
size(450, 270);
background(0);
float loanAmount = 5000;
float interestPercent = 12;
int loanTerm = 12;
loan(loanAmount, interestPercent, loanTerm);
}
void loan(float loanAmount, float interestPercent, int loanTerm) {
float interestRate = interestPercent/100;
float effectiveInterest = interestRate/12;
float monthlyPayment = loanAmount*(effectiveInterest/(1-pow(1+effectiveInterest, -loanTerm)));
head(30, 20);
float interest = 0;
float totalInterest = 0;
float principal = 0;
float balance = loanAmount;
int term = 1;
while (term <= loanTerm) {
interest = balance * effectiveInterest;
principal = monthlyPayment-interest;
totalInterest += interest;
balance -= principal;
if (balance <= 0) {
balance = 0;
}
showValue(term, balance, principal, interest, totalInterest, 30, 20*(1+term));
term++;
}
}
void showValue(int term, float balance, float principal, float interest, float totalInterest, int x, int y) {
int space = 80;
fill(#FFFF00);
text(term, x, y);
text("$"+nf(principal, 1, 2), x+space, y);
text("$"+nf(interest, 1, 2), x+space*2, y);
text("$"+nf(balance, 1, 2), x+space*3, y); text("$"+nf(totalInterest, 1, 2), x+space*4, y);
}
void head(int x, int y) {
int space = 80;
fill(#FF0000);
text("Payment No.", x, y);
text("Principal", x+space, y);
text("Interest", x+space*2, y);
text("Balance", x+space*3, y); text("Total Interest", x+space*4, y);
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น