-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathHotChocolate.java
51 lines (44 loc) · 1.76 KB
/
HotChocolate.java
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
package HotChocolate;
import java.util.concurrent.TimeUnit;
public class HotChocolate {
// creating maximum temparature for too hot
public static final double tooHot = 185;
// creating minimum tempature for too cold
public static final double tooCold = 160;
// methods must throw an error or else the error will show up as a syntax issue
// within code
public static void drinkHotChocolate(double cocoaTemp) throws TooColdException, TooHotException {
if (cocoaTemp >= tooHot) {
throw new TooHotException();
} else if (cocoaTemp <= tooCold) {
throw new TooColdException();
}
}
// main method
public static void main(String[] args) throws InterruptedException {
// Sets current temp using the double "currentCocoaTemp"
double currentCocoaTemp = 150;
boolean wrongTemp = true;
while (wrongTemp) {
// Try-Catch loop where we try to drink the hot chocolate using
// "drinkHotChocolate"
try {
drinkHotChocolate(currentCocoaTemp);
System.out.println("That cocoa was good");
wrongTemp = false;
}
// if it is too hot we send an error that states it is too hot
catch (TooHotException e1) {
System.out.println("That's Too Hot!");
currentCocoaTemp = currentCocoaTemp - 5;
}
// if it is too cold we send an error that states it is too cold
catch (TooColdException e2) {
System.out.println("That's Too Cold!");
currentCocoaTemp = currentCocoaTemp + 5;
}
TimeUnit.SECONDS.sleep(2);
}
System.out.println("And it's gone");
}
}