-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngine.java
47 lines (38 loc) · 1.31 KB
/
Engine.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
public class Engine extends CarPart{
private long creationTime;
private float lifeInMinutes;
/* CONSTRUCTOR */
// The engine lasts 10 years (10 real-life minutes), and as it gets
// older, the oil tank needs more frequent changes (see Car.run()
// for how they talk to each other).
public Engine(float yearsOld) {
super("engine", " years left", 10);
this.creationTime = System.currentTimeMillis();
this.lifeInMinutes = 0;
}
/* GETTERS */
public float getLifeInMinutes() { return this.lifeInMinutes; }
/* SETTERS */
// override replacePart() because lifeInMinutes isn't a default
// field of the CarPart class.
public void replacePart() {
super.replacePart();
this.lifeInMinutes = 0;
}
public void function(float milesDriven) throws CarCrashException {
super.function(milesDriven);
this.lifeInMinutes = (System.currentTimeMillis() - this.creationTime) / 60000f;
this.setCondition(this.bestCondition - lifeInMinutes);
if (this.condition <= 0) {
this.crashCar();
} else if (this.condition <= 2) {
this.status("Your engine is getting old!");
if (getBoolean("Replace?")) {
this.replacePart();
this.status("Engine replaced!");
}
} else {
this.status("You'll need a new engine in " + this.condition + " years.");
}
}
}