/*
 * @(#)TestEnum.java
 *
 * Summary: Demonstrate use of Enum static and instance fields.
 *
 * Copyright: (c) 2009 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.6+
 *
 * Created with: IntelliJ IDEA IDE.
 *
 * Version History:
 *  1.0 2009-03-24 - initial version
 */
package com.mindprod.example;

import static java.lang.System.out;

/**
 * Demonstrate use of Enum static and instance fields.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2009-03-24 - initial version
 * @since 2009-03-24
 */
public class TestEnum
    {
    // -------------------------- INNER CLASSES --------------------------

    /* nested static class */
    enum Animals
        {
            COW( 2 ),
            GOAT( 2 ),
            OSTRICH( 0 )
                    {
                    /**
                     OSTRICH overrides the default version of the method. */
                    int getWingCount()
                        {
                        return wc;
                        }

                    /**
                     field that only OSTRICH has */
                    final int wc = 2;
                    };

        /**
         * COW, GOAT and OSTRICH all share a single copy of this field.
         */
        static int legs = 4;

        // ------------------------------ FIELDS ------------------------------

        /**
         * even though COW, GOAT and OSTRICH all have this field, they each have their own copy.
         */
        int horns;

        // --------------------------- CONSTRUCTORS ---------------------------

        Animals( int horns )
            {
            this.horns = horns;
            // may not reference the static legs variable here
            }

        // --------------------- GETTER / SETTER METHODS ---------------------

        int getHorns()
            {
            return horns;
            }

        void setHorns( int horns )
            {
            // set horns variable, one copy for each enum constant.
            this.horns = horns;
            }

        int getLegs()
            {
            return legs;
            }

        // -------------------------- OTHER METHODS --------------------------

        /**
         * default method, overridden by OSTRICH only.
         *
         * @return number of wings this animal has.
         */
        int getWingCount()
            {
            return 0;
            }

        void setLegs( int legs )
            {
            // set shared legs variable.
            Animals.legs = legs;
            }
        }

    // --------------------------- main() method ---------------------------

    /**
     * test enum
     *
     * @param args not used
     */
    public static void main( String[] args )
        {
        // prints 2
        out.println( Animals.COW.getHorns() );
        Animals.COW.setHorns( 1 );
        // prints 1
        out.println( Animals.COW.getHorns() );
        // prints 2
        out.println( Animals.GOAT.getHorns() );

        // prints 4
        out.println( Animals.COW.getLegs() );
        Animals.COW.setLegs( 5 );
        // prints 5
        out.println( Animals.COW.getLegs() );
        // prints 5
        out.println( Animals.GOAT.getLegs() );

        //  prints 2
        out.println( Animals.OSTRICH.getWingCount() );
        //  prints 0
        out.println( Animals.GOAT.getWingCount() );
        }
    }