mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-05-15 17:58:44 +00:00
[Sync Iteration] java/need-for-speed/1
This commit is contained in:
parent
f80fbca12e
commit
ac56aaf992
@ -0,0 +1,47 @@
|
||||
class NeedForSpeed {
|
||||
private int speed;
|
||||
private int batteryDrain;
|
||||
private int metersDriven;
|
||||
private int battery;
|
||||
|
||||
NeedForSpeed(int speed, int batteryDrain) {
|
||||
this.speed = speed;
|
||||
this.batteryDrain = batteryDrain;
|
||||
this.metersDriven = 0;
|
||||
this.battery = 100;
|
||||
}
|
||||
|
||||
public boolean batteryDrained() {
|
||||
return battery < batteryDrain;
|
||||
}
|
||||
|
||||
public int distanceDriven() {
|
||||
return metersDriven;
|
||||
}
|
||||
|
||||
public void drive() {
|
||||
if (battery - batteryDrain < 0) {
|
||||
return;
|
||||
}
|
||||
battery -= batteryDrain;
|
||||
metersDriven += speed;
|
||||
}
|
||||
|
||||
public static NeedForSpeed nitro() {
|
||||
return new NeedForSpeed(50 ,4);
|
||||
}
|
||||
}
|
||||
|
||||
class RaceTrack {
|
||||
private int distance;
|
||||
RaceTrack(int distance) {
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
public boolean canFinishRace(NeedForSpeed car) {
|
||||
while (!car.batteryDrained()) {
|
||||
car.drive();
|
||||
}
|
||||
return car.distanceDriven() >= distance;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user