Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132948 views
License: OTHER
1
/**
2
* A bike model in Java.
3
*
4
* @author Markus Iser, Martin Thoma
5
* @version 1.0
6
*/
7
8
public class Bike {
9
private final Gears gears;
10
private final Wheels wheels;
11
12
enum Material {
13
ALU, STEEL, TITAN
14
}
15
16
private final Material material;
17
18
private final String modelId;
19
20
private final boolean hasBell;
21
private final boolean hasLights;
22
23
/** Price of the bike in cents. */
24
private int price;
25
26
Bike(String modelId) {
27
this.gears = null;
28
this.wheels = null;
29
this.material = Material.ALU;
30
this.modelId = modelId;
31
this.hasBell = true;
32
this.hasLights = true;
33
}
34
35
Bike(Gears gears, Wheels wheels, Material material,
36
String modelId, boolean bell, boolean lights) {
37
this.gears = gears;
38
this.wheels = wheels;
39
this.material = material;
40
switch (material) {
41
case ALU:
42
price = 20000;
43
break;
44
case STEEL:
45
price = 30000;
46
break;
47
case TITAN:
48
price = 40000;
49
break;
50
}
51
this.modelId = modelId;
52
this.hasBell = bell;
53
this.hasLights = lights;
54
}
55
56
/**
57
* Check if the bike is legal for usage on streets.
58
*
59
* @return {@code true} if the bike has a bell and has lights
60
*/
61
public boolean isStreetLegal() {
62
return hasBell && hasLights;
63
}
64
65
/**
66
* Get the price of the bike.
67
*
68
* @return the sum of the bike's base-price and the price of the wheels and
69
* gears
70
*/
71
public int getPrice() {
72
return price + gears.getPrice() + wheels.getPrice();
73
}
74
75
/**
76
* @return the material of this bike
77
*/
78
public Material getMaterial() {
79
return this.material;
80
}
81
82
/**
83
* @return the model id of this bike
84
*/
85
public String getModelId() {
86
return this.modelId;
87
}
88
89
/*
90
* (non-Javadoc)
91
*
92
* @see java.lang.Object#toString()
93
*/
94
@Override
95
public String toString() {
96
return "Bike[" + modelId + "]";
97
}
98
}
99