-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCAuto.java
More file actions
135 lines (103 loc) · 2.96 KB
/
CAuto.java
File metadata and controls
135 lines (103 loc) · 2.96 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
public class CAuto implements IFahrzeug {
private static int sCarCount;
private final String mColor;
private final String mBrand;
private final int mMaxSpeed;
private final int mGears;
private final int mSerialNumber;
private int mCurrentGear = 1;
private int mCurrentSpeed = 0;
private double mPerformance; // in KW
public CAuto() {
this("Silber", "VW", 230, 5, 75);
}
public CAuto(final String color, final String brand, final int maxSpeed, final int gears, int performance) {
mColor = color;
mBrand = brand;
mMaxSpeed = maxSpeed;
mGears = gears;
mPerformance = performance;
mSerialNumber = sCarCount++;
}
public CAuto get() {
CAuto car = new CAuto(mColor, mBrand, mMaxSpeed, mGears);
car.accelerate(mCurrentSpeed);
car.shiftGears(mCurrentGear);
return car;
}
public String getColor() {
return mColor;
}
public String getBrand() {
return mBrand;
}
public int getMaxSpeed() {
return mMaxSpeed;
}
public int getGears() {
return mGears;
}
public int getCurrentSpeed() {
return mCurrentSpeed;
}
public int getCurrentGear() {
return mCurrentGear;
}
@Override
public void shiftGears(int gear) {
if (gear < -1 || gear > mGears)
return;
mCurrentGear = gear;
System.out.println(this.getClass().getName() + " shifted to gear: " + mCurrentGear);
}
public void setPerformance(int performanceInKW) {
if (mPerformance <= 0.0)
return;
mPerformance = performanceInKW;
}
public double getPerformanceKW() {
return mPerformance;
}
public double getPerformanceHP() {
return getHP(mPerformance);
}
public String getSerialNumber() {
int first = 0;
int serial = mSerialNumber;
while (serial >= 10) {
first++;
serial -= 10;
}
return "" + first + "-" + serial;
}
@Override
public void accelerate(int increment) {
if (mCurrentGear == 0)
return;
if (mCurrentGear == -1)
increment = -1 * increment;
if (mCurrentSpeed + increment > mMaxSpeed) {
mCurrentSpeed = mMaxSpeed;
return;
}
if (mCurrentSpeed + increment < 0) {
mCurrentSpeed = 0;
return;
}
mCurrentSpeed += increment;
System.out.println(this.getClass().getName() + " changed speed to " + mCurrentSpeed + "km/h");
}
@Override
public void brake(int decrement) {
accelerate(-1 * decrement);
}
public void shiftUp() {
shiftGears(mCurrentGear + 1);
}
public void shiftDown() {
shiftGears(mCurrentGear - 1);
}
public static double getHP(double performanceInKW) {
return performanceInKW * 1.36;
}
}