mardi 11 mai 2021

Why does the counter inside if statement not work?

Hello friends i am trying to build a class Car for a project.There are many methods inside the following code as well as an if statement that i am having trouble to build, cosinder the following code

public class Car extends Vehicle {
    private boolean isDriving;
    private final int horsepower;
    private boolean needsMaintenance = false;
    private int tripsSinceMaintenance = 0;

    Car() {
        super();
        this.horsepower = 0;
        this.isDriving = false;
        this.needsMaintenance = false;
        this.tripsSinceMaintenance = 0;
    }

    public int getHorsepower() {
        return this.horsepower;
    }

    public boolean getDrive() {
        return this.isDriving;
    }

    public boolean getMain() {
        return this.needsMaintenance;
    }

    public int getTRIP() {
        return this.tripsSinceMaintenance;
    }

    public void drive() {
        this.isDriving = true;
    }

    public void stop() {
        this.isDriving = false;
    }


    public void repair() {
        this.needsMaintenance = false;
        this.tripsSinceMaintenance = 0;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {

        Car auto = new Car();
        auto.drive();
        auto.stop();

        if (auto.isDriving == true) {
            if (auto.isDriving == false)
                auto.tripsSinceMaintenance = auto.tripsSinceMaintenance + 1;
        }

        if (auto.tripsSinceMaintenance > 100)
            auto.needsMaintenance = true;
        System.out.println("Drive: " + auto.getDrive());
        System.out.println("trip: " + auto.getTRIP());
    }
}

What i want to do is whenever the attribute isDriving goes from true to false the tripsSinceMaintenance should increase by 1 and also when tripsSinceMaintenance is greater than 100 needsMaintenance should become true

here i expected trips to be 1 but the result is the following:

Drive: false trip: 0

i have tried this.isDriving==true; and basicaly wherever auto is inside the if statement i put this but the following error appears

non static variable cannot be referenced from static context

help me please!

Aucun commentaire:

Enregistrer un commentaire