Hi have a question,why i do not get value output for FutureValue,do i use in the wrong place in Main method calculateFutureValue() ?
package sd4.dobs.ui;
import java.util.Scanner;
public class Console {
private static Scanner sc = new Scanner(System.in);
public static void displayLine() {
System.out.println();
}
public static void displayLine(String s) {
System.out.println(s);
}
public static String getString(String prompt) {
System.out.print(prompt);
String s = sc.nextLine();
return s;
}
public static int getInt(String prompt) {
int i = 0;
while (true) {
System.out.print(prompt);
try {
i = Integer.parseInt(sc.nextLine());
break;
} catch (NumberFormatException e) {
System.out.println("Error! Invalid integer. Try again.");
}
}
return i;
}
public static double getDouble(String prompt) {
double d = 0;
while (true) {
System.out.print(prompt);
try {
d = Double.parseDouble(sc.nextLine());
break;
} catch (NumberFormatException e) {
System.out.println("Error! Invalid decimal. Try again.");
}
}
return d;
}
}
----------------------------------------------------------------------------------
package sd4.dobs.model;
public class Investment {
private double monthlyInvestment;
public double getMonthlyInvestment() {
return monthlyInvestment;
}
public void setMonthlyInvestment(double monthlyInvestment) {
this.monthlyInvestment = monthlyInvestment;
}
public double getYearlyInterestRate() {
return yearlyInterestRate;
}
public void setYearlyInterestRate(double yearlyInterestRate) {
this.yearlyInterestRate = yearlyInterestRate;
}
public int getYears() {
return years;
}
public void setYears(int years) {
this.years = years;
}
private double yearlyInterestRate;
private int years;
public Investment(double monthlyInvestment, double yearlyInterestRate, int years) {
this.monthlyInvestment = monthlyInvestment;
this.yearlyInterestRate = yearlyInterestRate;
this.years = years;
}
public Investment() {
this.monthlyInvestment=0;
this.yearlyInterestRate=0;
this.years=0;
}
public double calculateFutureValue()
{
double monthlyInterestRate = yearlyInterestRate/12/100;
int months = years *12;
double futureValue =0;
for(int i =1;i<=months;i++)
{
futureValue += monthlyInterestRate;
double monthlyInterestAmount = futureValue * monthlyInterestRate;
futureValue += monthlyInterestAmount;
}
return futureValue;
}
@Override
public String toString() {
return "Inv/Mo " + monthlyInvestment + " Rate " + yearlyInterestRate + " Years " + years + " Future value "+ calculateFutureValue();
}
}
-------------------------------------------------------------------------------------
package sd4.dobs.ui;
import java.text.NumberFormat;
import java.util.ArrayList;
import sd4.dobs.model.Investment;
public class Main {
public static void main(String[] args) {
ArrayList<Investment>investmentList = new ArrayList<>();
Investment inv = new Investment();
double futurevalue = inv.calculateFutureValue();
// displayLine a welcome message
double monthlyInvestment=0;
double yearlyInterestRate =0;
int years =0;
Console.displayLine("Welcome to the Future Value Calculator");
Console.displayLine();
String choice = "y";
while (choice.equalsIgnoreCase("y")) {
// get input from user
monthlyInvestment
= Console.getDouble("Enter monthly investment: ");
yearlyInterestRate
= Console.getDouble("Enter yearly interest rate: ");
years
= Console.getInt("Enter number of years: ");
// see if the user wants to continue
choice = Console.getString("Continue? (y/n): ");
Console.displayLine();
}
investmentList.add(new Investment(monthlyInvestment,yearlyInterestRate,years));
for(Investment in : investmentList)
{
System.out.println(in + " " + futurevalue);
}
Console.displayLine("Bye!");
}
}