In: Computer Science
Sanchez Construction Loan Co. makes loans of up to $100,000 for construction projects. There are two categories of Loans—those to businesses and those to individual applicants.
Write an application that tracks all new construction loans. The application also must calculate the total amount owed at the due date (original loan amount + loan fee). The application should include the following classes:
• Loan —A public abstract class that implements the LoanConstants interface. A Loan includes a loan number, customer last name, amount of loan, interest rate, and term. The constructor requires data for each of the fields except interest rate. Do not allow loan amounts greater than $100,000. Force any loan term that is not one of the three defined in the LoanConstants class to a short-term, 1-year loan. Create a toString() method that displays all the loan data.
• LoanConstants—A public interface class. LoanConstants includes constant values for short-term (1 year), medium-term (3 years), and long-term (5 years) loans. It also contains constants for the company name and the maximum loan amount.
• BusinessLoan—A public class that extends Loan. The BusinessLoan constructor sets the interest rate to 1% more than the current prime interest rate.
• PersonalLoan—A public class that extends Loan. The PersonalLoan constructor sets the interest rate to 2% more than the current prime interest rate.
• CreateLoans— An application that creates an array of five Loans. Prompt the user for the current prime interest rate. Then, in a loop, prompt the user for a loan type and all relevant information for that loan. Store the created Loan objects in the array. When data entry is complete, display all the loans. For example, the program should accept input similar to the sample program execution below:
Welcome to Sanchez Construction Enter the current prime interest rate as a decimal number, for example, .05 0.08 Is this a (1) Business loan or (2) Personal loan 1 Enter account number 1 Enter name Joe Enter loan amount 10000 Enter term 5 Is this a (1) Business loan or (2) Personal loan 2 Enter account number 2 Enter name Sara Enter loan amount 5000 Enter term 3 ... And so on for 5 total loans
After all loan information is input, the program should output the loans in the following format:
Sanchez Construction Loan #1 Name: Joe $10000.0 for 5 year(s) at 9% interest Loan #2 Name: Sara $5000.0 for 3 year(s) at 10% interest Loan #3 Name: Mike $975.0 for 1 year(s) at 10% interest Loan #4 Name: Jane $7000.0 for 5 year(s) at 9% interest Loan #5 Name: Peter $300.0 for 1 year(s) at 9% interest
====================================================
public class BusinessLoan extends Loan
{
public BusinessLoan(int num, String name, double amt, int yrs,
double prime)
{
// write your code here
}
}
===============================================
import java.util.*;
public class CreateLoans implements LoanConstants
{
public static void main(String[] args)
{
// write your code here
}
}
===============================================
public abstract class Loan implements LoanConstants
{
protected int loanNum;
protected String lastName;
protected double amount;
protected double rate;
protected int term;
public Loan(int num, String name, double amt, int yrs)
{
// write your code here
}
public String toString()
{
// write your code here
}
public boolean equals(Loan loan)
{
// write your code here
}
}
=========================================
public interface LoanConstants
{
public static final int MAXLOAN = 100000;
public static final int SHORT_TERM = 1;
public static final int MEDIUM_TERM = 3;
public static final int LONG_TERM = 5;
public static final String COMPANY = "Sanchez Construction";
}
============================================
public class PersonalLoan extends Loan
{
public PersonalLoan(int num, String name, double amt, int yrs,
double prime)
{
// write your code here
}
}
CODE :
Interface LoanConstants:
public interface LoanConstants {
public static final int MAXLOAN = 100000;
public static final int SHORT_TERM = 1;
public static final int MEDIUM_TERM = 3;
public static final int LONG_TERM = 5;
public static final String COMPANY = "Sanchez
Construction";
}
Class Loan :
public abstract class Loan implements LoanConstants
{
protected int loanNum;
protected String lastName;
protected double amount;
protected double rate;
protected int term;
public Loan(int num, String name, double amt, int
yrs)
{
if(amt > 100000)
throw new
IllegalArgumentException("Loan Amount could not be greater than
$100,000");
loanNum = num;
lastName = name;
amount = amt;
if(yrs == MEDIUM_TERM || yrs == LONG_TERM)
term = yrs;
else
term = SHORT_TERM;
}
public boolean equals(Loan obj) {
if (this == obj)
return
true;
if (obj == null)
return
false;
if (getClass() !=
obj.getClass())
return
false;
Loan other = (Loan) obj;
if (Double.doubleToLongBits(amount)
!= Double.doubleToLongBits(other.amount))
return
false;
if (lastName == null) {
if
(other.lastName != null)
return false;
} else if
(!lastName.equals(other.lastName))
return
false;
if (loanNum != other.loanNum)
return
false;
if (Double.doubleToLongBits(rate)
!= Double.doubleToLongBits(other.rate))
return
false;
if (term != other.term)
return
false;
return true;
}
@Override
public String toString() {
return "Loan #" + loanNum + " Name:
" + lastName + " $" + amount + "\n for " + term
+ " year(s) at " +
Math.round(rate*100.0*100.0)/100.0 + "% interest";
}
}
Class BusinessLoan :
public class BusinessLoan extends Loan
{
public BusinessLoan(int num, String name, double amt,
int yrs, double prime)
{
super(num,name,amt,yrs);
this.rate = prime + 0.01;
}
}
Class PersonalLoan :
public class PersonalLoan extends Loan
{
public PersonalLoan(int num, String name, double amt,
int yrs, double prime)
{
super(num,name,amt,yrs);
this.rate = prime + 0.02;
}
}
Class CreateLoans :
import java.util.*;
public class CreateLoans implements LoanConstants
{
public static void main(String[] args)
{
Loan arrLoan[] = new Loan[5];
Scanner sc = new
Scanner(System.in);
double primeRate,amt;
int loanType,acctNum,term;
String name;
System.out.println("Welcome to " +
COMPANY );
System.out.println("Enter the
current prime interest rate as a decimal number, for example,
.05");
primeRate =
Double.parseDouble(sc.nextLine());
for(int i=0; i < arrLoan.length;
i++)
{
System.out.println("Is this a (1) Business loan or (2) Personal
loan");
loanType =
Integer.parseInt(sc.nextLine());
System.out.println("Enter account number");
acctNum =
Integer.parseInt(sc.nextLine());
System.out.println("Enter name");
name =
sc.nextLine();
System.out.println("Enter loan amount");
amt =
Double.parseDouble(sc.nextLine());
System.out.println("Enter term");
term =
Integer.parseInt(sc.nextLine());
if(loanType ==
1)
arrLoan[i] = new
BusinessLoan(acctNum,name,amt,term,primeRate);
else if(loanType
== 2)
arrLoan[i] = new
PersonalLoan(acctNum,name,amt,term,primeRate);
else {
throw new IllegalArgumentException("Please
Select proper loan type");
}
}
for(int j=0; j < arrLoan.length;
j++)
System.out.println(arrLoan[j]);
sc.close();
}
}
OUTPUT :
I have tested with two inputs.