-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeedForSpeed.java
51 lines (43 loc) · 1.08 KB
/
NeedForSpeed.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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() {
if (battery <= 0) {
return true;
}
return false;
}
public int distanceDriven() {
return distanceDriven;
}
public void drive() {
if (!this.batteryDrained()) {
distanceDriven += speed;
battery -= 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 (distance > car.distanceDriven()) {
if (car.batteryDrained()) {
return false;
}
car.drive();
}
return true;
}
}