回答編集履歴
2
MonthDay クラス使用
answer
CHANGED
@@ -7,17 +7,15 @@
|
|
7
7
|
try {
|
8
8
|
int month = Integer.parseInt(myDay.substring(0, 2));
|
9
9
|
int day = Integer.parseInt(myDay.substring(2, 4));
|
10
|
+
MonthDay monthDay = MonthDay.of(month, day);
|
10
11
|
// 星座取得.
|
11
|
-
Zodiac constellation = Zodiac.get(
|
12
|
+
Zodiac constellation = Zodiac.get(monthDay);
|
12
|
-
if (constellation == null) {
|
13
|
-
System.out.println("日付が正しくない");
|
14
|
-
return;
|
15
|
-
}
|
16
13
|
System.out.println(constellation.getJName());
|
17
|
-
} catch (
|
14
|
+
} catch (DateTimeException e) {
|
18
15
|
System.out.println("日付が正しくない");
|
19
16
|
}
|
20
17
|
}
|
18
|
+
|
21
19
|
enum Zodiac {
|
22
20
|
Aries("牡羊座", 3,21, 4,19),
|
23
21
|
Taurus("牡牛座", 4,20, 5,20),
|
@@ -28,29 +26,29 @@
|
|
28
26
|
Libra("天秤座", 9,23, 10,23),
|
29
27
|
Scorpius("蠍座", 10,24, 11,22),
|
30
28
|
Sagittarius("射手座", 11,23, 12,21),
|
31
|
-
Capricornus("山羊座", 12,22, 1,19)
|
29
|
+
Capricornus("山羊座", 12,22, 1,19) {
|
30
|
+
boolean contains(MonthDay target) { //年をまたがるのでこれだけ式が違う
|
31
|
+
return start.compareTo(target) <= 0 || target.compareTo(end) <= 0;
|
32
|
+
}
|
33
|
+
},
|
32
34
|
Aquarius("水瓶座", 1,20, 2,18),
|
33
35
|
Pisces("魚座", 2,19, 3,20);
|
34
36
|
|
35
|
-
|
37
|
+
protected String jname;
|
36
|
-
|
38
|
+
protected MonthDay start, end;
|
37
|
-
private int endMonth, endDay;
|
38
39
|
Zodiac(String jname, int startMonth, int startDay, int endMonth, int endDay) {
|
39
40
|
this.jname = jname;
|
40
|
-
this.
|
41
|
+
this.start = MonthDay.of(startMonth, startDay);
|
41
|
-
this.startDay = startDay;
|
42
|
-
this.
|
42
|
+
this.end = MonthDay.of(endMonth, endDay);
|
43
|
-
this.endDay = endDay;
|
44
43
|
}
|
45
44
|
String getJName() { return jname; }
|
46
|
-
boolean contains(
|
45
|
+
boolean contains(MonthDay target) {
|
47
|
-
return (
|
46
|
+
return start.compareTo(target) <= 0 && target.compareTo(end) <= 0;
|
48
47
|
}
|
49
48
|
|
50
|
-
static Zodiac get(
|
49
|
+
static Zodiac get(MonthDay target) {
|
51
|
-
if(month < 1 || month > 12 || day < 1 || day > 31) throw new IllegalArgumentException("month="+month+",day="+day);
|
52
|
-
for(Zodiac z : values()) if(z.contains(
|
50
|
+
for(Zodiac z : values()) if(z.contains(target)) return z;
|
53
|
-
|
51
|
+
throw new DateTimeException("定義に洩れがあるようです. target="+target);
|
54
52
|
}
|
55
53
|
}
|
56
54
|
}
|
1
メソッド追加
answer
CHANGED
@@ -43,13 +43,13 @@
|
|
43
43
|
this.endDay = endDay;
|
44
44
|
}
|
45
45
|
String getJName() { return jname; }
|
46
|
+
boolean contains(int month, int day) {
|
47
|
+
return (startMonth == month && startDay <= day || endMonth == month && endDay >= day);
|
48
|
+
}
|
46
49
|
|
47
50
|
static Zodiac get(int month, int day) {
|
48
51
|
if(month < 1 || month > 12 || day < 1 || day > 31) throw new IllegalArgumentException("month="+month+",day="+day);
|
49
|
-
for(Zodiac z : values()) {
|
50
|
-
if(z.startMonth == month && z.startDay <= day ||
|
51
|
-
|
52
|
+
for(Zodiac z : values()) if(z.contains(month, day)) return z;
|
52
|
-
}
|
53
53
|
return null;
|
54
54
|
}
|
55
55
|
}
|