回答編集履歴

10

誤字

2023/09/17 19:32

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -1,4 +1,4 @@
1
- テーブルの枠に合わせて予定を探し、無かったら空の td 、有って開始時間が枠と同じなら時数分の rowspan の td を書き、枠と違うなら何も書かないようにします。
1
+ テーブルの枠に合わせて予定を探し、無かったら空の td 、有って開始時間が枠と同じなら時数分の rowspan の td を書き、枠と違うなら何も書かないようにします。
2
2
  ```jsp
3
3
  <%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
4
4
  <%@page import="entity.Facility"%>

9

不要メソッド削除

2023/09/17 19:31

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -76,17 +76,6 @@
76
76
  this.name = name;
77
77
  }
78
78
 
79
- @Override
80
- public int hashCode() {
81
- return Objects.hash(id, name);
82
- }
83
- @Override
84
- public boolean equals(Object obj) {
85
- if(this == obj) return true;
86
- if(obj == null || getClass() != obj.getClass()) return false;
87
- Facility other = (Facility)obj;
88
- return id == other.id && Objects.equals(name, other.name);
89
- }
90
79
  @Override
91
80
  public String toString() {
92
81
  return "Facility [id=" + id + ", name=" + name + "]";

8

ファイル名間違い

2023/09/17 08:08

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -47,7 +47,7 @@
47
47
  </body>
48
48
  </html>
49
49
  ```
50
- entity/Facility.class ( FacilityInfo クラスを不変・マスタ化, フィールド名変更 等 )
50
+ entity/Facility.java ( FacilityInfo クラスを不変・マスタ化, フィールド名変更 等 )
51
51
  ```java
52
52
  package entity;
53
53
 

7

コード修正

2023/09/17 06:38

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -108,8 +108,8 @@
108
108
  public final LocalTime start, endExclusive;
109
109
 
110
110
  public Reservation(int id, int facilityId, LocalTime start, LocalTime endExclusive) {
111
- if(start.getMinute()%Reservation.TIME_UNIT != 0) throw new IllegalArgumentException("start=" + start);
111
+ if(start.getMinute()%TIME_UNIT != 0) throw new IllegalArgumentException("start=" + start);
112
- if(endExclusive.getMinute()%Reservation.TIME_UNIT != 0) throw new IllegalArgumentException("endExclusive=" + endExclusive);
112
+ if(endExclusive.getMinute()%TIME_UNIT != 0) throw new IllegalArgumentException("endExclusive=" + endExclusive);
113
113
  if(!start.isBefore(endExclusive)) throw new IllegalArgumentException("start=" + start + ", endExclusive=" + endExclusive);
114
114
 
115
115
  this.id = id;

6

jsp の表示時間範囲(start~endExclusive)を変えても正常に表示されるようにコード修正

2023/09/17 05:53

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -5,51 +5,73 @@
5
5
  <%@page import="entity.Reservation"%>
6
6
  <%@page import="entity.TableReader"%>
7
7
  <%@page import="java.time.LocalTime"%>
8
+ <%@page import="java.time.temporal.ChronoUnit"%>
8
9
  <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
9
10
  <!DOCTYPE html>
10
11
  <html>
11
12
  <head>
12
13
  <meta charset="UTF-8">
13
14
  <link rel="stylesheet" type="text/css" href="calendar.css" />
14
- <title>予約一覧</title>
15
+ <title>予約テーブル</title>
15
16
  </head>
16
17
  <body>
17
18
  <table>
18
19
  <%TableReader reader = new TableReader(); %>
19
20
  <tr>
20
21
  <th></th>
21
- <%for(Facility facility: reader.getFacilityList()) { %>
22
+ <%for(Facility facility: Facility.getList()) { %>
22
23
  <th><%=facility.name %></th>
23
24
  <%} %>
24
25
  </tr>
25
- <%for(LocalTime time: reader.getTimeList()) { %>
26
+ <%LocalTime start = LocalTime.of(9,0); %>
27
+ <%LocalTime endExclusive = LocalTime.of(18,0); %>
28
+ <%for(LocalTime time=start; time.isBefore(endExclusive); time=time.plusMinutes(Reservation.TIME_UNIT)) { %>
26
29
  <tr>
27
30
  <td><%=time %></td>
28
- <%for(Facility facility: reader.getFacilityList()) {
31
+ <%for(Facility facility: Facility.getList()) {
29
32
  Reservation reservation = reader.findReservation(time, facility.id);
30
33
  if(reservation == null) { %>
31
34
  <td bgcolor="#dddddd"></td>
32
- <%} else if(reservation.startTime.equals(time)) { %>
35
+ <%} else if(reservation.start.equals(time) || time == start) {
36
+ long max = time.until(endExclusive, ChronoUnit.MINUTES) / Reservation.TIME_UNIT;
37
+ long rowspan = Math.min(time.until(reservation.endExclusive, ChronoUnit.MINUTES) / Reservation.TIME_UNIT, max); %>
33
- <td bgcolor="#aaaaaa" rowspan="<%=reservation.utilizationTime/15 %>"><%=reservation.id %></td>
38
+ <td bgcolor="#aaaaaa" rowspan="<%=rowspan %>"><%=reservation.id %></td>
34
39
  <%} %>
35
40
  <%} %>
36
41
  </tr>
37
42
  <%} %>
43
+ <tr>
44
+ <td><%=endExclusive %></td>
45
+ </tr>
38
46
  </table>
39
47
  </body>
40
48
  </html>
41
49
  ```
