回答編集履歴
3
コード変更
test
CHANGED
@@ -14,123 +14,281 @@
|
|
14
14
|
|
15
15
|
|
16
16
|
|
17
|
+
以下は、 FACTORIES に追加するだけで(ロジックを弄らずとも)管理対象を増やすことが出来るようにしてみました。
|
18
|
+
|
19
|
+
|
20
|
+
|
17
21
|
Kanri.java
|
18
22
|
|
19
23
|
```java
|
20
24
|
|
21
25
|
import java.io.IOException;
|
22
26
|
|
23
|
-
import java.util.
|
27
|
+
import java.util.*;
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
public class Kanri {
|
32
|
+
|
33
|
+
private static ManagedObjectFactory<?>[] FACTORIES = new ManagedObjectFactory[]{
|
34
|
+
|
35
|
+
new CompactDisc.Factory(),
|
36
|
+
|
37
|
+
new Magazine.Factory(),
|
38
|
+
|
39
|
+
//new Video.Factory(),
|
40
|
+
|
41
|
+
//new Book.Factory(),
|
42
|
+
|
43
|
+
};
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
public static void main(String[] args) throws IOException {
|
48
|
+
|
49
|
+
ArrayList<ManagedObject> stock = new ArrayList<>();
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
Scanner scanner = new Scanner(System.in);
|
54
|
+
|
55
|
+
for(int num=0; num!=3; ) {
|
56
|
+
|
57
|
+
System.out.println("登録:1 一覧:2 終了:3");
|
58
|
+
|
59
|
+
switch(num = Integer.parseInt(scanner.nextLine())) {
|
60
|
+
|
61
|
+
case 1: regist(stock, scanner); break;
|
62
|
+
|
63
|
+
case 2: printAll(stock); break;
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
System.out.println("終了.");
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
private static void regist(ArrayList<ManagedObject> stock, Scanner scanner) {
|
76
|
+
|
77
|
+
//選択肢表示
|
78
|
+
|
79
|
+
StringJoiner sj = new StringJoiner(" ");
|
80
|
+
|
81
|
+
for(int i=0; i<FACTORIES.length; i++) sj.add(FACTORIES[i].getTypeName() + ":" + (i+1));
|
82
|
+
|
83
|
+
System.out.println(sj);
|
84
|
+
|
85
|
+
//選択
|
86
|
+
|
87
|
+
int bunrui = Integer.parseInt(scanner.nextLine());
|
88
|
+
|
89
|
+
if(bunrui < 1 || FACTORIES.length < bunrui) {
|
90
|
+
|
91
|
+
System.out.println("未実装");
|
92
|
+
|
93
|
+
return;
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
//生成・登録
|
98
|
+
|
99
|
+
ManagedObject obj = FACTORIES[bunrui-1].create(scanner);
|
100
|
+
|
101
|
+
stock.add(obj);
|
102
|
+
|
103
|
+
System.out.println(obj.toString("\n"));
|
104
|
+
|
105
|
+
}
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
private static void printAll(ArrayList<ManagedObject> stock) {
|
110
|
+
|
111
|
+
if(stock.isEmpty()) {
|
112
|
+
|
113
|
+
System.out.println("データがありません");
|
114
|
+
|
115
|
+
} else {
|
116
|
+
|
117
|
+
for(ManagedObject obj : stock) System.out.println(obj.toString("\n"));
|
118
|
+
|
119
|
+
}
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
//管理対象物
|
126
|
+
|
127
|
+
interface ManagedObject {
|
128
|
+
|
129
|
+
String toString(CharSequence delimiter);
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
//管理対象物生成
|
134
|
+
|
135
|
+
abstract class ManagedObjectFactory<E extends ManagedObject> {
|
136
|
+
|
137
|
+
abstract String getTypeName();
|
138
|
+
|
139
|
+
abstract E create(Scanner scanner);
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
protected String readLine(String prompt, Scanner scanner) {
|
144
|
+
|
145
|
+
System.out.println(prompt);
|
146
|
+
|
147
|
+
return scanner.nextLine();
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
```
|
154
|
+
|
155
|
+
CompactDisc.java
|
156
|
+
|
157
|
+
```java
|
24
158
|
|
25
159
|
import java.util.Scanner;
|
26
160
|
|
27
|
-
|
161
|
+
import java.util.StringJoiner;
|
28
|
-
|
162
|
+
|
163
|
+
|
164
|
+
|
29
|
-
p
|
165
|
+
class CompactDisc implements ManagedObject {
|
30
|
-
|
166
|
+
|
31
|
-
p
|
167
|
+
private static final String TYPE_NAME = "CD";
|
168
|
+
|
32
|
-
|
169
|
+
private static final String[] TITLES = { "タイトル:", "アーティスト:", "その他情報:" };
|
170
|
+
|
171
|
+
|
172
|
+
|
33
|
-
|
173
|
+
static class Factory extends ManagedObjectFactory<CompactDisc> {
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
174
|
+
|
38
|
-
|
39
|
-
|
175
|
+
@Override
|
40
|
-
|
41
|
-
|
176
|
+
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
177
|
+
public String getTypeName() { return TYPE_NAME; }
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
178
|
+
|
50
|
-
|
51
|
-
System.out.println("CD:1 雑誌:2 ビデオ:3 書籍:4");
|
52
|
-
|
53
|
-
int bunrui = Integer.parseInt(scanner.nextLine());
|
54
|
-
|
55
|
-
|
179
|
+
@Override
|
56
|
-
|
180
|
+
|
57
|
-
|
181
|
+
public CompactDisc create(Scanner scanner) {
|
58
|
-
|
182
|
+
|
59
|
-
s
|
183
|
+
String title = readLine(TITLES[0], scanner);
|
60
|
-
|
184
|
+
|
61
|
-
|
185
|
+
String artist = readLine(TITLES[1], scanner);
|
62
|
-
|
63
|
-
|
186
|
+
|
64
|
-
|
65
|
-
System.out.println("未実装");
|
66
|
-
|
67
|
-
}
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
} else if(num == 2) {
|
72
|
-
|
73
|
-
if(stock.isEmpty()) {
|
74
|
-
|
75
|
-
System.out.println("データがありません");
|
76
|
-
|
77
|
-
} else {
|
78
|
-
|
79
|
-
|
187
|
+
String etcetera = readLine(TITLES[2], scanner);
|
80
|
-
|
81
|
-
|
188
|
+
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
189
|
+
return new CompactDisc(title, artist, etcetera);
|
86
|
-
|
87
|
-
break;
|
88
|
-
|
89
|
-
}
|
90
190
|
|
91
191
|
}
|
92
192
|
|
93
193
|
}
|
94
194
|
|
195
|
+
|
196
|
+
|
197
|
+
final String title, artist, etcetera;
|
198
|
+
|
199
|
+
private CompactDisc(String title, String artist, String etcetera) {
|
200
|
+
|
201
|
+
this.title = title;
|
202
|
+
|
203
|
+
this.artist = artist;
|
204
|
+
|
205
|
+
this.etcetera = etcetera;
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
@Override
|
212
|
+
|
213
|
+
public String toString() {
|
214
|
+
|
215
|
+
return toString(" ");
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
@Override
|
222
|
+
|
223
|
+
public String toString(CharSequence delimiter) {
|
224
|
+
|
225
|
+
StringJoiner sj = new StringJoiner(delimiter);
|
226
|
+
|
227
|
+
sj.add("●" + TYPE_NAME);
|
228
|
+
|
229
|
+
sj.add(TITLES[0] + title);
|
230
|
+
|
231
|
+
sj.add(TITLES[1] + artist);
|
232
|
+
|
233
|
+
sj.add(TITLES[2] + etcetera);
|
234
|
+
|
235
|
+
return sj.toString();
|
236
|
+
|
237
|
+
}
|
238
|
+
|
95
239
|
}
|
96
240
|
|
97
|
-
|
241
|
+
```
|
242
|
+
|
98
|
-
|
243
|
+
Magazine.java
|
244
|
+
|
245
|
+
```java
|
246
|
+
|
247
|
+
import java.util.Scanner;
|
248
|
+
|
249
|
+
import java.util.StringJoiner;
|
250
|
+
|
251
|
+
|
252
|
+
|
99
|
-
class
|
253
|
+
class Magazine implements ManagedObject {
|
254
|
+
|
100
|
-
|
255
|
+
private static final String TYPE_NAME = "雑誌";
|
256
|
+
|
101
|
-
private static final String[] TITLES = { "
|
257
|
+
private static final String[] TITLES = { "書名:", "出版社:", "その他情報:" };
|
258
|
+
|
259
|
+
|
260
|
+
|
102
|
-
|
261
|
+
static class Factory extends ManagedObjectFactory<Magazine> {
|
262
|
+
|
103
|
-
|
263
|
+
@Override
|
104
|
-
|
264
|
+
|
105
|
-
|
265
|
+
public String getTypeName() { return TYPE_NAME; }
|
266
|
+
|
106
|
-
|
267
|
+
@Override
|
268
|
+
|
107
|
-
public
|
269
|
+
public Magazine create(Scanner scanner) {
|
108
|
-
|
109
|
-
|
270
|
+
|
110
|
-
|
111
|
-
String title = scanner
|
271
|
+
String title = readLine(TITLES[0], scanner);
|
112
|
-
|
113
|
-
|
272
|
+
|
114
|
-
|
115
|
-
String
|
273
|
+
String publisher = readLine(TITLES[1], scanner);
|
116
|
-
|
117
|
-
|
274
|
+
|
118
|
-
|
119
|
-
String etcetera = scanner
|
275
|
+
String etcetera = readLine(TITLES[2], scanner);
|
120
|
-
|
276
|
+
|
121
|
-
return new
|
277
|
+
return new Magazine(title, publisher, etcetera);
|
122
|
-
|
278
|
+
|
123
|
-
}
|
279
|
+
}
|
280
|
+
|
124
|
-
|
281
|
+
}
|
125
|
-
|
126
|
-
|
282
|
+
|
283
|
+
|
284
|
+
|
127
|
-
|
285
|
+
final String title, publisher, etcetera;
|
128
|
-
|
286
|
+
|
129
|
-
private
|
287
|
+
private Magazine(String title, String publisher, String etcetera) {
|
130
288
|
|
131
289
|
this.title = title;
|
132
290
|
|
133
|
-
this.
|
291
|
+
this.publisher = publisher;
|
134
292
|
|
135
293
|
this.etcetera = etcetera;
|
136
294
|
|
@@ -142,16 +300,130 @@
|
|
142
300
|
|
143
301
|
public String toString() {
|
144
302
|
|
145
|
-
return "
|
303
|
+
return toString(" ");
|
304
|
+
|
146
|
-
|
305
|
+
}
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
@Override
|
310
|
+
|
311
|
+
public String toString(CharSequence delimiter) {
|
312
|
+
|
313
|
+
StringJoiner sj = new StringJoiner(delimiter);
|
314
|
+
|
315
|
+
sj.add("●" + TYPE_NAME);
|
316
|
+
|
147
|
-
|
317
|
+
sj.add(TITLES[0] + title);
|
148
|
-
|
318
|
+
|
149
|
-
|
319
|
+
sj.add(TITLES[1] + publisher);
|
150
|
-
|
320
|
+
|
151
|
-
|
321
|
+
sj.add(TITLES[2] + etcetera);
|
322
|
+
|
323
|
+
return sj.toString();
|
152
324
|
|
153
325
|
}
|
154
326
|
|
155
327
|
}
|
156
328
|
|
157
329
|
```
|
330
|
+
|
331
|
+
実行結果
|
332
|
+
|
333
|
+
```plain
|
334
|
+
|
335
|
+
登録:1 一覧:2 終了:3
|
336
|
+
|
337
|
+
1
|
338
|
+
|
339
|
+
CD:1 雑誌:2
|
340
|
+
|
341
|
+
1
|
342
|
+
|
343
|
+
タイトル:
|
344
|
+
|
345
|
+
f
|
346
|
+
|
347
|
+
アーティスト:
|
348
|
+
|
349
|
+
g
|
350
|
+
|
351
|
+
その他情報:
|
352
|
+
|
353
|
+
w
|
354
|
+
|
355
|
+
●CD
|
356
|
+
|
357
|
+
タイトル:f
|
358
|
+
|
359
|
+
アーティスト:g
|
360
|
+
|
361
|
+
その他情報:w
|
362
|
+
|
363
|
+
登録:1 一覧:2 終了:3
|
364
|
+
|
365
|
+
2
|
366
|
+
|
367
|
+
●CD
|
368
|
+
|
369
|
+
タイトル:f
|
370
|
+
|
371
|
+
アーティスト:g
|
372
|
+
|
373
|
+
その他情報:w
|
374
|
+
|
375
|
+
登録:1 一覧:2 終了:3
|
376
|
+
|
377
|
+
1
|
378
|
+
|
379
|
+
CD:1 雑誌:2
|
380
|
+
|
381
|
+
2
|
382
|
+
|
383
|
+
書名:
|
384
|
+
|
385
|
+
q
|
386
|
+
|
387
|
+
出版社:
|
388
|
+
|
389
|
+
h
|
390
|
+
|
391
|
+
その他情報:
|
392
|
+
|
393
|
+
y
|
394
|
+
|
395
|
+
●雑誌
|
396
|
+
|
397
|
+
書名:q
|
398
|
+
|
399
|
+
出版社:h
|
400
|
+
|
401
|
+
その他情報:y
|
402
|
+
|
403
|
+
登録:1 一覧:2 終了:3
|
404
|
+
|
405
|
+
2
|
406
|
+
|
407
|
+
●CD
|
408
|
+
|
409
|
+
タイトル:f
|
410
|
+
|
411
|
+
アーティスト:g
|
412
|
+
|
413
|
+
その他情報:w
|
414
|
+
|
415
|
+
●雑誌
|
416
|
+
|
417
|
+
書名:q
|
418
|
+
|
419
|
+
出版社:h
|
420
|
+
|
421
|
+
その他情報:y
|
422
|
+
|
423
|
+
登録:1 一覧:2 終了:3
|
424
|
+
|
425
|
+
3
|
426
|
+
|
427
|
+
終了.
|
428
|
+
|
429
|
+
```
|
2
修正
test
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
これは「最初に戻す」ではありません。 main メソッドの呼び出しです。
|
6
6
|
|
7
|
-
あるメソッドの中でそのメソッドを呼び出すことを ”再帰” と呼び、これは適切に扱えないと無限に呼び出しが続き、やがてメモリを食い尽くして異常終了してしまう構造です。
|
7
|
+
あるメソッドの中でそのメソッド自身を呼び出すことを ”再帰” と呼び、これは適切に扱えないと無限に呼び出しが続き、やがてメモリを食い尽くして異常終了してしまう構造です。
|
8
8
|
|
9
9
|
横着せず、ループをご利用ください。
|
10
10
|
|
1
コード追加
test
CHANGED
@@ -7,3 +7,151 @@
|
|
7
7
|
あるメソッドの中でそのメソッドを呼び出すことを ”再帰” と呼び、これは適切に扱えないと無限に呼び出しが続き、やがてメモリを食い尽くして異常終了してしまう構造です。
|
8
8
|
|
9
9
|
横着せず、ループをご利用ください。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
----
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
Kanri.java
|
18
|
+
|
19
|
+
```java
|
20
|
+
|
21
|
+
import java.io.IOException;
|
22
|
+
|
23
|
+
import java.util.ArrayList;
|
24
|
+
|
25
|
+
import java.util.Scanner;
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
public class Kanri {
|
30
|
+
|
31
|
+
public static void main(String[] args) throws IOException {
|
32
|
+
|
33
|
+
ArrayList<Object> stock = new ArrayList<>();
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
Scanner scanner = new Scanner(System.in);
|
38
|
+
|
39
|
+
while(true) {
|
40
|
+
|
41
|
+
System.out.println("登録:1 一覧:2 終了:3");
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
int num = Integer.parseInt(scanner.nextLine());
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
if(num == 1) {
|
50
|
+
|
51
|
+
System.out.println("CD:1 雑誌:2 ビデオ:3 書籍:4");
|
52
|
+
|
53
|
+
int bunrui = Integer.parseInt(scanner.nextLine());
|
54
|
+
|
55
|
+
if(bunrui == 1) {
|
56
|
+
|
57
|
+
CompactDisc cd = CompactDisc.create(scanner);
|
58
|
+
|
59
|
+
stock.add(cd);
|
60
|
+
|
61
|
+
System.out.println("登録しました : "+cd);
|
62
|
+
|
63
|
+
} else {
|
64
|
+
|
65
|
+
System.out.println("未実装");
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
} else if(num == 2) {
|
72
|
+
|
73
|
+
if(stock.isEmpty()) {
|
74
|
+
|
75
|
+
System.out.println("データがありません");
|
76
|
+
|
77
|
+
} else {
|
78
|
+
|
79
|
+
for(Object obj : stock) System.out.println(obj);
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
} else if(num == 3) {
|
86
|
+
|
87
|
+
break;
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
class CompactDisc {
|
100
|
+
|
101
|
+
private static final String[] TITLES = { "タイトル:", "アーティスト:", "その他情報:" };
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
/* データ入力 メソッドの定義 */
|
106
|
+
|
107
|
+
public static CompactDisc create(Scanner scanner) throws IOException {
|
108
|
+
|
109
|
+
System.out.println(TITLES[0]);
|
110
|
+
|
111
|
+
String title = scanner.nextLine();
|
112
|
+
|
113
|
+
System.out.println(TITLES[1]);
|
114
|
+
|
115
|
+
String artist = scanner.nextLine();
|
116
|
+
|
117
|
+
System.out.println(TITLES[2]);
|
118
|
+
|
119
|
+
String etcetera = scanner.nextLine();
|
120
|
+
|
121
|
+
return new CompactDisc(title, artist, etcetera);
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
private String title, artist, etcetera;
|
128
|
+
|
129
|
+
private CompactDisc(String title, String artist, String etcetera) {
|
130
|
+
|
131
|
+
this.title = title;
|
132
|
+
|
133
|
+
this.artist = artist;
|
134
|
+
|
135
|
+
this.etcetera = etcetera;
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
@Override
|
142
|
+
|
143
|
+
public String toString() {
|
144
|
+
|
145
|
+
return "●CD " +
|
146
|
+
|
147
|
+
TITLES[0] + title + " " +
|
148
|
+
|
149
|
+
TITLES[1] + artist + " " +
|
150
|
+
|
151
|
+
TITLES[2] + etcetera;
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
```
|