Computer Science 202
Introduction to Programming

Fall 2012, The College of Saint Rose

NoInterestLoan BlueJ Project

Click here to download a BlueJ project for NoInterestLoan.


NoInterestLoan Source Code

The Java source code for NoInterestLoan is below. Click on a file name to download it.


NoInterestLoan.java

/*
 * Example NoInterestLoan: a program that uses JOptionPane dialog
 * boxes to read in a loan amount and monthly payment, and report output
 * for the number of payments needed to pay of a no-interest loan, and 
 * any amount after those full payments have been made.
 *
 * Jim Teresco, The College of Saint Rose, CSC 202, Fall 2012
 *
 * $Id: NoInterestLoan.java 1908 2012-09-20 03:11:12Z terescoj $
 */

// a new import to use the message dialogs instead of Scanners
// and System.out.println for our I/O
import javax.swing.JOptionPane;

public class NoInterestLoan {

    public static void main(String[] args) {
        
        // We declare but do not initialize the String variable
        // that will be used for temporary storage of String inputs.
        String input;
        
        // Get the loan amount: this will bring up a "dialog box" with the
        // message we provide with a text field where input can be typed, and 
        // will return to us a String representation of what was typed in.
        input = JOptionPane.showInputDialog("How many dollars do you wish to borrow?");
        
        // Now, we have that amount as a String.  We really want it to be
        // an int.  Scanners were able to convert for us -- all we had to
        // do was use the nextInt method of the Scanner.
        //
        // Here, we need to use a method provided by Java's Integer class
        // to do the conversion:
        int loan = Integer.parseInt(input);
        
        // Get the monthly payment
        // Note that we reuse the input variable, since we no longer need
        // the String it contained from our first prompt.
        input = JOptionPane.showInputDialog("How much can you afford to pay per month?");
        
        // And again convert to an int
        int payment = Integer.parseInt(input);
        
        // Now, we compute the number of months we need to make full payments
        int monthsToPayInFull = loan/payment;
        
        // Report that result with a "message dialog" which will be an
        // output-only popup window.
        JOptionPane.showMessageDialog(null, "You will make payments in full of $" + 
                    payment + " for " + monthsToPayInFull + " months.");
                    
        // There may be some remaining money to pay back after the last payment
        // in full, let's see how much.
        int lastPayment = loan % payment;
        
        // If there is no last payment due, we do not need to print anything, so
        // the following is enclosed in an if statement that happens only when
        // there is a nonzero last payment to include.
        
        if (lastPayment > 0) {
            JOptionPane.showMessageDialog(null, "Then in month " + (monthsToPayInFull + 1) +
                    " you pay the final $" + lastPayment + ".");
        }
    }
}