/*
* BasicMethodsLab.java
*
* Created on February 8, 2007, 9:56 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package lab03;
import javax.swing.*;
import java.text.*;
public class BasicMethodsLab {
public static void main( String args[] ) {
String propertyCostStr; // Value entered by user; string type
String downPaymentStr; // Value entered by user; string type
String numberOfYearsStr; // Value entered by user; string type
String propertyStr; // Letter entered by user; string type
char propertyType; // Storing the letter chosen by the user
double propertyCost; // The double form of the user-entered property cost
double downPayment = 0; // The double form of the user-entered down payment
double loanAmount = 0; // The calculated price of the loan amount
double monthlyPayment; //The monthly loan payment
double annualInterestRate; //The annual interest rate
double result = 0;
int response = 0; // The user's response as to whether to continue
int numberOfYears = 0; //integer form of the user-entered down payment
DecimalFormat prec2 = new DecimalFormat("$#.00");
// Initialize the output string to the empty string
String outputStr = "";
while (true) {
// Read in item price from user as a string
propertyCostStr = JOptionPane.showInputDialog("Enter property cost");
// Convert from type String to type double
propertyCost = Double.parseDouble(propertyCostStr);
// Read in number ordered from user as a string
downPaymentStr = JOptionPane.showInputDialog("Enter down payment");
// Convert from type String to type double
downPayment = Double.parseDouble(downPaymentStr);
// Read in number of years from user as a string
numberOfYearsStr = JOptionPane.showInputDialog("Enter number of years for loan:");
propertyType = 'R';
propertyType = 'C';
propertyType = 'A';
//Read in category from user as a number
propertyStr = JOptionPane.showInputDialog("Choose the letter that best corresponds with your" +
"property type" +
"\nR. Residential "
+"\nC. Commercial \nA. Argricultural");
// Convert from type String to type integer
numberOfYears = Integer.parseInt(numberOfYearsStr);
// Calculate loan amount for the property
loanAmount = propertyCost - downPayment;
// Echo print the entered values
outputStr = outputStr + "Type: " + getPropertyTypeName(propertyType)+ ", ";
outputStr = outputStr + "Cost: " + prec2.format(propertyCost) + ", ";
outputStr = outputStr + "Loan Amount: " + prec2.format(loanAmount) + ", ";
// Calculate monthly payment
// Change the annual interest rate to a decimal (/100) and a monthly rate (/12)
// Print the monthly payment
outputStr = outputStr + "Monthly Amount: " + prec2.format(calculatePayment(propertyCost, downPayment,
numberOfYears, propertyType, loanAmount)) + ", " + "Number of Years: " +
numberOfYears + "\n" + "Total Amount Paid: " +
prec2.format(computeTotalPayment(result, numberOfYears));
//Print the number of years for the loan
// Determine whether the user wants to continue the loop
response = JOptionPane.showConfirmDialog(null, outputStr + "\nContinue?", "Confirm",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.NO_OPTION)
break;
}
// Display final output to the user
JOptionPane.showMessageDialog(
null, outputStr, "Property Loan Amounts", JOptionPane.INFORMATION_MESSAGE);
System.exit( 0 ); // Terminate the program
}
static double calculatePayment(double propertyCost, double downPayment, int numberOfYears,
char propertyType, double loanAmount){
double subResult = 0.075;
double result = 0;
switch (propertyType){
case 'R':
case 'r': subResult = computeResidentialPayment(propertyCost, downPayment, numberOfYears);break;
case 'C':
case 'c': subResult = computeCommercialPayment(propertyCost, downPayment, numberOfYears);break;
case 'A':
case 'a': subResult = computeAgriculturalPayment(propertyCost, downPayment, numberOfYears);break;
default: subResult = computeResidentialPayment(propertyCost, downPayment, numberOfYears);break;
}
result = computeMonthlyPayment(loanAmount, subResult, numberOfYears);
return result;
}
static double computeResidentialPayment(double propertyCost, double downPayment, int numberOfYears) {
double annualInterestRate;
double loanAmount;
if ((propertyCost/downPayment) < 0.2)
annualInterestRate = 0.0575;
else
annualInterestRate = 0.0675;
return annualInterestRate;
}
static double computeCommercialPayment(double propertyCost, double downPayment, int numberOfYears) {
double annualInterestRate;
double loanAmount;
if ((propertyCost/downPayment) < 0.3)
annualInterestRate = 0.086;
else
annualInterestRate = 0.096;
return annualInterestRate;
}
static double computeAgriculturalPayment(double propertyCost, double downPayment, int numberOfYears) {
double annualInterestRate;
double loanAmount;
if ((propertyCost/downPayment) < 0.1)
annualInterestRate = 0.0525;
else
annualInterestRate = 0.0625;
return annualInterestRate;
}
static double computeMonthlyPayment(double loanAmount, double subResult, int numberOfYears) {
double monthlyPayment;
// Change the annual interest rate to a decimal (/100) and a monthly rate (/12)
monthlyPayment = loanAmount * (subResult/1200) /
(1 - 1 / Math.pow(1 + (subResult/1200), numberOfYears * 12));
return monthlyPayment;
}
static double computeTotalPayment(double result, int numberOfYears) {
double total = 0;
total = result * numberOfYears * 12;
return total;
}
static String getPropertyTypeName(char propertyType){
String propertyName;
switch (propertyType) {
case 'R':
case 'r': propertyName = ("Residential"); break;
case 'C':
case 'c': propertyName = ("Commercial"); break;
case 'A':
case 'a': propertyName = ("Agricultural"); break;
default: propertyName = ("Residential"); break;
}
return propertyName;
}
}