how to call getSeason(Months.OCTOBER);? [closed]

Solution 1:

The Answer by Alexander Ivanchenko is correct. I'll add a bit about naming, and some tips.

We already have the java.time.Month enum. No need to re-invent.

Generally, an enum should be named in the singular. For example, java.time.Month and java.time.DayOfWeek. So, Season rather than Seasons.

The java.time classes (JSR 310) established some naming conventions that have proven to be sensible and handy. For moving from the more specific to the more general, meaning we are losing information, use from. For example, java.time.YearMonth offers a static factory method from to produce a year-month value from a date object, etc. So Season#from( java.time.Month ).

And consider adding a method to produce a display name, versus the all-caps name of the enum object. For a small specific project, hard-code the result of such a method, for a specific language. If re-using this class, consider adding localization via a passed Locale argument as seen on the java.time classes. For example, java.time.Month#getDisplayName( TextStyle , Locale ).

enum Season {
        SPRING ( "Spring" ), 
        SUMMER ( "Summer" ), 
        WINTER ( "Winter" ), 
        AUTUMN ( "Autumn" );
    
    private String displayName ;
    
    Season ( String displayName ) {
        this.displayName = displayName ;
    }
    
    public String getDisplayName() { return this.displayName ; }
    
    public Season from( java.time.Month month ) {
        Objects.requireNonNull( month ) ;
        return switch ( month ) {
            case SEPTEMBER, OCTOBER, NOVEMBER -> Season.SPRING;
            case DECEMBER, JANUARY, FEBRUARY -> Season.WINTER;
            case JUNE, JULY, AUGUST -> Season.SUMMER;
            case MARCH, APRIL, MAY -> Season.AUTUMN;
        };
    }
}

Usage:

System.out.println( Season.from( Month.JANUARY ) ) ;
System.out.println( Season.from( Month.JANUARY ).getDisplayName() ) ;

WINTER

Winter

By the way, a tip: An enum can now be defined locally, in Java 16 and later. This came from work on records.

Solution 2:

This...

public class Seasons {
  enum Seasons {SPRING, SUMMER, WINTER, AUTUMN};

is giving me no end of issues, the compiler doesn't seem to like the fact that you've re-declared Seasons

You also seem to be missing an opportunity to encapsulate some of the functionality, for example, wouldn't it be easier if you could ask the MONTH directly what season it represents?

So, I started by renaming you enums so they are plural and then added support to the Month enum to get the current Season

public class Seasons {

    enum Season {
        SPRING, SUMMER, WINTER, AUTUMN
    };

    enum Month {
        JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST,
        SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;

        public Season getSeason() {
            switch (this) {
                case SEPTEMBER:
                case OCTOBER:
                case NOVEMBER:
                    return Season.SPRING;
                case DECEMBER:
                case JANUARY:
                case FEBRUARY:
                    return Season.WINTER;
                case JUNE:
                case JULY:
                case AUGUST:
                    return Season.SUMMER;
                case MARCH:
                case APRIL:
                case MAY:
                    return Season.AUTUMN;
            }
            return null;
        }
    };

    public static void main(String[] args) {
        System.out.println(Month.OCTOBER.getSeason());
    }
}