Reputation: 61
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.
Upvotes: 1
Views: 519
Reputation: 40396
You have:
Calculator calculator = new Calculator();
calculator.calculator();
Calculator years = new Calculator();
years.years();
Calculator inflation = new Calculator();
inflation.inflation();
Calculator price = new Calculator();
price.price();
You are creating a different Calculator
for every piece of information you input. Each of those is independent of the others. Instead, you mean:
Calculator calculator = new Calculator();
calculator.calculator();
calculator.years();
calculator.inflation();
calculator.price();
Each Calculator
has its own copy of the instance fields (and they're initialized to 0 be default). Setting e.g. the time difference in one does not affect the time difference set in the others.
The objects you instantiate should reflect, conceptually, what you are trying to do. Was your intention to have 4 separate calculators? Or was your intention to have a single calculator with all of the information? Of course, it was the latter -- and your code should reflect that.
Upvotes: 0
Reputation: 45080
You're using different instances of Calculator
in the calculateCost()
. Use the same instance to get all the details and to calculate the final price as well.
public void calculateCost(){
Calculator calculator = new Calculator();
calculator.calculator();
calculator.years();
calculator.inflation();
calculator.price();
}
As you're using different instances, each instance has its own copy of the instance variables(which you're ultimately using to calculate the final price) and that's why for each instance only that particular value is being populated. Using the same instance would populate all the values by calling the various methods and thus, the price would be calculated using all the values entered by you via the different methods.
Upvotes: 3