回答編集履歴

1

コード例追加

2021/07/24 11:36

投稿

jimbe
jimbe

スコア12632

test CHANGED
@@ -1,9 +1,43 @@
1
- 私なら enum を Factory のようにはせずに、単に
1
+ 私なら enum を Factory のようにはせずに、単に ```public static Constellation get(int month, int dayOfMonth) { ~ }``` というメソッドを Constellation に作るでしょう。
2
2
 
3
3
 
4
4
 
5
- ```public static Constellation get(int month, int dayOfMonth) { ~ }```
5
+ 例:
6
6
 
7
+ ```java
7
8
 
9
+ public boolean match(int month, int dayOfMonth) {
8
10
 
11
+ MonthDay target = MonthDay.of(month, dayOfMonth);
12
+
13
+ return !(endDay.isAfter(startDay)
14
+
15
+ ? target.isBefore(startDay) || target.isAfter(endDay)
16
+
17
+ : target.isBefore(startDay) && target.isAfter(endDay));
18
+
19
+ }
20
+
21
+ public static Constellation get(int month, int dayOfMonth) {
22
+
9
- というメソッドを Constellation に作るでしょう。
23
+ Constellation cons[] = values();
24
+
25
+ for(int i=0; i<cons.length-1; i++) {
26
+
27
+ if(cons[i].match(month,dayOfMonth)) return cons[i];
28
+
29
+ }
30
+
31
+ return cons[cons.length-1];
32
+
33
+ }
34
+
35
+ ```
36
+
37
+ ```java
38
+
39
+ Constellation cons = Constellation.get(month, dayOfMonth);
40
+
41
+ System.out.println(year+"年"+month+"月"+dayOfMonth+"日生まれの人の星座は"+cons.getType());
42
+
43
+ ```