OnJava8-Examples/enums/SecurityCategory.java

47 lines
1.1 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// enums/SecurityCategory.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.
2016-01-25 18:05:55 -08:00
// More succinct subcategorization of enums
import onjava.*;
2015-06-15 17:47:35 -07:00
enum SecurityCategory {
2017-01-20 21:30:44 -08:00
STOCK(Security.Stock.class),
BOND(Security.Bond.class);
2015-06-15 17:47:35 -07:00
Security[] values;
SecurityCategory(Class<? extends Security> kind) {
values = kind.getEnumConstants();
}
interface Security {
2017-01-20 21:30:44 -08:00
enum Stock implements Security {
SHORT, LONG, MARGIN
}
enum Bond implements Security {
MUNICIPAL, JUNK
}
2015-06-15 17:47:35 -07:00
}
public Security randomSelection() {
return Enums.random(values);
}
public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
SecurityCategory category =
Enums.random(SecurityCategory.class);
System.out.println(category + ": " +
category.randomSelection());
}
}
2015-09-07 11:44:36 -06:00
}
/* Output:
BOND: MUNICIPAL
BOND: MUNICIPAL
STOCK: MARGIN
2015-06-15 17:47:35 -07:00
STOCK: MARGIN
BOND: JUNK
STOCK: SHORT
2015-06-15 17:47:35 -07:00
STOCK: LONG
STOCK: LONG
BOND: MUNICIPAL
BOND: JUNK
2015-09-07 11:44:36 -06:00
*/