-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCruiseControl.java
More file actions
94 lines (60 loc) · 1.88 KB
/
CruiseControl.java
File metadata and controls
94 lines (60 loc) · 1.88 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
class CruiseControl {
static Boolean cruiseControlSet = false;
static int speed = 0;
static final int MAX_SPEED = 50;
static int warningSpeed = 30;
public static void main(String[] args) {
toggleCruiseControl();
accelerate();
}
public static void toggleCruiseControl() {
cruiseControlSet = !cruiseControlSet;
if( !cruiseControlSet ) {
System.out.println("Cruise Control Disabled.");
speed = 0;
}
else {
System.out.println("Cruise Control Enabled");
}
}
public static void accelerate() {
if( cruiseControlSet && ( speed < MAX_SPEED )) {
speed += 5;
System.out.println("Accelerated 5mph");
reportSpeed();
}
if( exceedsWarningThreshold() ) {
alert( false );
}
}
public static void decelerate() {
if(cruiseControlSet && (speed > 0 )) {
speed -= 5;
System.out.println( "Decelerated 5mph" );
reportSpeed();
}
if( exceedsWarningThreshold() ) {
alert(true);
}
}
public static void reportSpeed () {
System.out.println("Current speed is:\t" + speed + "mph");
}
public static void alert( Boolean isDecelerating) {
if( isDecelerating ) {
System.out.println("Warning, current speed still exceeds warning threshold.");
}
else {
System.out.println("Warning, current speed exceeds warning threshold.");
}
System.out.println("Your speed:\t\t" + speed + "mph/" + warningSpeed + "mph");
}
public static Boolean exceedsWarningThreshold() {
if( speed > warningSpeed ) {
return true;
}
else {
return false;
}
}
}