/** * Example of Enums in Java. * Realistically, enum inherits from Object so anything you can do to a * class you can do to an enum. (constructors, methods, etc). However, * this example covers about 99% of the uses of Enum. Mostly, the point * is to create a time with a limited set of values, then you do things * with switch across all of the value set. * This sounds limited, but it is really useful; as below * @author gtowell * adapted from https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html * Created: Oct 2022 */ public class GTEnum { /* * The actual enum. They are almost always public. */ public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } public GTEnum() { } /** * A function to do thigns on the basis of the value of the enum * @param day the value of the enum * @return a string */ public String tellItLikeItIs(Day day) { switch (day) { case MONDAY: return "Mondays are bad."; case FRIDAY: return "Fridays are better."; case SATURDAY: case SUNDAY: return "Weekends are best."; default: return "Midweek days are so-so."; } } /** * Simple driver * @param _args UNUSED */ public static void main(String[] _args) { GTEnum gte = new GTEnum(); System.out.println(gte.tellItLikeItIs(Day.MONDAY)); System.out.println(gte.tellItLikeItIs(Day.FRIDAY)); for (Day day : Day.values()) { System.out.println(day + " " + gte.tellItLikeItIs(day)); } } }