forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
33 lines (29 loc) · 941 Bytes
/
Main.java
File metadata and controls
33 lines (29 loc) · 941 Bytes
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
package modern.challenge;
public class Main {
public enum PlayerTypes {
TENNIS,
FOOTBALL,
SNOOKER
}
public static void main(String[] args) {
Player player = createPlayer(PlayerTypes.SNOOKER);
}
private static Player createPlayer(PlayerTypes playerType) {
return switch (playerType) {
case TENNIS-> {
System.out.println("Creating a TennisPlayer ...");
break new TennisPlayer();
}
case FOOTBALL-> {
System.out.println("Creating a FootballPlayer ...");
break new FootballPlayer();
}
case SNOOKER-> {
System.out.println("Creating a SnookerPlayer ...");
break new SnookerPlayer();
}
default->
throw new IllegalArgumentException("Invalid player type: " + playerType);
};
}
}