Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132948 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. Uses default-values if consisteny criteria are
27
* not met.
28
*
29
* @param sprockets
30
* @param rearSprockets
31
*/
32
void setSprockets(int sprockets, int rearSprockets) {
33
this.frontSprockets = sprockets;
34
this.rearSprockets = rearSprockets;
35
36
if (!(this.frontSprockets >= 1)) { // A.1
37
this.frontSprockets = 1;
38
} else if (!(this.frontSprockets < 4)) { // A.2
39
this.frontSprockets = 3;
40
}
41
42
if (this.rearSprockets < 1) { // B.1
43
this.rearSprockets = this.frontSprockets;
44
}
45
if (this.rearSprockets > 9) { // B.2
46
this.rearSprockets = this.frontSprockets * 3;
47
}
48
49
if (this.rearSprockets < this.frontSprockets) { // C.1
50
this.rearSprockets = this.frontSprockets;
51
} else if (this.rearSprockets > 3 * this.frontSprockets) { // C.2
52
this.rearSprockets = 3 * this.frontSprockets;
53
}
54
}
55
56
/**
57
* @return the number of gears as the number of sprocket-combinations
58
*/
59
int getNumberOfGears() {
60
return frontSprockets * rearSprockets;
61
}
62
63
/**
64
* @return the price of the gears
65
*/
66
int getPrice() {
67
return price;
68
}
69
}
70
71