Reputation: 35
I am not too familiar with enum type and I must create a Sport competition class which extends from the class event. It takes in 2 new parameters, one of them being the season of type enum. Not sure how to implement this so that I can choose one of them when initializing the object.
package SportCompetition;
import Event.Event;
public class SportCompetition extends Event {
enum Seasons {SUMMER,FALL,WINTER,SPRING};
int numberOfActivities;
public SportCompetition(int year, int month,
int numberOfCities, int numberOfActivities) {
super(year,month,numberOfCities);
this.numberOfActivities = numberOfActivities;
}
}
Upvotes: 1
Views: 143
Reputation: 7315
You should create your Enum
outside the class. When you have an Enum
defined you can use it like you use other classes to declare a variable and set it using a constructor.
Try the Below code:
public class SportCompetition extends Event {
int numberOfActivities;
Season season;
public SportCompetition(int year, int month, int numberOfCities, int numberOfActivities, Season season) {
super(year, month, numberOfCities);
this.numberOfActivities = numberOfActivities;
this.season = season;
}
}
enum Season {
SUMMER, FALL, WINTER, SPRING
}
When you create the object of SportCompetition
create the object like below.
SportCompetition sportCompetition = new SportCompetition(2018,1,4,10,Seasons.WINTER);
Upvotes: 2
Reputation: 7504
I know you have got the answer but there possibly could be the reason to keep the enum inside the class to achieve separation of concern.
You could use static enum
and enjoy the architectural benefit of putting enum inside its relevant class itself.
package SportCompetition;
import Event.Event;
public class SportCompetition extends Event {
static enum Season {
SUMMER,FALL,WINTER,SPRING
}
int numberOfActivities;
Season season;
public SportCompetition(int year, int month, int numberOfCities, int numberOfActivities, Season season) {
super(year, month, numberOfCities);
this.numberOfActivities = numberOfActivities;
this.season = season;
}
}
And create object like below:
SportCompetition sportCompetition = new SportCompetition(2018,1,4,10,SportCompetition.Season.WINTER);
Upvotes: 0
Reputation: 36229
enum Season {
SPRING, SUMMER, FALL, WINTER;
static Season[] Seasons = {Season.SPRING, Season.SUMMER, Season.FALL, Season.WINTER};
static public Season fromMonth (int m) {
int idx = ((m + 10) % 12)/3;
return Seasons[idx];
}
}
for (int i = 0; i < 24; ++i)
System.out.printf ("%2d %2d %s\n", i, ((i + 10) % 12), Season.fromMonth (i));
Okay, first, you have to take care - are your months literal values (1==Jan) or indexes (0=Jan)? Second, Seasons don't change on month borders, or do they in finance seasons? So it's just a prove of concept. But if you want to go with this simplified calender concept, using a linear function to map months to Seasons is the way to go.
And naming - the single thing is a Season.WINTER not Informatiker Alfred Winter and not Seasons.Winter. Seasons would be, and is, a Collection of Season.
-> for (int i = 0; i < 24; ++i) System.out.printf ("%2d %2d %s\n", i, ((i + 10) % 12), Season.fromMonth (i));
0 10 WINTER
1 11 WINTER
2 0 SPRING
3 1 SPRING
4 2 SPRING
5 3 SUMMER
6 4 SUMMER
7 5 SUMMER
8 6 FALL
9 7 FALL
10 8 FALL
11 9 WINTER
12 10 WINTER
13 11 WINTER
14 0 SPRING
15 1 SPRING
16 2 SPRING
17 3 SUMMER
18 4 SUMMER
19 5 SUMMER
20 6 FALL
21 7 FALL
22 8 FALL
23 9 WINTER
I guess nobody understands, why in the enum, we have to repeat the name Season in Season.SPRING and so on. Maybe I'm missing something.
Upvotes: 0
Reputation: 467
You only have problem in passing enum as argument, so I am creating a constructor which accepts only argument as enum for simplicity and you can extend this solution to your actual problem
Creating a Enum
enum Seasons {
SUMMER,FALL,WINTER,SPRING
};
Now you want to create an object of SportCompetition from the constructor which accepts only one Enum attribute
SportCompetition sportCompetition = new SportCompetition(Seasons.SUMMER)
Complete code:
public class SportCompetition extends Event {
int numberOfActivities;
Seasons season;
public SportCompetition(Seasons season) {
this.season = season;
}
public static void main(String[] args) {
SportCompetition sportCompetition = new SportCompetition(Seasons.SPRING);
}
}
enum Seasons {SUMMER,FALL,WINTER,SPRING};
passing Enum new SportCompetition(Seasons.SUMMER)
Upvotes: 0