I have the following code, but the tests are not running correctly. Every time I try to run the tests, it returns a timeout error as if there were an infinite loop in my code.
class NeedForSpeed {
private int speed;
private int batteryDrain;
private int distanceDriven = 0;
private int battery = 100;
public NeedForSpeed(int speed, int batteryDrain) {
this.speed = speed;
this.batteryDrain = batteryDrain;
}
public boolean batteryDrained() {
return this.batteryDrain > this.battery;
}
public int distanceDriven() {
return this.distanceDriven;
}
public void drive() {
if(this.batteryDrain < this.battery) {
this.distanceDriven += this.speed;
this.battery -= this.batteryDrain;
}
}
public static NeedForSpeed nitro() {
return new NeedForSpeed(50, 4);
}
}
class RaceTrack {
private int distance;
public RaceTrack(int distance) {
this.distance = distance;
}
public boolean tryFinishTrack(NeedForSpeed car) {
while (car.distanceDriven() < this.distance && !car.batteryDrained()) {
car.drive();
}
return car.distanceDriven() >= this.distance;
}
}
Does anyone can help me?