42
- entity/Facility.class ( FacilityInfo クラスを不変化, フィールド名変更 )
50
+ entity/Facility.class ( FacilityInfo クラスを不変・マスタ化, フィールド名変更 )
43
51
  ```java
44
52
  package entity;
45
53
 
46
- import java.util.Objects;
54
+ import java.util.*;
47
55
 
48
56
  public class Facility {
57
+ private static List<Facility> list;
58
+
59
+ public static List<Facility> getList() {
60
+ if(list == null) {
61
+ list = Collections.unmodifiableList(Arrays.asList(
62
+ new Facility(1,"会議室1"),
63
+ new Facility(2,"会議室2"),
64
+ new Facility(3,"会議室3"),
65
+ new Facility(4,"会議室4")
66
+ ));
67
+ }
68
+ return list;
69
+ }
70
+
49
71
  public final int id;
50
72
  public final String name;
51
73
 
52
- public Facility(int id, String name) {
74
+ private Facility(int id, String name) {
53
75
  this.id = id;
54
76
  this.name = name;
55
77
  }
@@ -61,53 +83,61 @@
61
83
  @Override
62
84
  public boolean equals(Object obj) {
63
85
  if(this == obj) return true;
64
- if(obj == null) return false;
65
- if(getClass() != obj.getClass()) return false;
86
+ if(obj == null || getClass() != obj.getClass()) return false;
66
87
  Facility other = (Facility)obj;
67
- return Objects.equals(name, other.name) && id == other.id;
88
+ return id == other.id && Objects.equals(name, other.name);
89
+ }
90
+ @Override
91
+ public String toString() {
92
+ return "Facility [id=" + id + ", name=" + name + "]";
68
93
  }
69
94
  }
70
95
  ```
71
- entity/Reservation.java ( ReservationInfo クラスを不変化, メソッド追加 )
96
+ entity/Reservation.java ( ReservationInfo クラスを不変化, 定数・メソッド追加 )
72
97
  ```java
73
98
  package entity;
74
99
 
75
100
  import java.time.LocalTime;
76
- import java.time.temporal.ChronoUnit;
77
101
  import java.util.Objects;
78
102
 
79
103
  public class Reservation {
104
+ public static final int TIME_UNIT = 15; //[minute]
105
+
80
106
  public final int id;
81
107
  public final int facilityId;
82
- public final LocalTime startTime; //include
108
+ public final LocalTime start, endExclusive;
83
- public final LocalTime endTime; //exclude
84
- public final long utilizationTime; //[minute]
85
109
 
86
- public Reservation(int id, int facilityId, LocalTime startTime, LocalTime endTime) {
110
+ public Reservation(int id, int facilityId, LocalTime start, LocalTime endExclusive) {
111
+ if(start.getMinute()%Reservation.TIME_UNIT != 0) throw new IllegalArgumentException("start=" + start);
112
+ if(endExclusive.getMinute()%Reservation.TIME_UNIT != 0) throw new IllegalArgumentException("endExclusive=" + endExclusive);
113
+ if(!start.isBefore(endExclusive)) throw new IllegalArgumentException("start=" + start + ", endExclusive=" + endExclusive);
114
+
87
115
  this.id = id;
88
116
  this.facilityId = facilityId;
89
- this.startTime = startTime;
117
+ this.start = start;
90
- this.endTime = endTime;
118
+ this.endExclusive = endExclusive;
91
- utilizationTime = startTime.until(endTime, ChronoUnit.MINUTES);
92
119
  }
93
120
 
94
121
  public boolean inTimeRange(LocalTime time) {
95
- return startTime.equals(time) || (startTime.isBefore(time) && endTime.isAfter(time));
122
+ return start.equals(time) || (start.isBefore(time) && endExclusive.isAfter(time));
96
123
  }
97
124
 
98
125
  @Override
99
126
  public int hashCode() {
100
- return Objects.hash(endTime, facilityId, id, startTime);
127
+ return Objects.hash(id, facilityId, start, endExclusive);
101
128
  }
102
-
103
129
  @Override
104
130
  public boolean equals(Object obj) {
105
131
  if(this == obj) return true;
106
- if(obj == null) return false;
107
- if(getClass() != obj.getClass()) return false;
132
+ if(obj == null || getClass() != obj.getClass()) return false;
108
- Reservation other = (Reservation) obj;
133
+ Reservation other = (Reservation)obj;
109
- return Objects.equals(endTime, other.endTime) && facilityId == other.facilityId && id == other.id
134
+ return id == other.id && facilityId == other.facilityId
110
- && Objects.equals(startTime, other.startTime);
135
+ && Objects.equals(start, other.start) && Objects.equals(endExclusive, other.endExclusive);
136
+ }
137
+ @Override
138
+ public String toString() {
139
+ return "Reservation [id=" + id + ", facilityId=" + facilityId
140
+ + ", start=" + start + ", endExclusive=" + endExclusive + "]";
111
141
  }
112
142
  }
113
143
  ```
