-
Notifications
You must be signed in to change notification settings - Fork 2
/
SwitchDemo.java
62 lines (51 loc) · 1.38 KB
/
SwitchDemo.java
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
package com.di1shuai.java13.switchdemo;
/**
* @author Shea
* @date 2021-01-22
* @description Switch 增强 (预览)
* case 多个 -> xxx;
*/
public class SwitchDemo {
enum DAY {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
}
enum TODAY {
WEEKEND_DAY,
WORKING_DAY
}
public static void main(String[] args) {
DAY day = DAY.MONDAY;
TODAY toDay = whatIsTodayBefore13(day);
System.out.println(toDay);
toDay = whatIsToday13(day);
System.out.println(toDay);
}
private static TODAY whatIsTodayBefore13(DAY day) {
// Before 13
TODAY today;
switch (day) {
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> today = TODAY.WORKING_DAY;
case SATURDAY, SUNDAY -> today = TODAY.WEEKEND_DAY;
default -> throw new IllegalArgumentException("Invalid day");
}
return today;
}
private static TODAY whatIsToday13(DAY day) {
// 13
return switch (day) {
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> {
yield TODAY.WORKING_DAY;
}
case SATURDAY, SUNDAY -> {
yield TODAY.WEEKEND_DAY;
}
default -> throw new IllegalArgumentException("Invalid day");
};
}
}