Skip to content

Commit d43fbd4

Browse files
committed
enumaratedtypes
1 parent 8b43272 commit d43fbd4

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

EnumaratedTypes/Inputs.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.util.Random;
2+
3+
public enum Inputs {
4+
NICKEL(5), DIME(10), QUARTER(25), DOLLAR(100),
5+
TOOTHPASTE(200), CHIPS(75), SODA(100), SOAP(50),
6+
ABORT_TRANSACTION {
7+
public int amount() { // Disallow
8+
throw new RuntimeException("ABORT.amount()");
9+
}
10+
},
11+
STOP { // This must be the last instance.
12+
public int amount() { // Disallow
13+
throw new RuntimeException("SHUT_DOWN.amount()");
14+
}
15+
};
16+
17+
int value; // In cents
18+
19+
Inputs(int value) {
20+
this.value = value;
21+
}
22+
23+
Inputs() {
24+
}
25+
26+
int amount() {
27+
return value;
28+
}; // In cents
29+
30+
static Random rand = new Random(47);
31+
32+
public static Inputs randomSelection() {
33+
// Don’t include STOP:
34+
return values()[rand.nextInt(values().length - 1)];
35+
}
36+
}

0 commit comments

Comments
 (0)