OnJava8-Examples/enums/Input.java

32 lines
926 B
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// enums/Input.java
// (c)2021 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2015-06-15 17:47:35 -07:00
import java.util.*;
public enum Input {
NICKEL(5), DIME(10), QUARTER(25), DOLLAR(100),
TOOTHPASTE(200), CHIPS(75), SODA(100), SOAP(50),
ABORT_TRANSACTION {
@Override public int amount() { // Disallow
2015-06-15 17:47:35 -07:00
throw new RuntimeException("ABORT.amount()");
}
},
STOP { // This must be the last instance.
@Override public int amount() { // Disallow
2017-01-20 21:30:44 -08:00
throw new
RuntimeException("SHUT_DOWN.amount()");
2015-06-15 17:47:35 -07:00
}
};
int value; // In cents
Input(int value) { this.value = value; }
Input() {}
int amount() { return value; }; // In cents
2017-01-20 21:30:44 -08:00
static Random rand = new Random(47);
2015-06-15 17:47:35 -07:00
public static Input randomSelection() {
// Don't include STOP:
2017-01-20 21:30:44 -08:00
return
values()[rand.nextInt(values().length - 1)];
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}