Monday, 2 October 2017

Enum types in Java - A Superhero version



Hello Buddies.


This one is for Enum type.




  As Java Doc states -



" An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.You should use enum types any time you need to represent a fixed set of constants. "



Ok, that sounds cool, but can we have a working Demo that can depict where can we use it further?


Surely we can, and here is one example.


We have a scenario here - 


A superhero scenario. 


Superman, who is not willing to reveal his real name, Batman, hmmm we all know when he wants and he doesn't want to reveal his real name, and Deadpool who is not invited in the party, yet trying to sneak in.

So can we cater to the requirements here -
1. Provide Superman the data hiding.
2. Provide Batman his real name revealed, when he wants.
3. Stop Deadpool from ruining the party.


Just have a glance at the code, the comments are included to narrate what's going on -



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public class EnumDemo {
    
    public static void main(String[] args) {
        
        // This is a demo to show you how you can make use of the 'Superhero'
        // Enum
        
        // By default toString() method will be converting the enum variable
        // into String and displaying it, a case similar to Superman, who is not
        // interested in revealing his ID
        String selectedHero = Superhero.SUPERMAN.name();
        System.out.println("You selected - " + selectedHero);
        String realName = Superhero.SUPERMAN.toString();
        System.out.println("Real Name - " + realName + "\n");
        
        // While with Batman, it's not always the case, remember the Spoof "Do
        // you want to know my secret identity ??" ?, Hence a separate method
        // for such scenarios ;)
        String selectedHero2 = Superhero.BATMAN.name();
        System.out.println("You selected - " + selectedHero2);
        String realName2 = Superhero.BATMAN.toStringRevealRealName();
        System.out.println("Real Name - " + realName2);
        Superhero verify = Superhero.fromValue(realName2);
        System.out.println("ID verified, it's - " + verify.name() + "\n");
        
        // Now what is the advantage of using Enum? As you can see we have kind
        // of restricted the users with a given set of Superheroes, that means
        // Deadpool is not invited to the party... Check this out -
        String uninvitedGuest = "Wade Wilson";
        verify = Superhero.fromValue(uninvitedGuest);
        try {
            System.out.println("What do we get ? " + verify.name());
            } catch (Exception e) {
            System.out.println("Wade Wilson AKA Deadpool trying to sneak in...");
            System.out.println("Entry Allowed? " + e.getMessage());
        }
    }
    
}

enum Superhero {
    BATMAN("Bruce Wayne"), SUPERMAN("Clark Kent"), IRONMAN("Tony Stark"), SPIDERMAN("Peter Parker");
    
    private String value;
    
    Superhero(String value) {
        this.value = value;
    }
    
    public static Superhero fromValue(String str) {
        for (Superhero s: Superhero.values()) {
            if (String.valueOf(s.value).equalsIgnoreCase(str)) {
                return s;
            }
        }
        return null;
    }
    
    public String toStringRevealRealName() {
        return value;
    }
    
}


Well, after this action, here is the aftermath, I mean the logs -

You selected - SUPERMAN
Real Name - SUPERMAN

You selected - BATMAN
Real Name - Bruce Wayne
ID verified, it's - BATMAN

Wade Wilson AKA Deadpool trying to sneak in...
Entry Allowed? null

For full code/ project, visit -

https://github.com/namitsharma99/EnumDemoJava.git


So that's it for now. Will meet you soon with some other interesting topics.
Happy coding!







Featured post

JAVA based project, that can be used to hit DB using JDBC, from WSO2 ESB

Hi Buddies, Here is a small project that will enable you to hit MySQL DB using WSO2 ESB - https://github.com/namitsharma99/customM...

Popular Posts