Reputation: 375
I am almost finished with programming a monthly amortization calculator that is based on the user's principal amount, monthly interest and monthly payment. But there is on last problem that I can't seem to figure out. I seemed to have set the limit at 0 but it still shows the first amount if negative money was a thing. Here is the code for a better understanding of what I mean:
import java.util.Scanner;
public class Amortization {
public static void main(String []args){
Scanner input = new Scanner(System.in);
int month = 1;
int year = 0;
double balance;
double rate;
double payment;
double principal;
double calculated_interest;
double actual_payment;
double principal_amt;
System.out.println("What is your principal amount?"); principal = input.nextDouble(); balance = principal;
System.out.println("What is your monthly interest rate in decimal?"); rate = input.nextDouble();
System.out.println("What is your monthly payment?"); payment = input.nextDouble();
while(balance>0){
if(month == 13){
year++;
month = 1;
}
calculated_interest = ((int)(Math.round(balance*rate*100)))/100.0;
principal_amt = ((int)(Math.round((payment-calculated_interest))*100))/100.0;
actual_payment = ((int)(Math.round((payment-calculated_interest)*100)))/100.0;
System.out.println("Year " + year + ", " + "Month " + month + ":");
System.out.println("Your interest amount is " + "$" + calculated_interest);
System.out.println("Your principal amount " + "$" + principal_amt);
balance = ((int)(Math.round((balance-actual_payment)*100)))/100.0;
System.out.println("Your new balance is " + "$" + balance);
System.out.println();
month++;
}
input.close();
}
}
Upvotes: 0
Views: 1402
Reputation: 5668
There are few problems with your code.
Because you are taking monthly payments from user, user will have to pay less than monthly payment in last month and that's what causing the -ve If you want your payments to be equal every month you'll have to calculate monthly payments yourself.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int month = 1;
int year = 0;
double balance;
double rate;
double payment;
double principal;
double calculated_interest;
double actual_payment;
double principal_amt;
System.out.println("What is your principal amount?");
principal = input.nextDouble();
balance = principal;
System.out.println("What is your monthly interest rate in decimal?");
rate = input.nextDouble();
System.out.println("What is your monthly payment?");
payment = input.nextDouble();
while (balance > 0) {
if (month == 13) {
year++;
month = 1;
}
calculated_interest = (balance * rate * 100) / 100.0;
principal_amt = payment - calculated_interest;
System.out.println("Year " + year + ", " + "Month " + month + ":");
System.out.println("Your interest amount is " + "$" + calculated_interest);
if (balance > payment) {
balance = ((int) (Math.round((balance - principal_amt) * 100))) / 100.0;
System.out.println("Your principal amount " + "$" + principal_amt);
System.out.println("Your new balance is " + "$" + balance);
} else {
System.out.println("Your principal amount " + "$" + (balance - calculated_interest));
System.out.println("Your new balance is " + "$" + 0);
System.out.println("You'll only pay $" + (calculated_interest + balance) + " this month.");
break;
}
System.out.println();
month++;
}
input.close();
}
Upvotes: 0
Reputation: 901
The problem is when the loop is at Year 29, month 12 the values are
Year 29, Month 12:
Your interest amount is $20.9
Your principal amount $4176.0
Your new balance is $3.45
Now, at this the value of balance
is not less than 0
, so while
will be true and when the new values are calculated the balance (balance = ((int)(Math.round((balance-actual_payment)*100)))/100.0;
) becomes negative as balance - atual_payment will return negative because the previous balance is $3.45
.
One of the solution, add another if condition in the while loop after computing the balance.
while(balance>0){
calculated_interest = ((int)(Math.round(balance*rate*100)))/100.0;
principal_amt = ((int)(Math.round((payment-calculated_interest))*100))/100.0;
actual_payment = ((int)(Math.round((payment-calculated_interest)*100)))/100.0;
balance = ((int)(Math.round((balance-actual_payment)*100)))/100.0;
if (balance < 0){
break; //this will break out of the loop
}
if(month == 13){
year++;
month = 1;
}
System.out.println("Year " + year + ", " + "Month " + month + ":");
System.out.println("Your interest amount is " + "$" + calculated_interest);
System.out.println("Your principal amount " + "$" + principal_amt);
System.out.println("Your new balance is " + "$" + balance);
System.out.println();
month++;
}
This code will not print out negative balance.
Upvotes: 0