@@ -119,28 +149,16 @@
119
149
  import java.util.*;
120
150
 
121
151
  public class TableReader {
122
- private List<LocalTime> timeList;
123
- private List<Facility> facilityList;
124
152
  private List<Reservation> reservationList;
125
153
 
126
154
  public TableReader() {
127
- List<LocalTime> timeList = new ArrayList<>();
128
- for(LocalTime time=LocalTime.of(9,0), end=LocalTime.of(18,0); time.isBefore(end)||time.equals(end); time=time.plusMinutes(15)) timeList.add(time);
129
- this.timeList = Collections.unmodifiableList(timeList);
130
-
131
- List<Facility> facilityList = new ArrayList<>();
155
+ //テストデータ
132
- for(int i=1; i<=4; i++) facilityList.add(new Facility(i, "会議室" + i));
133
- this.facilityList = Collections.unmodifiableList(facilityList);
134
-
135
156
  reservationList = new ArrayList<>();
136
157
  reservationList.add(new Reservation(1, 1, LocalTime.of( 9, 0), LocalTime.of(12, 0)));
137
158
  reservationList.add(new Reservation(2, 1, LocalTime.of(15, 0), LocalTime.of(17, 0)));
138
159
  reservationList.add(new Reservation(3, 2, LocalTime.of(10, 0), LocalTime.of(12, 0)));
139
160
  reservationList.add(new Reservation(4, 4, LocalTime.of(10, 0), LocalTime.of(12, 0)));
140
161
  }
141
-
142
- public List<LocalTime> getTimeList() { return timeList; }
143
- public List<Facility> getFacilityList() { return facilityList; }
144
162
 
145
163
  public Reservation findReservation(LocalTime time, int facilityId) {
146
164
  for(Reservation reservation : reservationList) {

5

コード修正

2023/09/17 04:26

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -16,27 +16,28 @@
16
16
  <body>
17
17
  <table>
18
18
  <%TableReader reader = new TableReader(); %>
19
- <tr>
19
+ <tr>
20
- <th></th>
20
+ <th></th>
21
- <%for(Facility facility: reader.getFacilityList()) { %>
21
+ <%for(Facility facility: reader.getFacilityList()) { %>
22
- <th><%=facility.name %></th>
22
+ <th><%=facility.name %></th>
23
- <%} %>
23
+ <%} %>
24
- </tr>
24
+ </tr>
25
- <%for(LocalTime time: reader.getTimeList()) { %>
25
+ <%for(LocalTime time: reader.getTimeList()) { %>
26
- <tr>
26
+ <tr>
27
- <td><%=time %></td>
27
+ <td><%=time %></td>
28
- <%for(Facility facility: reader.getFacilityList()) {
28
+ <%for(Facility facility: reader.getFacilityList()) {
29
29
  Reservation reservation = reader.findReservation(time, facility.id);
30
30
  if(reservation == null) { %>
31
31
  <td bgcolor="#dddddd"></td>
32
- <%} else if(reservation.startTime.equals(time)) { %>
32
+ <%} else if(reservation.startTime.equals(time)) { %>
33
- <td bgcolor="#aaaaaa" rowspan="<%=reservation.getUtilizationTime()/15 %>"><%=reservation.id %></td>
33
+ <td bgcolor="#aaaaaa" rowspan="<%=reservation.utilizationTime/15 %>"><%=reservation.id %></td>
34
34
  <%} %>
35
35
  <%} %>
36
- </tr>
36
+ </tr>
37
37
  <%} %>
38
38
  </table>
39
39
  </body>
40
+ </html>
40
41
  ```
41
42
  entity/Facility.class ( FacilityInfo クラスを不変化, フィールド名変更 )
42
43
  ```java
@@ -78,17 +79,18 @@
78
79
  public class Reservation {
79
80
  public final int id;
80
81
  public final int facilityId;
81
- public final LocalTime startTime;
82
+ public final LocalTime startTime; //include
82
- public final LocalTime endTime;
83
+ public final LocalTime endTime; //exclude
84
+ public final long utilizationTime; //[minute]
83
85
 
84
86
  public Reservation(int id, int facilityId, LocalTime startTime, LocalTime endTime) {
85
87
  this.id = id;
86
88
  this.facilityId = facilityId;
87
89
  this.startTime = startTime;
88
90
  this.endTime = endTime;
91
+ utilizationTime = startTime.until(endTime, ChronoUnit.MINUTES);
89
92
  }
90
93
 
91
- public long getUtilizationTime() { return startTime.until(endTime, ChronoUnit.MINUTES); }
92
94
  public boolean inTimeRange(LocalTime time) {
93
95
  return startTime.equals(time) || (startTime.isBefore(time) && endTime.isAfter(time));
94
96
  }

4

フォルダ名修正

2023/09/17 04:13

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -38,7 +38,7 @@
38
38
  </table>
39
39
  </body>
40
40
  ```
41
- entry/Facility.class ( FacilityInfo クラスを不変化, フィールド名変更 )
41
+ entity/Facility.class ( FacilityInfo クラスを不変化, フィールド名変更 )
42
42
  ```java
43
43
  package entity;
44
44
 
@@ -67,7 +67,7 @@
67
67
  }
68
68
  }
69
69
  ```
70
- entry/Reservation.java ( ReservationInfo クラスを不変化, メソッド追加 )
70
+ entity/Reservation.java ( ReservationInfo クラスを不変化, メソッド追加 )
71
71
  ```java
72
72
  package entity;
73
73
 
@@ -109,7 +109,7 @@
109
109
  }
110
110
  }
111
111
  ```
112
- entry/TableReader.java ( 新規, オリジナル jsp にあった処理を移動等 )
112
+ entity/TableReader.java ( 新規, オリジナル jsp にあった処理を移動等 )
113
113
  ```java
114
114
  package entity;
115
115
 

3

修正

2023/09/16 21:39

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -1,5 +1,4 @@
1
- テーブルの枠に合わせて予定を探し、無かったら
2
- 空の td 、有って開始と同じなら時関数分の rowspan の td を書き、開始以外なら何も書かないようにします。
1
+ テーブルの枠に合わせて予定を探し、無かったら空の td 、有って開始時間が枠と同じなら時関数分の rowspan の td を書き、枠と違うなら何も書かないようにします。
3
2
  ```jsp
4
3
  <%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
5
4
  <%@page import="entity.Facility"%>

2

修正

2023/09/16 21:36

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -1,4 +1,5 @@
1
+ テーブルの枠に合わせて予定を探し、無かったら
1
- 予定枠に合わせテーブルを設定するでは無く、テーブル枠に合わせて予定探すほうが簡単なようす。
2
+ td 、有っ開始と同じなら時関数分 rowspan td 書き、開始以外ら何も書かないようにします。
2
3
  ```jsp
3
4
  <%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
4
5
  <%@page import="entity.Facility"%>

1

修正

2023/09/16 21:07

投稿

jimbe
jimbe

スコア13318

test CHANGED
@@ -1,5 +1,4 @@
1
- 予定テーブルに当てはめるのでは無く、テーブルに予定を当てはめるというほうが簡単なようです。
1
+ 予定の枠に合わせてテーブルを設定するのでは無く、テーブルの枠合わせて予定を探すほうが簡単なようです。
2
-
3
2
  ```jsp
4
3
  <%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
5
4
  <%@page import="entity.Facility"%>