import java.util.Scanner;
public class InterestCalculator5 {
public static void main(String[] args )
{
Scanner input = new Scanner(System.in);
String strAnsYesNo;
char ansYesNo = 0;
//Prompt user to enter loan amount
System.out.print("Enter Loan Amount: ");
double principle = input.nextInt();
getPrincipalAmount(principle);
//Prompt user to enter interest rate
System.out.print("Enter Yearly Interest Rate (1 to 100 percent): ");
double interest = input.nextDouble();
getInterestRate(interest);
//Prompt user to enter term
System.out.print("Enter the Term (in months): ");
double term = input.nextInt();
getTerm(term);
int type=0;
//Prompt user to enter calculation type
do{System.out.print("Enter Interest Calculation Type (1 – Simple, 2 – Monthly Compounded, 3 – Daily Compounded):");
type = input.nextInt();
switch(type)
{
case 1: displayInterest(principle,interest,term,"Simple",round((calculateSimpleInterest(principle, interest, term)),5),round((calculateSimpleInterest(principle, interest, term)+principle),2));
break;
case 2: displayInterest(principle,interest,term,"Compound Monthly", round(calculateCompoundInterest(principle, interest,term ,12.0 ),5), round((calculateCompoundInterest(principle, interest,term ,12.0)+principle),2));
break;
case 3: displayInterest(principle,interest,term,"Compound Daily", round(calculateCompoundInterest(principle, interest, term, 365.0 ),5), round((calculateCompoundInterest(principle, interest, term, 365.0)+principle),2));
break;
default: System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +type);
break;
}
}while (ansYesNo =='y' || ansYesNo =='Y');
System.out.println("Thank you for using Interest Calculator!");
}
/** Round **/
public static double round(double numb1, double numb2) {
double round = ((double) Math.round(numb1*(Math.pow(10, numb2)))/(Math.pow(10, numb2)));;
return round;
}
/** Calculate Simple **/
public static double calculateSimpleInterest(double numb1, double numb2, double numb3) {
double calculateSimpleInterest = ((numb1)*(numb2/100.0)*(numb3/12.0));
return calculateSimpleInterest;
}
/** Calculate Compounded Daily **/
public static double calculateCompoundInterest(double numb1, double numb2, double numb3, double numb4 ) {
double calculateCompoundInterest = (numb1*Math.pow((1.0+((numb2/100.0)/numb4)),(numb4*(numb3/12.0))))-numb1;
return calculateCompoundInterest;
}
/** display interest **/
public static void displayInterest(double numb1, double numb2, double numb3, String word1, double numb4, double numb5 ) {
System.out.println("Principal Amount Interest Rate Term Calculation Type Calculated Interest Total Repayment");
System.out.println("---------------- ------------- ---- ---------------- ------------------- ---------------");
System.out.print(numb1+" ");
System.out.print(+numb2+" ");
System.out.print(+numb3+" ");
System.out.print(word1+" ");
System.out.print(+numb4+" ");
System.out.println(+numb5);
}
/** Get principal amount **/
public static double getPrincipalAmount(double numb1) {
double getPrincipalAmount = 0;
do{if (numb1 > 0)
getPrincipalAmount = numb1;
else{
System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb1);
}
}while (numb1 < 0);
return getPrincipalAmount;
}
/** Get interest rate **/
public static double getInterestRate(double numb1) {
double getInterestRate = 0;
do{if (numb1 >= 0 && numb1 <= 100)
getInterestRate = numb1;
else{
System.out.println("Data Error: Interest rate must be greater than or equal to zero and less than or equal to 100. You entered " +numb1);
}
}while (numb1 <= 0 && numb1 >= 100);
return getInterestRate;
}
/** Get term **/
public static double getTerm(double numb1) {
double getTerm = 0;
do{if (numb1 > 0)
getTerm = numb1;
else{
System.out.println("Data Error: Loan amount must be greater than zero. You entered " +numb1);
}
}while (numb1 < 0);
return getTerm;
}
/** Get calculation type **/
public static double getCalculationType(double numb1) {
double getCalculationType = 0;
do{if (numb1 <1||numb1 >3)
getCalculationType = numb1;
else{
System.out.println("Calculation Type Error: You must select 1, 2 or 3. You entered " +numb1);;
}
}while (numb1 > 1||numb1 < 3);
return getCalculationType;
}
}