-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCar.java
59 lines (51 loc) · 1.75 KB
/
Car.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
52
53
54
55
56
57
58
59
import java.util.ArrayList;
import java.util.Iterator;
public class Car implements Interactive {
private ArrayList<CarPart> parts = new ArrayList<>();
/* CONSTRUCTOR */
public Car() {
this.parts.add(new GasTank(18.43f, 35));
this.parts.add(new OilTank(2));
this.parts.add(new Engine(2));
for (int t = 0; t<4; t++) {
this.parts.add(new Tire(32));
}
}
/* GETTERS */
public String getParts() { return toString(this.parts); }
public String toString(ArrayList<CarPart> partsList) {
String result = "";
for (Iterator<CarPart> iter = partsList.iterator(); iter.hasNext();) {
result += ((CarPart) iter.next()).partName + "\n";
}
return result;
}
public void status() {
for (Iterator<CarPart> iter = this.parts.iterator(); iter.hasNext();) {
iter.next().status();
}
}
public void run() {
try {
do {
float miles = getFloat("How many miles are you driving?");
OilTank oTank = null;
Engine engine = null;
for (int p = 0; p<parts.size(); p++) {
parts.get(p).function(miles);
if (parts.get(p).partName == "oil tank") { oTank = (OilTank) parts.get(p); }
if (parts.get(p).partName == "engine") { engine = (Engine) parts.get(p); }
}
// The oil tank's behavior is affected by the engine's age in years,
// which are actually real-life minutes.
oTank.setEngineAgeModifier(1 + (engine.getLifeInMinutes() / engine.getBestCondition()));
} while (getBoolean("Keep driving?"));
this.status();
} catch (CarCrashException e) {
System.out.println(e.getMessage());
System.out.println("Your car crashed! You'll have to buy a new one and start over.");
} finally {
System.out.println("Thank you for driving responsibly.");
}
}
}