forked from BruceEckel/OnJava8-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPizza.java
More file actions
48 lines (47 loc) · 1.56 KB
/
Pizza.java
File metadata and controls
48 lines (47 loc) · 1.56 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
// concurrent/Pizza.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.util.function.*;
import onjava.Nap;
public class Pizza {
public enum Step {
DOUGH(4), ROLLED(1), SAUCED(1), CHEESED(2),
TOPPED(5), BAKED(2), SLICED(1), BOXED(0);
int effort; // Needed to get to the next step
Step(int effort) { this.effort = effort; }
Step forward() {
if(equals(BOXED)) return BOXED;
new Nap(effort * 0.1);
return values()[ordinal() + 1];
}
}
private Step step = Step.DOUGH;
private final int id;
public Pizza(int id) { this.id = id; }
public Pizza next() {
step = step.forward();
System.out.println("Pizza " + id + ": " + step);
return this;
}
public Pizza next(Step previousStep) {
if(!step.equals(previousStep))
throw new IllegalStateException("Expected " +
previousStep + " but found " + step);
return next();
}
public Pizza roll() { return next(Step.DOUGH); }
public Pizza sauce() { return next(Step.ROLLED); }
public Pizza cheese() { return next(Step.SAUCED); }
public Pizza toppings() { return next(Step.CHEESED); }
public Pizza bake() { return next(Step.TOPPED); }
public Pizza slice() { return next(Step.BAKED); }
public Pizza box() { return next(Step.SLICED); }
public boolean complete() {
return step.equals(Step.BOXED);
}
@Override public String toString() {
return "Pizza" + id + ": " +
(step.equals(Step.BOXED)? "complete" : step);
}
}