Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132930 views
License: OTHER
1
/**
2
* This class models the gears. It is restricted to derailleur gears.
3
*
4
* @author Markus Iser, Martin Thoma
5
* @version 1.0
6
*/
7
8
public class Gears {
9
public static final int MIN_FRONT_SPROCKETS = 1;
10
public static final int MAX_FRONT_SPROCKETS = 3;
11
public static final int MIN_REAR_SPROCKETS = 1;
12
public static final int MAX_REAR_SPROCKETS = 10;
13
14
private int frontSprockets;
15
private int rearSprockets;
16
17
/** Price in cents. */
18
private final int price;
19
20
Gears(int frontSprockets, int rearSprockets, int price) {
21
setSprockets(frontSprockets, rearSprockets);
22
this.price = price;
23
}
24
25
/**
26
* Sets the sprocket numbers.
27
* Uses default-values if consisteny criteria are not met.
28
* @param sprockets
29
* @param rearSprockets
30
*/
31
void setSprockets(int sprockets, int rearSprockets) {
32
this.frontSprockets = sprockets;
33
this.rearSprockets = rearSprockets;
34
35
if (!(this.frontSprockets >= 1)) { // A.1
36
this.frontSprockets = 1;
37
} else if (!(this.frontSprockets < 4)) { // A.2
38
this.frontSprockets = 3;
39
}
40
41
// B.1, B.2
42
if (this.rearSprockets < 1 || this.rearSprockets > 9) {
43
this.rearSprockets = this.frontSprockets * 3;
44
}
45
46
if (this.rearSprockets < this.frontSprockets) { // C.1
47
this.rearSprockets = this.frontSprockets;
48
} else if (this.rearSprockets > 3 * this.frontSprockets) { // C.2
49
this.rearSprockets = 3 * this.frontSprockets;
50
}
51
}
52
53
/**
54
* @return the number of gears as the number of sprocket-combinations
55
*/
56
int getNumberOfGears() {
57
return frontSprockets * rearSprockets;
58
}
59
60
/**
61
* @return the price of the gears
62
*/
63
int getPrice() {
64
return price;
65
}
66
}
67
68
69