Some Name
Some Name

Reputation: 571

Java Variable from other class

I have 3 classes: Main, AutoHuur, Auto. Everythign works but 1 thing. I can't seem to get the "Prijs" variable from my Auto.class to display in my toString in my AutoHuur.class. It keeps showing up as 0. Why is this? (I know I initialize it to 0 if it's a null, but why is it a null and not the value from the Auto.class variable prijsPerDag?) Thank you

Main.class:

    public class Main{
    public static void main(String[] args) {
        AutoHuur ah1 = new AutoHuur();
        System.out.println("Eerste autohuur:\n" + ah1 + "\n");

        Klant k = new Klant("Mijnheer de Vries");
        k.setKorting(10.0);
        ah1.setHuurder(k);
        Auto a1 = new Auto("Peugeot 207", 50.0);
        ah1.setGehuurdeAuto(a1);
        ah1.setAantalDagen(4);
        System.out.println("Eerste autohuur:\n" + ah1 + "\n");

        AutoHuur ah2 = new AutoHuur();
        Auto a2 = new Auto("Ferrari", 3500.0);
        ah2.setGehuurdeAuto(a2);
        ah2.setHuurder(k);
        ah2.setAantalDagen(1);
        System.out.println("Tweede autohuur:\n" + ah2 + "\n");

        System.out.println("Gehuurd: " + ah1.getGehuurdeAuto());
        System.out.println("Gehuurd: " + ah2.getGehuurdeAuto());
    }
}

Autohuur.class:

public class AutoHuur {
    private Klant huurder;
    private Auto gehuurdeAuto;
    private Auto prijs;

    private Integer aantalDagen;

    public AutoHuur(){
    }

    public void setHuurder(Klant nwH){
        huurder = nwH;
    }

    public void setGehuurdeAuto(Auto nwGA){
        gehuurdeAuto = nwGA;
    }

    public Auto getGehuurdeAuto(){
    return gehuurdeAuto;
    }

    public Auto getPrijs(){
    return prijs;
    }

    public void setAantalDagen(Integer nwD){
        aantalDagen = nwD;
    }

public String toString(){
    String s = "";

    if (gehuurdeAuto == null){
        s = s + "er is geen auto bekend\n"; }
    else { 
    s = s + gehuurdeAuto; }

    if (huurder == null){
    s = s + "er is geen huurder bekend\n"; }
    else { 
    s = s + huurder; }

    if (aantalDagen == null){
    s = s + "aantal dagen: 0"; }
    else { 
    s = s + "aantal dagen: " + aantalDagen; }

    if (prijs == null){
        s = s + " en dat kost 0.0"; }
    else { 
    s = s + " en dat kost" + prijs; }

    return s;
}

}

Auto.class:

public class Auto {
    private String type;
    private Double prijsPerDag;

    public Auto(String tp, Double nwPr){
        type = tp;
        prijsPerDag = nwPr;
    }

    public void setPrijsPerDag(Double prPd){
        prijsPerDag = prPd;
    }

    public Double getPrijsPerDag(){
        return prijsPerDag;
    }

    public String toString(){
        String s = type + " met prijs per dag: " + prijsPerDag + "\n";
        return s;
    }
}

Upvotes: 1

Views: 112

Answers (3)

MC Emperor
MC Emperor

Reputation: 23057

The variable prijs is an object, which is initialized to null when an instance of AutoHuur is created. Since prijs is never set, it is always null.

That's why prijs == null always evaluates to true.

You need to set prijs somewhere.


It seems that you need to work on your design desicions. You are, for example, using an Auto class name, while it is in fact a link between a daily charge and a car type. If each link is represented by such an object (in your case Auto), then it absolutely makes no sense to have more than one instance.

For example,

new Auto("Peugeot", 40.0);
new Auto("Peugeot", 40.0);

is technically perfectly valid, but it is just not logical.

Furthermore, if a class holds data of some kind, then it's good to delegate operations upon that data also to that class.

class Auto {

    private String type;
    private double pricePerDay;

    public double calculateTotalPrice(int numberOfDays) {
        return numberOfDays * this.pricePerDay;
    }
}

Upvotes: 0

ivan martinez
ivan martinez

Reputation: 196

You have to remove the first System.out becasuse in Main.java your instance AutoHuur is empty, so when your want to print the result , the out will be values empty.

Upvotes: 0

Laci Végh
Laci Végh

Reputation: 66

In AutoHuur.class you can get your prijsPerDag variable from Auto.class using an Auto object, e.g.:

gehuurdeAuto.getPrijsPerDag()

You can calculate the price:

aantalDagen * gehuurdeAuto.getPrijsPerDag()

Is this what would you like to do?

Upvotes: 2

Related Questions