I am learning Java and am working on a basic program where a user can input a cost figure, an inflation percentage and a time (years) in order to calculate a cost including inflation.
I am using three classes, 1 application class, 1 driver class and 1 singular class. I am doing this as per the book to build OOP skills (I know there are simpler procedural / structured methods but that is not the point of my exercise).
The issue I am having that the output answer is $0 regardless of the figures I enter. This should not be the case obviously. If someone could please skim over my code and point me in the right direction I would be grateful.
Application Class:
package priceestimator;
public class PriceEstimator {
public static void main(String[] args) {
CostCalculator CostCalc = new CostCalculator();
CostCalc.calculateCost();
}
}
Driver Class:
public class CostCalculator {
public void calculateCost(){
Calculator calculator = new Calculator();
calculator.calculator();
Calculator years = new Calculator();
years.years();
Calculator inflation = new Calculator();
inflation.inflation();
Calculator price = new Calculator();
price.price();
}
}
Singular Class:
package priceestimator;
import java.util.Scanner;
public class Calculator {
private Scanner myScanner = new Scanner(System.in);
private double cost;
private double time;
private double costDif;
private void setTime(int time){
this.time = time;
}
private double getTime(){
return time;
}
private void setCost(double cost){
this.cost = cost;
}
private double getCost(){
return cost;
}
private void setCostDif(double costDif){
this.costDif = costDif;
}
private double getCostDif(){
return costDif;
}
private void getMyScanner(){
this.myScanner = myScanner;
}
public void calculator(){
System.out.println("Enter the current cost (numbers only):");
cost = myScanner.nextDouble();
}
public void years(){
System.out.println("Enter the time difference in years:");
time = myScanner.nextDouble();
}
public void inflation(){
double costTimeDif;
double decimalConversion;
int percent = 100;
double input;
System.out.println("Enter the current inflation rate percentage (numbers only):");
input = myScanner.nextDouble();
decimalConversion = input / percent;
costTimeDif = time * decimalConversion;
costDif = cost * costTimeDif;
}
public void price(){
double price;
price = costDif + cost;
System.out.println("The estimated price is: $" + price);
}
}
Thanks for your time.