diff --git a/enumerated/build.xml b/enumerated/build.xml deleted file mode 100644 index ee6d63b8..00000000 --- a/enumerated/build.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/enumerated/menu/Course.java b/enumerated/menu/Course.java deleted file mode 100644 index 0bf64f57..00000000 --- a/enumerated/menu/Course.java +++ /dev/null @@ -1,18 +0,0 @@ -//: enumerated/menu/Course.java -// ©2015 MindView LLC: see Copyright.txt -package enumerated.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 kind) { - values = kind.getEnumConstants(); - } - public Food randomSelection() { - return Enums.random(values); - } -} ///:~ diff --git a/enumerated/menu/Food.java b/enumerated/menu/Food.java deleted file mode 100644 index 22a82ad0..00000000 --- a/enumerated/menu/Food.java +++ /dev/null @@ -1,22 +0,0 @@ -//: enumerated/menu/Food.java -// ©2015 MindView LLC: see Copyright.txt -// Subcategorization of enums within interfaces. -package enumerated.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; - } -} ///:~ diff --git a/enumerated/menu/Meal.java b/enumerated/menu/Meal.java deleted file mode 100644 index 0effda88..00000000 --- a/enumerated/menu/Meal.java +++ /dev/null @@ -1,41 +0,0 @@ -//: enumerated/menu/Meal.java -// ©2015 MindView LLC: see Copyright.txt -package enumerated.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 ---- -*///:~ diff --git a/enumerated/menu/Meal2.java b/enumerated/menu/Meal2.java deleted file mode 100644 index c765091c..00000000 --- a/enumerated/menu/Meal2.java +++ /dev/null @@ -1,44 +0,0 @@ -//: enumerated/menu/Meal2.java -// ©2015 MindView LLC: see Copyright.txt -package enumerated.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 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 *///:~ diff --git a/enumerated/menu/TypeOfFood.java b/enumerated/menu/TypeOfFood.java deleted file mode 100644 index 01ae0457..00000000 --- a/enumerated/menu/TypeOfFood.java +++ /dev/null @@ -1,13 +0,0 @@ -//: enumerated/menu/TypeOfFood.java -// ©2015 MindView LLC: see Copyright.txt -package enumerated.menu; -import static enumerated.menu.Food.*; - -public class TypeOfFood { - public static void main(String[] args) { - Food food = Appetizer.SALAD; - food = MainCourse.LASAGNE; - food = Dessert.GELATO; - food = Coffee.CAPPUCCINO; - } -} ///:~ diff --git a/enumerated/AlarmPoints.java b/enums/AlarmPoints.java similarity index 76% rename from enumerated/AlarmPoints.java rename to enums/AlarmPoints.java index 46bd34af..03b3bdfb 100644 --- a/enumerated/AlarmPoints.java +++ b/enums/AlarmPoints.java @@ -1,6 +1,6 @@ -//: enumerated/AlarmPoints.java +//: enums/AlarmPoints.java // ©2015 MindView LLC: see Copyright.txt -package enumerated; +package enums; public enum AlarmPoints { STAIR1, STAIR2, LOBBY, OFFICE1, OFFICE2, OFFICE3, OFFICE4, BATHROOM, UTILITY, KITCHEN diff --git a/enumerated/BigEnumSet.java b/enums/BigEnumSet.java similarity index 97% rename from enumerated/BigEnumSet.java rename to enums/BigEnumSet.java index 35e43dd1..9f113e33 100644 --- a/enumerated/BigEnumSet.java +++ b/enums/BigEnumSet.java @@ -1,4 +1,4 @@ -//: enumerated/BigEnumSet.java +//: enums/BigEnumSet.java // ©2015 MindView LLC: see Copyright.txt import java.util.*; diff --git a/enumerated/Burrito.java b/enums/Burrito.java similarity index 84% rename from enumerated/Burrito.java rename to enums/Burrito.java index 3867d59e..8793d6bd 100644 --- a/enumerated/Burrito.java +++ b/enums/Burrito.java @@ -1,7 +1,7 @@ -//: enumerated/Burrito.java +//: enums/Burrito.java // ©2015 MindView LLC: see Copyright.txt -package enumerated; -import static enumerated.Spiciness.*; +package enums; +import static enums.Spiciness.*; public class Burrito { Spiciness degree; diff --git a/enumerated/CarWash.java b/enums/CarWash.java similarity index 98% rename from enumerated/CarWash.java rename to enums/CarWash.java index 85ac9a0a..5548c376 100644 --- a/enumerated/CarWash.java +++ b/enums/CarWash.java @@ -1,4 +1,4 @@ -//: enumerated/CarWash.java +//: enums/CarWash.java // ©2015 MindView LLC: see Copyright.txt import java.util.*; import static net.mindview.util.Print.*; diff --git a/enumerated/Competitor.java b/enums/Competitor.java similarity index 77% rename from enumerated/Competitor.java rename to enums/Competitor.java index 46af5cfa..8a24bc2b 100644 --- a/enumerated/Competitor.java +++ b/enums/Competitor.java @@ -1,7 +1,7 @@ -//: enumerated/Competitor.java +//: enums/Competitor.java // ©2015 MindView LLC: see Copyright.txt // Switching one enum on another. -package enumerated; +package enums; public interface Competitor> { Outcome compete(T competitor); diff --git a/enumerated/ConstantSpecificMethod.java b/enums/ConstantSpecificMethod.java similarity index 93% rename from enumerated/ConstantSpecificMethod.java rename to enums/ConstantSpecificMethod.java index 2d90d076..e855173b 100644 --- a/enumerated/ConstantSpecificMethod.java +++ b/enums/ConstantSpecificMethod.java @@ -1,4 +1,4 @@ -//: enumerated/ConstantSpecificMethod.java +//: enums/ConstantSpecificMethod.java // ©2015 MindView LLC: see Copyright.txt import java.util.*; import java.text.*; diff --git a/enumerated/EnumClass.java b/enums/EnumClass.java similarity index 97% rename from enumerated/EnumClass.java rename to enums/EnumClass.java index 475eee94..1f60c235 100644 --- a/enumerated/EnumClass.java +++ b/enums/EnumClass.java @@ -1,4 +1,4 @@ -//: enumerated/EnumClass.java +//: enums/EnumClass.java // ©2015 MindView LLC: see Copyright.txt // Capabilities of the Enum class import static net.mindview.util.Print.*; diff --git a/enumerated/EnumMaps.java b/enums/EnumMaps.java similarity index 90% rename from enumerated/EnumMaps.java rename to enums/EnumMaps.java index 62b8f2f8..7c9f68e9 100644 --- a/enumerated/EnumMaps.java +++ b/enums/EnumMaps.java @@ -1,9 +1,9 @@ -//: enumerated/EnumMaps.java +//: enums/EnumMaps.java // ©2015 MindView LLC: see Copyright.txt // Basics of EnumMaps. -package enumerated; +package enums; import java.util.*; -import static enumerated.AlarmPoints.*; +import static enums.AlarmPoints.*; import static net.mindview.util.Print.*; interface Command { void action(); } diff --git a/enumerated/EnumSets.java b/enums/EnumSets.java similarity index 90% rename from enumerated/EnumSets.java rename to enums/EnumSets.java index 1677d771..89a4c64a 100644 --- a/enumerated/EnumSets.java +++ b/enums/EnumSets.java @@ -1,9 +1,9 @@ -//: enumerated/EnumSets.java +//: enums/EnumSets.java // ©2015 MindView LLC: see Copyright.txt // Operations on EnumSets -package enumerated; +package enums; import java.util.*; -import static enumerated.AlarmPoints.*; +import static enums.AlarmPoints.*; import static net.mindview.util.Print.*; public class EnumSets { diff --git a/enumerated/Input.java b/enums/Input.java similarity index 96% rename from enumerated/Input.java rename to enums/Input.java index 46cc4fde..f8e3a2fd 100644 --- a/enumerated/Input.java +++ b/enums/Input.java @@ -1,4 +1,4 @@ -//: enumerated/Input.java +//: enums/Input.java // ©2015 MindView LLC: see Copyright.txt import java.util.*; diff --git a/enumerated/NonEnum.java b/enums/NonEnum.java similarity index 93% rename from enumerated/NonEnum.java rename to enums/NonEnum.java index 6109b4ad..af23bac7 100644 --- a/enumerated/NonEnum.java +++ b/enums/NonEnum.java @@ -1,4 +1,4 @@ -//: enumerated/NonEnum.java +//: enums/NonEnum.java // ©2015 MindView LLC: see Copyright.txt public class NonEnum { diff --git a/enumerated/NotClasses.java b/enums/NotClasses.java similarity index 95% rename from enumerated/NotClasses.java rename to enums/NotClasses.java index d12d41b3..be45c585 100644 --- a/enumerated/NotClasses.java +++ b/enums/NotClasses.java @@ -1,4 +1,4 @@ -//: enumerated/NotClasses.java +//: enums/NotClasses.java // ©2015 MindView LLC: see Copyright.txt // {Exec: javap -c LikeClasses} import static net.mindview.util.Print.*; diff --git a/enumerated/Outcome.java b/enums/Outcome.java similarity index 64% rename from enumerated/Outcome.java rename to enums/Outcome.java index 554cb93e..ed483344 100644 --- a/enumerated/Outcome.java +++ b/enums/Outcome.java @@ -1,4 +1,4 @@ -//: enumerated/Outcome.java +//: enums/Outcome.java // ©2015 MindView LLC: see Copyright.txt -package enumerated; +package enums; public enum Outcome { WIN, LOSE, DRAW } ///:~ diff --git a/enumerated/OverrideConstantSpecific.java b/enums/OverrideConstantSpecific.java similarity index 91% rename from enumerated/OverrideConstantSpecific.java rename to enums/OverrideConstantSpecific.java index 1b21db46..9a7dd870 100644 --- a/enumerated/OverrideConstantSpecific.java +++ b/enums/OverrideConstantSpecific.java @@ -1,4 +1,4 @@ -//: enumerated/OverrideConstantSpecific.java +//: enums/OverrideConstantSpecific.java // ©2015 MindView LLC: see Copyright.txt import static net.mindview.util.Print.*; diff --git a/enumerated/OzWitch.java b/enums/OzWitch.java similarity index 97% rename from enumerated/OzWitch.java rename to enums/OzWitch.java index 13cbc634..dab24ad5 100644 --- a/enumerated/OzWitch.java +++ b/enums/OzWitch.java @@ -1,4 +1,4 @@ -//: enumerated/OzWitch.java +//: enums/OzWitch.java // ©2015 MindView LLC: see Copyright.txt // The witches in the land of Oz. import static net.mindview.util.Print.*; diff --git a/enumerated/PostOffice.java b/enums/PostOffice.java similarity index 99% rename from enumerated/PostOffice.java rename to enums/PostOffice.java index 933935d6..9942242b 100644 --- a/enumerated/PostOffice.java +++ b/enums/PostOffice.java @@ -1,4 +1,4 @@ -//: enumerated/PostOffice.java +//: enums/PostOffice.java // ©2015 MindView LLC: see Copyright.txt // Modeling a post office. import java.util.*; diff --git a/enumerated/RandomTest.java b/enums/RandomTest.java similarity index 94% rename from enumerated/RandomTest.java rename to enums/RandomTest.java index b893f666..2ed0fd56 100644 --- a/enumerated/RandomTest.java +++ b/enums/RandomTest.java @@ -1,4 +1,4 @@ -//: enumerated/RandomTest.java +//: enums/RandomTest.java // ©2015 MindView LLC: see Copyright.txt import net.mindview.util.*; diff --git a/enumerated/Reflection.java b/enums/Reflection.java similarity index 98% rename from enumerated/Reflection.java rename to enums/Reflection.java index e637f836..479039d2 100644 --- a/enumerated/Reflection.java +++ b/enums/Reflection.java @@ -1,4 +1,4 @@ -//: enumerated/Reflection.java +//: enums/Reflection.java // ©2015 MindView LLC: see Copyright.txt // Analyzing enums using reflection. import java.lang.reflect.*; diff --git a/enumerated/RoShamBo.java b/enums/RoShamBo.java similarity index 90% rename from enumerated/RoShamBo.java rename to enums/RoShamBo.java index 4bfc2500..d8f2a89f 100644 --- a/enumerated/RoShamBo.java +++ b/enums/RoShamBo.java @@ -1,7 +1,7 @@ -//: enumerated/RoShamBo.java +//: enums/RoShamBo.java // ©2015 MindView LLC: see Copyright.txt // Common tools for RoShamBo examples. -package enumerated; +package enums; import net.mindview.util.*; public class RoShamBo { diff --git a/enumerated/RoShamBo1.java b/enums/RoShamBo1.java similarity index 96% rename from enumerated/RoShamBo1.java rename to enums/RoShamBo1.java index b99f41d3..a204d34a 100644 --- a/enumerated/RoShamBo1.java +++ b/enums/RoShamBo1.java @@ -1,9 +1,9 @@ -//: enumerated/RoShamBo1.java +//: enums/RoShamBo1.java // ©2015 MindView LLC: see Copyright.txt // Demonstration of multiple dispatching. -package enumerated; +package enums; import java.util.*; -import static enumerated.Outcome.*; +import static enums.Outcome.*; interface Item { Outcome compete(Item it); diff --git a/enumerated/RoShamBo2.java b/enums/RoShamBo2.java similarity index 93% rename from enumerated/RoShamBo2.java rename to enums/RoShamBo2.java index 08cca244..250237e6 100644 --- a/enumerated/RoShamBo2.java +++ b/enums/RoShamBo2.java @@ -1,8 +1,8 @@ -//: enumerated/RoShamBo2.java +//: enums/RoShamBo2.java // ©2015 MindView LLC: see Copyright.txt // Switching one enum on another. -package enumerated; -import static enumerated.Outcome.*; +package enums; +import static enums.Outcome.*; public enum RoShamBo2 implements Competitor { PAPER(DRAW, LOSE, WIN), diff --git a/enumerated/RoShamBo3.java b/enums/RoShamBo3.java similarity index 92% rename from enumerated/RoShamBo3.java rename to enums/RoShamBo3.java index a9b01bec..59112184 100644 --- a/enumerated/RoShamBo3.java +++ b/enums/RoShamBo3.java @@ -1,8 +1,8 @@ -//: enumerated/RoShamBo3.java +//: enums/RoShamBo3.java // ©2015 MindView LLC: see Copyright.txt // Using constant-specific methods. -package enumerated; -import static enumerated.Outcome.*; +package enums; +import static enums.Outcome.*; public enum RoShamBo3 implements Competitor { PAPER { diff --git a/enumerated/RoShamBo4.java b/enums/RoShamBo4.java similarity index 94% rename from enumerated/RoShamBo4.java rename to enums/RoShamBo4.java index 11375336..5c28d56d 100644 --- a/enumerated/RoShamBo4.java +++ b/enums/RoShamBo4.java @@ -1,6 +1,6 @@ -//: enumerated/RoShamBo4.java +//: enums/RoShamBo4.java // ©2015 MindView LLC: see Copyright.txt -package enumerated; +package enums; public enum RoShamBo4 implements Competitor { ROCK { diff --git a/enumerated/RoShamBo5.java b/enums/RoShamBo5.java similarity index 92% rename from enumerated/RoShamBo5.java rename to enums/RoShamBo5.java index 5f9a04a7..4c22d5ba 100644 --- a/enumerated/RoShamBo5.java +++ b/enums/RoShamBo5.java @@ -1,9 +1,9 @@ -//: enumerated/RoShamBo5.java +//: enums/RoShamBo5.java // ©2015 MindView LLC: see Copyright.txt // Multiple dispatching using an EnumMap of EnumMaps. -package enumerated; +package enums; import java.util.*; -import static enumerated.Outcome.*; +import static enums.Outcome.*; enum RoShamBo5 implements Competitor { PAPER, SCISSORS, ROCK; diff --git a/enumerated/RoShamBo6.java b/enums/RoShamBo6.java similarity index 85% rename from enumerated/RoShamBo6.java rename to enums/RoShamBo6.java index 33f36308..a5b2cafa 100644 --- a/enumerated/RoShamBo6.java +++ b/enums/RoShamBo6.java @@ -1,8 +1,8 @@ -//: enumerated/RoShamBo6.java +//: enums/RoShamBo6.java // ©2015 MindView LLC: see Copyright.txt // Enums using "tables" instead of multiple dispatch. -package enumerated; -import static enumerated.Outcome.*; +package enums; +import static enums.Outcome.*; enum RoShamBo6 implements Competitor { PAPER, SCISSORS, ROCK; diff --git a/enumerated/SecurityCategory.java b/enums/SecurityCategory.java similarity index 96% rename from enumerated/SecurityCategory.java rename to enums/SecurityCategory.java index 22707994..87537be5 100644 --- a/enumerated/SecurityCategory.java +++ b/enums/SecurityCategory.java @@ -1,4 +1,4 @@ -//: enumerated/SecurityCategory.java +//: enums/SecurityCategory.java // ©2015 MindView LLC: see Copyright.txt // More succinct subcategorization of enums. import net.mindview.util.*; diff --git a/enumerated/SpaceShip.java b/enums/SpaceShip.java similarity index 93% rename from enumerated/SpaceShip.java rename to enums/SpaceShip.java index fd3310d5..64f12b8a 100644 --- a/enumerated/SpaceShip.java +++ b/enums/SpaceShip.java @@ -1,4 +1,4 @@ -//: enumerated/SpaceShip.java +//: enums/SpaceShip.java // ©2015 MindView LLC: see Copyright.txt public enum SpaceShip { SCOUT, CARGO, TRANSPORT, CRUISER, BATTLESHIP, MOTHERSHIP; diff --git a/enumerated/Spiciness.java b/enums/Spiciness.java similarity index 68% rename from enumerated/Spiciness.java rename to enums/Spiciness.java index 70484505..7a60201a 100644 --- a/enumerated/Spiciness.java +++ b/enums/Spiciness.java @@ -1,6 +1,6 @@ -//: enumerated/Spiciness.java +//: enums/Spiciness.java // ©2015 MindView LLC: see Copyright.txt -package enumerated; +package enums; public enum Spiciness { NOT, MILD, MEDIUM, HOT, FLAMING diff --git a/enumerated/TrafficLight.java b/enums/TrafficLight.java similarity index 96% rename from enumerated/TrafficLight.java rename to enums/TrafficLight.java index f5dfd215..303cb1b4 100644 --- a/enumerated/TrafficLight.java +++ b/enums/TrafficLight.java @@ -1,4 +1,4 @@ -//: enumerated/TrafficLight.java +//: enums/TrafficLight.java // ©2015 MindView LLC: see Copyright.txt // Enums in switch statements. import static net.mindview.util.Print.*; diff --git a/enumerated/UpcastEnum.java b/enums/UpcastEnum.java similarity index 93% rename from enumerated/UpcastEnum.java rename to enums/UpcastEnum.java index 510b320d..fada1708 100644 --- a/enumerated/UpcastEnum.java +++ b/enums/UpcastEnum.java @@ -1,4 +1,4 @@ -//: enumerated/UpcastEnum.java +//: enums/UpcastEnum.java // ©2015 MindView LLC: see Copyright.txt // No values() method if you upcast an enum diff --git a/enumerated/VendingMachine.java b/enums/VendingMachine.java similarity index 90% rename from enumerated/VendingMachine.java rename to enums/VendingMachine.java index d4f3d70a..7c9d2a23 100644 --- a/enumerated/VendingMachine.java +++ b/enums/VendingMachine.java @@ -1,16 +1,17 @@ -//: enumerated/VendingMachine.java +//: enums/VendingMachine.java // ©2015 MindView LLC: see Copyright.txt -// {CompileTimeError} Not working in Java 8 // {Args: VendingMachineInput.txt} import java.util.*; import net.mindview.util.*; import static net.mindview.util.Print.*; enum Category { - MONEY(NICKEL, DIME, QUARTER, DOLLAR), - ITEM_SELECTION(TOOTHPASTE, CHIPS, SODA, SOAP), - QUIT_TRANSACTION(ABORT_TRANSACTION), - SHUT_DOWN(STOP); + MONEY(Input.NICKEL, Input.DIME, + Input.QUARTER, Input.DOLLAR), + ITEM_SELECTION(Input.TOOTHPASTE, Input.CHIPS, + Input.SODA, Input.SOAP), + QUIT_TRANSACTION(Input.ABORT_TRANSACTION), + SHUT_DOWN(Input.STOP); private Input[] values; Category(Input... types) { values = types; } private static EnumMap categories = @@ -126,7 +127,8 @@ class RandomInputGenerator implements Generator { class FileInputGenerator implements Generator { private Iterator input; public FileInputGenerator(String fileName) { - input = new TextFile(fileName, ";").iterator(); + // Skip the comment line in the input file: + input = new TextFile(fileName, ";").listIterator(1); } @Override public Input next() { diff --git a/enumerated/VendingMachineInput.txt b/enums/VendingMachineInput.txt similarity index 81% rename from enumerated/VendingMachineInput.txt rename to enums/VendingMachineInput.txt index 6472db43..7dd1354e 100644 --- a/enumerated/VendingMachineInput.txt +++ b/enums/VendingMachineInput.txt @@ -1,4 +1,4 @@ -//: enumerated/VendingMachineInput.txt +//: enums/VendingMachineInput.txt QUARTER; QUARTER; QUARTER; CHIPS; DOLLAR; DOLLAR; TOOTHPASTE; QUARTER; DIME; ABORT_TRANSACTION; diff --git a/enums/build.xml b/enums/build.xml new file mode 100644 index 00000000..96614583 --- /dev/null +++ b/enums/build.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/enumerated/cartoons/EnumImplementation.java b/enums/cartoons similarity index 91% rename from enumerated/cartoons/EnumImplementation.java rename to enums/cartoons index a681470c..0a4a6b83 100644 --- a/enumerated/cartoons/EnumImplementation.java +++ b/enums/cartoons @@ -1,7 +1,7 @@ -//: enumerated/cartoons/EnumImplementation.java +//: enums/cartoons/EnumImplementation.java // ©2015 MindView LLC: see Copyright.txt // An enum can implement an interface -package enumerated.cartoons; +package enums.cartoons; import java.util.*; import net.mindview.util.*; @@ -28,3 +28,4 @@ public class EnumImplementation { } /* Output: BOB, PUNCHY, BOB, SPANKY, NUTTY, PUNCHY, SLAPPY, NUTTY, NUTTY, SLAPPY, *///:~ + \ No newline at end of file diff --git a/enums/menu b/enums/menu new file mode 100644 index 00000000..2a5c6d5f --- /dev/null +++ b/enums/menu @@ -0,0 +1,139 @@ +//: enums/menu/Course.java +// ©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 kind) { + values = kind.getEnumConstants(); + } + public Food randomSelection() { + return Enums.random(values); + } +} ///:~ +//: enums/menu/Food.java +// ©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 +// ©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 +// ©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 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 +// ©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; + } +} ///:~ + \ No newline at end of file