139 lines
3.0 KiB
Plaintext
139 lines
3.0 KiB
Plaintext
|
//: enums/menu/Course.java
|
|||
|
// <20>2015 MindView LLC: see Copyright.txt
|
|||
|
package enums.menu;
|
|||
|
import net.mindview.util.*;
|
|||
|
|
|||
|
public enum Course {
|
|||
|
APPETIZER(Food.Appetizer.class),
|
|||
|
MAINCOURSE(Food.MainCourse.class),
|
|||
|
DESSERT(Food.Dessert.class),
|
|||
|
COFFEE(Food.Coffee.class);
|
|||
|
private Food[] values;
|
|||
|
private Course(Class<? extends Food> kind) {
|
|||
|
values = kind.getEnumConstants();
|
|||
|
}
|
|||
|
public Food randomSelection() {
|
|||
|
return Enums.random(values);
|
|||
|
}
|
|||
|
} ///:~
|
|||
|
//: enums/menu/Food.java
|
|||
|
// <20>2015 MindView LLC: see Copyright.txt
|
|||
|
// Subcategorization of enums within interfaces.
|
|||
|
package enums.menu;
|
|||
|
|
|||
|
public interface Food {
|
|||
|
enum Appetizer implements Food {
|
|||
|
SALAD, SOUP, SPRING_ROLLS;
|
|||
|
}
|
|||
|
enum MainCourse implements Food {
|
|||
|
LASAGNE, BURRITO, PAD_THAI,
|
|||
|
LENTILS, HUMMOUS, VINDALOO;
|
|||
|
}
|
|||
|
enum Dessert implements Food {
|
|||
|
TIRAMISU, GELATO, BLACK_FOREST_CAKE,
|
|||
|
FRUIT, CREME_CARAMEL;
|
|||
|
}
|
|||
|
enum Coffee implements Food {
|
|||
|
BLACK_COFFEE, DECAF_COFFEE, ESPRESSO,
|
|||
|
LATTE, CAPPUCCINO, TEA, HERB_TEA;
|
|||
|
}
|
|||
|
} ///:~
|
|||
|
//: enums/menu/Meal.java
|
|||
|
// <20>2015 MindView LLC: see Copyright.txt
|
|||
|
package enums.menu;
|
|||
|
|
|||
|
public class Meal {
|
|||
|
public static void main(String[] args) {
|
|||
|
for(int i = 0; i < 5; i++) {
|
|||
|
for(Course course : Course.values()) {
|
|||
|
Food food = course.randomSelection();
|
|||
|
System.out.println(food);
|
|||
|
}
|
|||
|
System.out.println("---");
|
|||
|
}
|
|||
|
}
|
|||
|
} /* Output:
|
|||
|
SPRING_ROLLS
|
|||
|
VINDALOO
|
|||
|
FRUIT
|
|||
|
DECAF_COFFEE
|
|||
|
---
|
|||
|
SOUP
|
|||
|
VINDALOO
|
|||
|
FRUIT
|
|||
|
TEA
|
|||
|
---
|
|||
|
SALAD
|
|||
|
BURRITO
|
|||
|
FRUIT
|
|||
|
TEA
|
|||
|
---
|
|||
|
SALAD
|
|||
|
BURRITO
|
|||
|
CREME_CARAMEL
|
|||
|
LATTE
|
|||
|
---
|
|||
|
SOUP
|
|||
|
BURRITO
|
|||
|
TIRAMISU
|
|||
|
ESPRESSO
|
|||
|
---
|
|||
|
*///:~
|
|||
|
//: enums/menu/Meal2.java
|
|||
|
// <20>2015 MindView LLC: see Copyright.txt
|
|||
|
package enums.menu;
|
|||
|
import net.mindview.util.*;
|
|||
|
|
|||
|
public enum Meal2 {
|
|||
|
APPETIZER(Food.Appetizer.class),
|
|||
|
MAINCOURSE(Food.MainCourse.class),
|
|||
|
DESSERT(Food.Dessert.class),
|
|||
|
COFFEE(Food.Coffee.class);
|
|||
|
private Food[] values;
|
|||
|
private Meal2(Class<? extends Food> kind) {
|
|||
|
values = kind.getEnumConstants();
|
|||
|
}
|
|||
|
public interface Food {
|
|||
|
enum Appetizer implements Food {
|
|||
|
SALAD, SOUP, SPRING_ROLLS;
|
|||
|
}
|
|||
|
enum MainCourse implements Food {
|
|||
|
LASAGNE, BURRITO, PAD_THAI,
|
|||
|
LENTILS, HUMMOUS, VINDALOO;
|
|||
|
}
|
|||
|
enum Dessert implements Food {
|
|||
|
TIRAMISU, GELATO, BLACK_FOREST_CAKE,
|
|||
|
FRUIT, CREME_CARAMEL;
|
|||
|
}
|
|||
|
enum Coffee implements Food {
|
|||
|
BLACK_COFFEE, DECAF_COFFEE, ESPRESSO,
|
|||
|
LATTE, CAPPUCCINO, TEA, HERB_TEA;
|
|||
|
}
|
|||
|
}
|
|||
|
public Food randomSelection() {
|
|||
|
return Enums.random(values);
|
|||
|
}
|
|||
|
public static void main(String[] args) {
|
|||
|
for(int i = 0; i < 5; i++) {
|
|||
|
for(Meal2 meal : Meal2.values()) {
|
|||
|
Food food = meal.randomSelection();
|
|||
|
System.out.println(food);
|
|||
|
}
|
|||
|
System.out.println("---");
|
|||
|
}
|
|||
|
}
|
|||
|
} /* Same output as Meal.java *///:~
|
|||
|
//: enums/menu/TypeOfFood.java
|
|||
|
// <20>2015 MindView LLC: see Copyright.txt
|
|||
|
package enums.menu;
|
|||
|
import static enums.menu.Food.*;
|
|||
|
|
|||
|
public class TypeOfFood {
|
|||
|
public static void main(String[] args) {
|
|||
|
Food food = Appetizer.SALAD;
|
|||
|
food = MainCourse.LASAGNE;
|
|||
|
food = Dessert.GELATO;
|
|||
|
food = Coffee.CAPPUCCINO;
|
|||
|
}
|
|||
|
} ///:~
|
|||
|
|