enumerated to enums + Vending Machine Fix
This commit is contained in:
parent
8f5f2eeb7e
commit
ddb18c645d
@ -1,39 +0,0 @@
|
|||||||
<?xml version="1.0" ?>
|
|
||||||
|
|
||||||
<project default="run">
|
|
||||||
<property name="chapter" value="enumerated"/>
|
|
||||||
<property name="excludedfiles" value="VendingMachine.java"/>
|
|
||||||
<import file="../Ant-Common.xml"/>
|
|
||||||
<import file="../Ant-Clean.xml"/>
|
|
||||||
|
|
||||||
<target name="run" description="Compile and run" depends="build">
|
|
||||||
<jrun cls="BigEnumSet" />
|
|
||||||
<jrun cls="enumerated.Burrito" dirpath="../enumerated" />
|
|
||||||
<jrun cls="CarWash" />
|
|
||||||
<jrun cls="ConstantSpecificMethod" />
|
|
||||||
<jrun cls="EnumClass" />
|
|
||||||
<jrun cls="enumerated.EnumMaps" dirpath="../enumerated" />
|
|
||||||
<jrun cls="enumerated.EnumSets" dirpath="../enumerated" />
|
|
||||||
<jrun cls="NonEnum" />
|
|
||||||
<jrun cls="OverrideConstantSpecific" />
|
|
||||||
<jrun cls="OzWitch" />
|
|
||||||
<jrun cls="PostOffice" />
|
|
||||||
<jrun cls="RandomTest" />
|
|
||||||
<jrun cls="Reflection" />
|
|
||||||
<jrun cls="enumerated.RoShamBo1" dirpath="../enumerated" />
|
|
||||||
<jrun cls="enumerated.RoShamBo2" dirpath="../enumerated" />
|
|
||||||
<jrun cls="enumerated.RoShamBo3" dirpath="../enumerated" />
|
|
||||||
<jrun cls="enumerated.RoShamBo4" dirpath="../enumerated" />
|
|
||||||
<jrun cls="enumerated.RoShamBo5" dirpath="../enumerated" />
|
|
||||||
<jrun cls="enumerated.RoShamBo6" dirpath="../enumerated" />
|
|
||||||
<jrun cls="SecurityCategory" />
|
|
||||||
<jrun cls="SpaceShip" />
|
|
||||||
<jrun cls="TrafficLight" />
|
|
||||||
<jrun cls="UpcastEnum" />
|
|
||||||
<jrun cls="enumerated.cartoons.EnumImplementation" dirpath="../enumerated/cartoons" />
|
|
||||||
<jrun cls="enumerated.menu.Meal" dirpath="../enumerated/menu" />
|
|
||||||
<jrun cls="enumerated.menu.Meal2" dirpath="../enumerated/menu" />
|
|
||||||
<jrun cls="enumerated.menu.TypeOfFood" dirpath="../enumerated/menu" />
|
|
||||||
</target>
|
|
||||||
|
|
||||||
</project>
|
|
@ -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<? extends Food> kind) {
|
|
||||||
values = kind.getEnumConstants();
|
|
||||||
}
|
|
||||||
public Food randomSelection() {
|
|
||||||
return Enums.random(values);
|
|
||||||
}
|
|
||||||
} ///:~
|
|
@ -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;
|
|
||||||
}
|
|
||||||
} ///:~
|
|
@ -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
|
|
||||||
---
|
|
||||||
*///:~
|
|
@ -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<? 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 *///:~
|
|
@ -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;
|
|
||||||
}
|
|
||||||
} ///:~
|
|
@ -1,6 +1,6 @@
|
|||||||
//: enumerated/AlarmPoints.java
|
//: enums/AlarmPoints.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
package enumerated;
|
package enums;
|
||||||
public enum AlarmPoints {
|
public enum AlarmPoints {
|
||||||
STAIR1, STAIR2, LOBBY, OFFICE1, OFFICE2, OFFICE3,
|
STAIR1, STAIR2, LOBBY, OFFICE1, OFFICE2, OFFICE3,
|
||||||
OFFICE4, BATHROOM, UTILITY, KITCHEN
|
OFFICE4, BATHROOM, UTILITY, KITCHEN
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/BigEnumSet.java
|
//: enums/BigEnumSet.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
@ -1,7 +1,7 @@
|
|||||||
//: enumerated/Burrito.java
|
//: enums/Burrito.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
package enumerated;
|
package enums;
|
||||||
import static enumerated.Spiciness.*;
|
import static enums.Spiciness.*;
|
||||||
|
|
||||||
public class Burrito {
|
public class Burrito {
|
||||||
Spiciness degree;
|
Spiciness degree;
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/CarWash.java
|
//: enums/CarWash.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import static net.mindview.util.Print.*;
|
import static net.mindview.util.Print.*;
|
@ -1,7 +1,7 @@
|
|||||||
//: enumerated/Competitor.java
|
//: enums/Competitor.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// Switching one enum on another.
|
// Switching one enum on another.
|
||||||
package enumerated;
|
package enums;
|
||||||
|
|
||||||
public interface Competitor<T extends Competitor<T>> {
|
public interface Competitor<T extends Competitor<T>> {
|
||||||
Outcome compete(T competitor);
|
Outcome compete(T competitor);
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/ConstantSpecificMethod.java
|
//: enums/ConstantSpecificMethod.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.text.*;
|
import java.text.*;
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/EnumClass.java
|
//: enums/EnumClass.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// Capabilities of the Enum class
|
// Capabilities of the Enum class
|
||||||
import static net.mindview.util.Print.*;
|
import static net.mindview.util.Print.*;
|
@ -1,9 +1,9 @@
|
|||||||
//: enumerated/EnumMaps.java
|
//: enums/EnumMaps.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// Basics of EnumMaps.
|
// Basics of EnumMaps.
|
||||||
package enumerated;
|
package enums;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import static enumerated.AlarmPoints.*;
|
import static enums.AlarmPoints.*;
|
||||||
import static net.mindview.util.Print.*;
|
import static net.mindview.util.Print.*;
|
||||||
|
|
||||||
interface Command { void action(); }
|
interface Command { void action(); }
|
@ -1,9 +1,9 @@
|
|||||||
//: enumerated/EnumSets.java
|
//: enums/EnumSets.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// Operations on EnumSets
|
// Operations on EnumSets
|
||||||
package enumerated;
|
package enums;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import static enumerated.AlarmPoints.*;
|
import static enums.AlarmPoints.*;
|
||||||
import static net.mindview.util.Print.*;
|
import static net.mindview.util.Print.*;
|
||||||
|
|
||||||
public class EnumSets {
|
public class EnumSets {
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/Input.java
|
//: enums/Input.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/NonEnum.java
|
//: enums/NonEnum.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
|
|
||||||
public class NonEnum {
|
public class NonEnum {
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/NotClasses.java
|
//: enums/NotClasses.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// {Exec: javap -c LikeClasses}
|
// {Exec: javap -c LikeClasses}
|
||||||
import static net.mindview.util.Print.*;
|
import static net.mindview.util.Print.*;
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/Outcome.java
|
//: enums/Outcome.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
package enumerated;
|
package enums;
|
||||||
public enum Outcome { WIN, LOSE, DRAW } ///:~
|
public enum Outcome { WIN, LOSE, DRAW } ///:~
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/OverrideConstantSpecific.java
|
//: enums/OverrideConstantSpecific.java
|
||||||
// Š2015 MindView LLC: see Copyright.txt
|
// Š2015 MindView LLC: see Copyright.txt
|
||||||
import static net.mindview.util.Print.*;
|
import static net.mindview.util.Print.*;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/OzWitch.java
|
//: enums/OzWitch.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// The witches in the land of Oz.
|
// The witches in the land of Oz.
|
||||||
import static net.mindview.util.Print.*;
|
import static net.mindview.util.Print.*;
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/PostOffice.java
|
//: enums/PostOffice.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// Modeling a post office.
|
// Modeling a post office.
|
||||||
import java.util.*;
|
import java.util.*;
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/RandomTest.java
|
//: enums/RandomTest.java
|
||||||
// Š2015 MindView LLC: see Copyright.txt
|
// Š2015 MindView LLC: see Copyright.txt
|
||||||
import net.mindview.util.*;
|
import net.mindview.util.*;
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/Reflection.java
|
//: enums/Reflection.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// Analyzing enums using reflection.
|
// Analyzing enums using reflection.
|
||||||
import java.lang.reflect.*;
|
import java.lang.reflect.*;
|
@ -1,7 +1,7 @@
|
|||||||
//: enumerated/RoShamBo.java
|
//: enums/RoShamBo.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// Common tools for RoShamBo examples.
|
// Common tools for RoShamBo examples.
|
||||||
package enumerated;
|
package enums;
|
||||||
import net.mindview.util.*;
|
import net.mindview.util.*;
|
||||||
|
|
||||||
public class RoShamBo {
|
public class RoShamBo {
|
@ -1,9 +1,9 @@
|
|||||||
//: enumerated/RoShamBo1.java
|
//: enums/RoShamBo1.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// Demonstration of multiple dispatching.
|
// Demonstration of multiple dispatching.
|
||||||
package enumerated;
|
package enums;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import static enumerated.Outcome.*;
|
import static enums.Outcome.*;
|
||||||
|
|
||||||
interface Item {
|
interface Item {
|
||||||
Outcome compete(Item it);
|
Outcome compete(Item it);
|
@ -1,8 +1,8 @@
|
|||||||
//: enumerated/RoShamBo2.java
|
//: enums/RoShamBo2.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// Switching one enum on another.
|
// Switching one enum on another.
|
||||||
package enumerated;
|
package enums;
|
||||||
import static enumerated.Outcome.*;
|
import static enums.Outcome.*;
|
||||||
|
|
||||||
public enum RoShamBo2 implements Competitor<RoShamBo2> {
|
public enum RoShamBo2 implements Competitor<RoShamBo2> {
|
||||||
PAPER(DRAW, LOSE, WIN),
|
PAPER(DRAW, LOSE, WIN),
|
@ -1,8 +1,8 @@
|
|||||||
//: enumerated/RoShamBo3.java
|
//: enums/RoShamBo3.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// Using constant-specific methods.
|
// Using constant-specific methods.
|
||||||
package enumerated;
|
package enums;
|
||||||
import static enumerated.Outcome.*;
|
import static enums.Outcome.*;
|
||||||
|
|
||||||
public enum RoShamBo3 implements Competitor<RoShamBo3> {
|
public enum RoShamBo3 implements Competitor<RoShamBo3> {
|
||||||
PAPER {
|
PAPER {
|
@ -1,6 +1,6 @@
|
|||||||
//: enumerated/RoShamBo4.java
|
//: enums/RoShamBo4.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
package enumerated;
|
package enums;
|
||||||
|
|
||||||
public enum RoShamBo4 implements Competitor<RoShamBo4> {
|
public enum RoShamBo4 implements Competitor<RoShamBo4> {
|
||||||
ROCK {
|
ROCK {
|
@ -1,9 +1,9 @@
|
|||||||
//: enumerated/RoShamBo5.java
|
//: enums/RoShamBo5.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// Multiple dispatching using an EnumMap of EnumMaps.
|
// Multiple dispatching using an EnumMap of EnumMaps.
|
||||||
package enumerated;
|
package enums;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import static enumerated.Outcome.*;
|
import static enums.Outcome.*;
|
||||||
|
|
||||||
enum RoShamBo5 implements Competitor<RoShamBo5> {
|
enum RoShamBo5 implements Competitor<RoShamBo5> {
|
||||||
PAPER, SCISSORS, ROCK;
|
PAPER, SCISSORS, ROCK;
|
@ -1,8 +1,8 @@
|
|||||||
//: enumerated/RoShamBo6.java
|
//: enums/RoShamBo6.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// Enums using "tables" instead of multiple dispatch.
|
// Enums using "tables" instead of multiple dispatch.
|
||||||
package enumerated;
|
package enums;
|
||||||
import static enumerated.Outcome.*;
|
import static enums.Outcome.*;
|
||||||
|
|
||||||
enum RoShamBo6 implements Competitor<RoShamBo6> {
|
enum RoShamBo6 implements Competitor<RoShamBo6> {
|
||||||
PAPER, SCISSORS, ROCK;
|
PAPER, SCISSORS, ROCK;
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/SecurityCategory.java
|
//: enums/SecurityCategory.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// More succinct subcategorization of enums.
|
// More succinct subcategorization of enums.
|
||||||
import net.mindview.util.*;
|
import net.mindview.util.*;
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/SpaceShip.java
|
//: enums/SpaceShip.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
public enum SpaceShip {
|
public enum SpaceShip {
|
||||||
SCOUT, CARGO, TRANSPORT, CRUISER, BATTLESHIP, MOTHERSHIP;
|
SCOUT, CARGO, TRANSPORT, CRUISER, BATTLESHIP, MOTHERSHIP;
|
@ -1,6 +1,6 @@
|
|||||||
//: enumerated/Spiciness.java
|
//: enums/Spiciness.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
package enumerated;
|
package enums;
|
||||||
|
|
||||||
public enum Spiciness {
|
public enum Spiciness {
|
||||||
NOT, MILD, MEDIUM, HOT, FLAMING
|
NOT, MILD, MEDIUM, HOT, FLAMING
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/TrafficLight.java
|
//: enums/TrafficLight.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// Enums in switch statements.
|
// Enums in switch statements.
|
||||||
import static net.mindview.util.Print.*;
|
import static net.mindview.util.Print.*;
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/UpcastEnum.java
|
//: enums/UpcastEnum.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// No values() method if you upcast an enum
|
// No values() method if you upcast an enum
|
||||||
|
|
@ -1,16 +1,17 @@
|
|||||||
//: enumerated/VendingMachine.java
|
//: enums/VendingMachine.java
|
||||||
// ©2015 MindView LLC: see Copyright.txt
|
// ©2015 MindView LLC: see Copyright.txt
|
||||||
// {CompileTimeError} Not working in Java 8
|
|
||||||
// {Args: VendingMachineInput.txt}
|
// {Args: VendingMachineInput.txt}
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import net.mindview.util.*;
|
import net.mindview.util.*;
|
||||||
import static net.mindview.util.Print.*;
|
import static net.mindview.util.Print.*;
|
||||||
|
|
||||||
enum Category {
|
enum Category {
|
||||||
MONEY(NICKEL, DIME, QUARTER, DOLLAR),
|
MONEY(Input.NICKEL, Input.DIME,
|
||||||
ITEM_SELECTION(TOOTHPASTE, CHIPS, SODA, SOAP),
|
Input.QUARTER, Input.DOLLAR),
|
||||||
QUIT_TRANSACTION(ABORT_TRANSACTION),
|
ITEM_SELECTION(Input.TOOTHPASTE, Input.CHIPS,
|
||||||
SHUT_DOWN(STOP);
|
Input.SODA, Input.SOAP),
|
||||||
|
QUIT_TRANSACTION(Input.ABORT_TRANSACTION),
|
||||||
|
SHUT_DOWN(Input.STOP);
|
||||||
private Input[] values;
|
private Input[] values;
|
||||||
Category(Input... types) { values = types; }
|
Category(Input... types) { values = types; }
|
||||||
private static EnumMap<Input,Category> categories =
|
private static EnumMap<Input,Category> categories =
|
||||||
@ -126,7 +127,8 @@ class RandomInputGenerator implements Generator<Input> {
|
|||||||
class FileInputGenerator implements Generator<Input> {
|
class FileInputGenerator implements Generator<Input> {
|
||||||
private Iterator<String> input;
|
private Iterator<String> input;
|
||||||
public FileInputGenerator(String fileName) {
|
public FileInputGenerator(String fileName) {
|
||||||
input = new TextFile(fileName, ";").iterator();
|
// Skip the comment line in the input file:
|
||||||
|
input = new TextFile(fileName, ";").listIterator(1);
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public Input next() {
|
public Input next() {
|
@ -1,4 +1,4 @@
|
|||||||
//: enumerated/VendingMachineInput.txt
|
//: enums/VendingMachineInput.txt
|
||||||
QUARTER; QUARTER; QUARTER; CHIPS;
|
QUARTER; QUARTER; QUARTER; CHIPS;
|
||||||
DOLLAR; DOLLAR; TOOTHPASTE;
|
DOLLAR; DOLLAR; TOOTHPASTE;
|
||||||
QUARTER; DIME; ABORT_TRANSACTION;
|
QUARTER; DIME; ABORT_TRANSACTION;
|
40
enums/build.xml
Normal file
40
enums/build.xml
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?xml version="1.0" ?>
|
||||||
|
|
||||||
|
<project default="run">
|
||||||
|
<property name="chapter" value="enums"/>
|
||||||
|
<property name="excludedfiles" value=""/>
|
||||||
|
<import file="../Ant-Common.xml"/>
|
||||||
|
<import file="../Ant-Clean.xml"/>
|
||||||
|
|
||||||
|
<target name="run" description="Compile and run" depends="build">
|
||||||
|
<jrun cls="BigEnumSet" />
|
||||||
|
<jrun cls="enums.Burrito" dirpath="../enums" />
|
||||||
|
<jrun cls="CarWash" />
|
||||||
|
<jrun cls="ConstantSpecificMethod" />
|
||||||
|
<jrun cls="EnumClass" />
|
||||||
|
<jrun cls="enums.EnumMaps" dirpath="../enums" />
|
||||||
|
<jrun cls="enums.EnumSets" dirpath="../enums" />
|
||||||
|
<jrun cls="NonEnum" />
|
||||||
|
<jrun cls="OverrideConstantSpecific" />
|
||||||
|
<jrun cls="OzWitch" />
|
||||||
|
<jrun cls="PostOffice" />
|
||||||
|
<jrun cls="RandomTest" />
|
||||||
|
<jrun cls="Reflection" />
|
||||||
|
<jrun cls="enums.RoShamBo1" dirpath="../enums" />
|
||||||
|
<jrun cls="enums.RoShamBo2" dirpath="../enums" />
|
||||||
|
<jrun cls="enums.RoShamBo3" dirpath="../enums" />
|
||||||
|
<jrun cls="enums.RoShamBo4" dirpath="../enums" />
|
||||||
|
<jrun cls="enums.RoShamBo5" dirpath="../enums" />
|
||||||
|
<jrun cls="enums.RoShamBo6" dirpath="../enums" />
|
||||||
|
<jrun cls="SecurityCategory" />
|
||||||
|
<jrun cls="SpaceShip" />
|
||||||
|
<jrun cls="TrafficLight" />
|
||||||
|
<jrun cls="UpcastEnum" />
|
||||||
|
<jrun cls="VendingMachine" arguments="VendingMachineInput.txt" />
|
||||||
|
<jrun cls="enums.cartoons.EnumImplementation" dirpath="../enums/cartoons" />
|
||||||
|
<jrun cls="enums.menu.Meal" dirpath="../enums/menu" />
|
||||||
|
<jrun cls="enums.menu.Meal2" dirpath="../enums/menu" />
|
||||||
|
<jrun cls="enums.menu.TypeOfFood" dirpath="../enums/menu" />
|
||||||
|
</target>
|
||||||
|
|
||||||
|
</project>
|
@ -1,7 +1,7 @@
|
|||||||
//: enumerated/cartoons/EnumImplementation.java
|
//: enums/cartoons/EnumImplementation.java
|
||||||
// Š2015 MindView LLC: see Copyright.txt
|
// Š2015 MindView LLC: see Copyright.txt
|
||||||
// An enum can implement an interface
|
// An enum can implement an interface
|
||||||
package enumerated.cartoons;
|
package enums.cartoons;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import net.mindview.util.*;
|
import net.mindview.util.*;
|
||||||
|
|
||||||
@ -28,3 +28,4 @@ public class EnumImplementation {
|
|||||||
} /* Output:
|
} /* Output:
|
||||||
BOB, PUNCHY, BOB, SPANKY, NUTTY, PUNCHY, SLAPPY, NUTTY, NUTTY, SLAPPY,
|
BOB, PUNCHY, BOB, SPANKY, NUTTY, PUNCHY, SLAPPY, NUTTY, NUTTY, SLAPPY,
|
||||||
*///:~
|
*///:~
|
||||||
|
|
139
enums/menu
Normal file
139
enums/menu
Normal file
@ -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<? extends Food> 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<? 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
|
||||||
|
// ©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;
|
||||||
|
}
|
||||||
|
} ///:~
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user