Write an applet or application to keep track of how much money is owed
after each payment on a loan.
As an example, suppose you start out on day 1 with a loan of $1000 at
an annual percentage rate of 10%, compounded daily. Suppose you make
your first payment of $500 on day 30. The amount you owe after
applying your payment is computed as follows:
- The daily interest rate is 10% / 365 days = 0.0274 % per day
- On day 2, you owe $1000 + ($1000 * 0.0274 %) = $1000.27
- On day 3, you owe $1000.27 + ($1000.27 * 0.0274 %) = $1000.55
- ..
- On day 30, you compute the interest before applying the payment
(of course!). You owe $1007.70 from day 29. Now on day 30 before
the payment, you owe $1007.07 + ($1007.07 * 0.0274 %) = $1007.97.
- Then you apply the payment of $500. You now owe $507.97.
The program will report that after applying the payment on day 30, you
owe $507.97. Notice that the interest compounds daily, which means
each day interest is calculated on that day's balance and added to the
balance (that's how it's done in Ruritania, you got a problem with
that?). Now you could apply another payment on or after day 30, and
the program would tell you how much you owed after that payment. You
can not give a payment on a day before the current payment day.
Your program will input the initial loan amount. It will then ask if
you are a regular customer ® or a preferred customer (P). Regular
customers get an annual percentage rate (APR) of 10%; preferred
customers get an APR of 8%. This will determine the daily percentage
rate for the duration of the loan.
The program should then accept payments (and indicating the current
balance) until the loan is paid in full, and then report in a
JTextArea:
- How many payments it took to pay the loan
- Total interest that was paid
- Any money returned from the final payment if you overpaid
- Each payment day, amount and resulting balance.
ERROR CHECKING
- Ensure that the initial amount of the loan is positive
- Ensure that user enters preferred or regular customer correctly
- Ensure that each payment amount is positive
- Ensure that the current payment is being applied on or after the day
the last payment was applied
HINTS
- The interest is compounded daily, so calculating one day's interest
and multiplying that by the number of days to the next payment day will
NOT give the correct interest accrued.
Example payment schedule: (note: this 30, 60, 90 is an example
only. The user can make a payment schedule of any number of increasing
payment days and amounts).
Initial loan: $1000
regular customer.
payment #1 of $500 on day 30
On day#30, you owe $507.97.
payment #2 of $500 on day 60
On day#60, you owe $12.17.
payment #3 of $500 on day 90
$487.73 of your final payment will be returned.
You paid off your loan after 90 days in 3 payments.
You paid $12.27 in interest.
I'm having 3 main problems with my program, 1) i can't figure out how to make the interest compond daily. 2) i'm not sure how to do the payment periods, 3) each time my loop starts over it starts over with the initial loan balance. if anyone can help me I would be thankful.
CODE
import java.awt.*;
import javax.swing.*;
import java.text.*;// For Decimal Format Class
public class loan
{
public static void main(String [] argv)
{
String amount, customerStatus, input, payment;
DecimalFormat df2 = new DecimalFormat ("0.00");//sets the decimal to two spaces
double dailyInterest, percentRate, pay, totalPayment, remainingBalance, balance;
int userAnswer;
percentRate = 0.0; totalPayment = 0.0; dailyInterest = 0.0;
do
{
amount = JOptionPane.showInputDialog("Enter Initial Amount of Loan");
balance = Integer.parseInt( amount );
if ((balance<0))
{
JOptionPane.showMessageDialog( null,
"----- ---- -----\nERROR\n\n----- ---- -----\nYOU MUST ENTER A POSITIVE LOAN AMOUNT");
}
}
while ((balance<0));
customerStatus = JOptionPane.showInputDialog("Please enter if you are a Preferred or Reqular customer" + "\n" +
"Please only enter the first letter.\n" +
"P for Preferred - R for Regular");
do
{
payment = JOptionPane.showInputDialog("Please enter the amount you want to pay.");
pay = Integer.parseInt( payment );
do
{
if ((pay<0))
{
JOptionPane.showMessageDialog( null,
"----- ---- -----\nERROR\n\n----- ---- -----\nYOU MUST ENTER A POSITIVE PAYMENT AMOUNT");
}
}
while ((pay<0));
input = JOptionPane.showInputDialog("Enter your payment plan");
if (customerStatus.equalsIgnoreCase("P"))
percentRate = (0.0219/100 * balance) + balance;
if (customerStatus.equalsIgnoreCase("R"))
percentRate = (0.0274/100 * balance) + balance;
remainingBalance = percentRate - pay;
dailyInterest += percentRate - balance;
userAnswer = JOptionPane.showConfirmDialog(null,
"Do you want to make another payment",
"some title message",
JOptionPane.YES_NO_OPTION);
if (userAnswer == JOptionPane.NO_OPTION);
}
while (userAnswer == JOptionPane.YES_OPTION);
JOptionPane.showMessageDialog( null, (" You have a remaining balance is " + df2.format(remainingBalance)));
JOptionPane.showMessageDialog( null, (" You have a Daily interest of " + df2.format(dailyInterest)));//displays GPA
System.exit(0);
}
}