Last active
April 14, 2020 04:03
-
-
Save SanthoshBabuMR/df9c8aaac6c53e8d4542dce1f64a5dc0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://leetcode.com/playground/otdz2h93 | |
| /* | |
| Why? | |
| - Readability | |
| - Grouping of constants under one roof | |
| - Compile Type Check | |
| - Enumertion of values | |
| - Unlike constants, we can store multiple values against a single enum type | |
| */ | |
| enum HappinessIndicator { | |
| Happy, | |
| Neutral, | |
| Dull | |
| } | |
| enum State { | |
| TN("Tamil Nadu", HappinessIndicator.Happy), | |
| AP("Andhra Pradesh", HappinessIndicator.Neutral), | |
| TG("Telangana", HappinessIndicator.Dull); | |
| State(String displayText, HappinessIndicator happinessIndicator) { | |
| this.displayText = displayText; | |
| this.happinessIndicator = happinessIndicator; | |
| } | |
| String getDisplayText() { | |
| return displayText; | |
| } | |
| HappinessIndicator getHappinessIndicator() { | |
| return happinessIndicator; | |
| } | |
| String displayText; | |
| HappinessIndicator happinessIndicator; | |
| } | |
| class Person { | |
| String name; | |
| State state; | |
| Person(String name, State state) { | |
| this.name = name; | |
| this.state = state; | |
| } | |
| } | |
| public class Main { | |
| public static void main(String[] args) { | |
| Person santhosh = new Person("Santhosh", State.TN); | |
| Person dheeraj = new Person("Dheeraj", State.TG); | |
| System.out.println(String.format("%s lives in: ", santhosh.name)); | |
| System.out.println(santhosh.state); | |
| System.out.println("Looping all values in State"); | |
| // get all values in enum | |
| State[] states = State.values(); | |
| for(State state: states) { | |
| System.out.println(String.format("%s(%s) is %s", state.getDisplayText(), state, state.getHappinessIndicator().toString())); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment