Java project returning null values

Hi I am new to Java programming. Why are my values returning null after I enter them on the input dialog. I have two classes, one called VehicleApp and the other called VehicleFactory. Help would be appreciated. Thanks.

Full code VehicleApp.java

package romprojectname;

import java.text.NumberFormat;
import javax.swing.JOptionPane;

public class VehicleApp{

public static void main(String[] args) {

        String firstname = JOptionPane.showInputDialog("Enter your first name");
        String lastname = JOptionPane.showInputDialog("Enter your last name");
        long phone = Long.parseLong(JOptionPane.showInputDialog("Enter your phone"));
        int nbrVehicles = Integer.parseInt(JOptionPane.showInputDialog("Enter number of vehicles"));
        int nbrTanks = Integer.parseInt(JOptionPane.showInputDialog("Enter number of tanks"));

        VehicleFactory vehicleObject = new VehicleFactory();
        vehicleObject.getSummary();
        vehicleObject.HayloFactory(firstname, lastname, phone, nbrVehicles, nbrTanks);
        vehicleObject.calcFuelTankCost();
        vehicleObject.calcManufacturingCost();
        vehicleObject.calcSubtotal();
        vehicleObject.calcTax();
        vehicleObject.calcTotal();
    }
    }

Full code VehicleFactory.java

package romprojectname;

   import java.text.NumberFormat;
   import javax.swing.JOptionPane;

   public class VehicleFactory{
    private String firstname;
    private String lastname;
    private Long phone;
    private int nbrVehicles =0;
    private int nbrTanks =0;
    private double manufactureCost =0;
    private double fuelTankCost =0;
    private double subtotal =0;
    private double tax =0;
    private double total = 0;
    private final double VEHICLE_PRICE = 500.19;
    private final double FUELCELL_PRICE = 2.15;
    private final int CELLS_PER_TANK = 12;
    private final double taxrate = 7.25 / 100 ;

public void HayloFactory(String firstname, String lastname, Long phone, int nbrVehicles, int nbrTanks){
    this.firstname = firstname;
    this.lastname = lastname;
    this.phone = phone;
    this.nbrVehicles = nbrVehicles;
    this.nbrTanks = nbrTanks;
}

public void calcManufacturingCost(){
    double manufactureCost = nbrVehicles * VEHICLE_PRICE;
}

public void calcFuelTankCost(){
    double fuelTankCost = nbrVehicles * nbrTanks * CELLS_PER_TANK * FUELCELL_PRICE;
}

public void calcSubtotal(){
    double subtotal = manufactureCost + fuelTankCost;
}

public void calcTax(){
    double tax = subtotal * taxrate;
}

public void calcTotal(){
    double total = subtotal + tax;
}   

NumberFormat cf = NumberFormat.getCurrencyInstance();

public void getSummary(){
    String summary = "WELCOME TO HAYLO MANUFACTURING" + "\n" + "\n";

    summary += "Customer Name: " + firstname + " " + lastname + "\n";
    summary += "Customer Phone: " + phone + "\n";
    summary += "Number of Vehicles: " + nbrVehicles + "\n";
    summary += "Number of Tanks: " + nbrTanks + "\n";
    summary += "Vehicle Cost ($500.19 / vehicle): " + cf.format(manufactureCost) + "\n";
    summary += "Tanks Cost ($2.15 / fuel cell): " + cf.format(fuelTankCost) + "\n";
    summary += "Subtotal: " + cf.format(subtotal) + "\n";
    summary += "Tax (7.25%): " + cf.format(tax) + "\n";
    summary += "Total: " + cf.format(total) + "\n";

    //display the summary
    JOptionPane.showMessageDialog(null, summary);
            }  
    }

Problem (code taken from VehicleFactory.java)

All of the summaries such as customer name are returning null values and all the costs and totals are $0.00.

public void getSummary(){
String summary = "WELCOME TO HAYLO MANUFACTURING" + "\n" + "\n";

summary += "Customer Name: " + firstname + " " + lastname + "\n";
summary += "Customer Phone: " + phone + "\n";
summary += "Number of Vehicles: " + nbrVehicles + "\n";
summary += "Number of Tanks: " + nbrTanks + "\n";
summary += "Vehicle Cost ($500.19 / vehicle): " + cf.format(manufactureCost) + "\n";
summary += "Tanks Cost ($2.15 / fuel cell): " + cf.format(fuelTankCost) + "\n";
summary += "Subtotal: " + cf.format(subtotal) + "\n";
summary += "Tax (7.25%): " + cf.format(tax) + "\n";
summary += "Total: " + cf.format(total) + "\n";

//display the summary
JOptionPane.showMessageDialog(null, summary);
Jon Skeet
people
quotationmark

You haven't really described the problem, but I suspect this is the cause:

VehicleFactory vehicleObject = new VehicleFactory();
vehicleObject.getSummary();
vehicleObject.HayloFactory(firstname, lastname, phone, nbrVehicles, nbrTanks);

You're calling getSummary before you call HayloFactory - so it's trying to display the values in the object before you've set them to useful values.

Additionally, all your calcXyz methods are introducing new local variables, like this:

public void calcTotal(){
    double total = subtotal + tax;
}   

Instead, they should be setting the field values:

public void calcTotal(){
    total = subtotal + tax;
}   

If you change all of your calculation methods appropriately, then move the getSummary() call to the very end, it will work. (It's not quite how I'd have written the code, but that's a different matter.)

people

See more on this question at Stackoverflow