回答編集履歴

2

MonthDay クラス使用

2019/10/15 14:19

投稿

jimbe
jimbe

スコア13202

test CHANGED
@@ -16,27 +16,23 @@
16
16
 
17
17
  int day = Integer.parseInt(myDay.substring(2, 4));
18
18
 
19
+ MonthDay monthDay = MonthDay.of(month, day);
20
+
19
21
  // 星座取得.
20
22
 
21
- Zodiac constellation = Zodiac.get(month, day);
23
+ Zodiac constellation = Zodiac.get(monthDay);
22
-
23
- if (constellation == null) {
24
-
25
- System.out.println("日付が正しくない");
26
-
27
- return;
28
-
29
- }
30
24
 
31
25
  System.out.println(constellation.getJName());
32
26
 
33
- } catch (Exception e) {
27
+ } catch (DateTimeException e) {
34
28
 
35
29
  System.out.println("日付が正しくない");
36
30
 
37
31
  }
38
32
 
39
33
  }
34
+
35
+
40
36
 
41
37
  enum Zodiac {
42
38
 
@@ -58,7 +54,15 @@
58
54
 
59
55
  Sagittarius("射手座", 11,23, 12,21),
60
56
 
61
- Capricornus("山羊座", 12,22, 1,19),
57
+ Capricornus("山羊座", 12,22, 1,19) {
58
+
59
+ boolean contains(MonthDay target) { //年をまたがるのでこれだけ式が違う
60
+
61
+ return start.compareTo(target) <= 0 || target.compareTo(end) <= 0;
62
+
63
+ }
64
+
65
+ },
62
66
 
63
67
  Aquarius("水瓶座", 1,20, 2,18),
64
68
 
@@ -66,43 +70,35 @@
66
70
 
67
71
 
68
72
 
69
- private String jname;
73
+ protected String jname;
70
74
 
71
- private int startMonth, startDay;
75
+ protected MonthDay start, end;
72
-
73
- private int endMonth, endDay;
74
76
 
75
77
  Zodiac(String jname, int startMonth, int startDay, int endMonth, int endDay) {
76
78
 
77
79
  this.jname = jname;
78
80
 
79
- this.startMonth = startMonth;
81
+ this.start = MonthDay.of(startMonth, startDay);
80
82
 
81
- this.startDay = startDay;
82
-
83
- this.endMonth = endMonth;
83
+ this.end = MonthDay.of(endMonth, endDay);
84
-
85
- this.endDay = endDay;
86
84
 
87
85
  }
88
86
 
89
87
  String getJName() { return jname; }
90
88
 
91
- boolean contains(int month, int day) {
89
+ boolean contains(MonthDay target) {
92
90
 
93
- return (startMonth == month && startDay <= day || endMonth == month && endDay >= day);
91
+ return start.compareTo(target) <= 0 && target.compareTo(end) <= 0;
94
92
 
95
93
  }
96
94
 
97
95
 
98
96
 
99
- static Zodiac get(int month, int day) {
97
+ static Zodiac get(MonthDay target) {
100
98
 
101
- if(month < 1 || month > 12 || day < 1 || day > 31) throw new IllegalArgumentException("month="+month+",day="+day);
99
+ for(Zodiac z : values()) if(z.contains(target)) return z;
102
100
 
103
- for(Zodiac z : values()) if(z.contains(month, day)) return z;
101
+ throw new DateTimeException("定義に洩れがあるようです. target="+target);
104
-
105
- return null;
106
102
 
107
103
  }
108
104
 

1

メソッド追加

2019/10/15 14:19

投稿

jimbe
jimbe

スコア13202

test CHANGED
@@ -88,19 +88,19 @@
88
88
 
89
89
  String getJName() { return jname; }
90
90
 
91
+ boolean contains(int month, int day) {
92
+
93
+ return (startMonth == month && startDay <= day || endMonth == month && endDay >= day);
94
+
95
+ }
96
+
91
97
 
92
98
 
93
99
  static Zodiac get(int month, int day) {
94
100
 
95
101
  if(month < 1 || month > 12 || day < 1 || day > 31) throw new IllegalArgumentException("month="+month+",day="+day);
96
102
 
97
- for(Zodiac z : values()) {
98
-
99
- if(z.startMonth == month && z.startDay <= day ||
100
-
101
- z.endMonth == month && z.endDay >= day) return z;
103
+ for(Zodiac z : values()) if(z.contains(month, day)) return z;
102
-
103
- }
104
104
 
105
105
  return null;
106
106