-
Notifications
You must be signed in to change notification settings - Fork 2
/
SwitchDemo.java
71 lines (60 loc) · 1.51 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
63
64
65
66
67
68
69
70
71
package com.di1shuai.java12.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 = whatIsTodayBefore12(day);
System.out.println(toDay);
toDay = whatIsToday12(day);
System.out.println(toDay);
}
private static TODAY whatIsTodayBefore12(DAY day) {
// Before 12
TODAY today;
switch (day) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
today = TODAY.WORKING_DAY;
break;
case SATURDAY:
case SUNDAY:
today = TODAY.WEEKEND_DAY;
break;
default:
throw new RuntimeException("N/A");
}
return today;
}
private static TODAY whatIsToday12(DAY day) {
// 12
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;
}
}