回答編集履歴
2
コードを追加
test
CHANGED
@@ -235,3 +235,285 @@
|
|
235
235
|
name=cnt_all, value=10
|
236
236
|
|
237
237
|
```
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
全ての Attribute を設定するため, 集計をクラス化してみました.
|
242
|
+
|
243
|
+
form/color を enum にし, 異常値を弾くようにしました.
|
244
|
+
|
245
|
+
```java
|
246
|
+
|
247
|
+
import java.util.*;
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
public class Q214024_2 {
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
enum Form {
|
256
|
+
|
257
|
+
circle, triangle, square, ellipse, rhombus, total;
|
258
|
+
|
259
|
+
static int size() { return Form.values().length; }
|
260
|
+
|
261
|
+
//テスト用
|
262
|
+
|
263
|
+
static Form getValue(Random random) {
|
264
|
+
|
265
|
+
return Form.values()[random.nextInt(Form.total.ordinal())];
|
266
|
+
|
267
|
+
}
|
268
|
+
|
269
|
+
};
|
270
|
+
|
271
|
+
enum Color {
|
272
|
+
|
273
|
+
red, blue, yellow, black, white, total;
|
274
|
+
|
275
|
+
static int size() { return Color.values().length; }
|
276
|
+
|
277
|
+
//テスト用
|
278
|
+
|
279
|
+
static Color getValue(Random random) {
|
280
|
+
|
281
|
+
return Color.values()[random.nextInt(Color.total.ordinal())];
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
};
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
//集計テーブル
|
290
|
+
|
291
|
+
private static class SummaryTable {
|
292
|
+
|
293
|
+
private int[][] values; //件数
|
294
|
+
|
295
|
+
SummaryTable() {
|
296
|
+
|
297
|
+
values = new int[Form.size()][Color.size()];
|
298
|
+
|
299
|
+
for(int i=0; i<values.length; i++)
|
300
|
+
|
301
|
+
for(int j=0; j<values[i].length; j++)
|
302
|
+
|
303
|
+
values[i][j] = 0;
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
//form,colorの件数を +1 する
|
308
|
+
|
309
|
+
void increment(Form form, Color color) {
|
310
|
+
|
311
|
+
values[form.ordinal()][color.ordinal()] ++;
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
//(Model に設定する) Attribute と件数のマップを返す
|
316
|
+
|
317
|
+
Map<String,Integer> getAttributeMap() {
|
318
|
+
|
319
|
+
calcTotals();
|
320
|
+
|
321
|
+
Map<String,Integer> map = new HashMap<>();
|
322
|
+
|
323
|
+
for(Form form : Form.values()) {
|
324
|
+
|
325
|
+
for(Color color : Color.values()) {
|
326
|
+
|
327
|
+
map.put(getAttributeName(form,color), values[form.ordinal()][color.ordinal()]);
|
328
|
+
|
329
|
+
}
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
return map;
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
//各合計を計算
|
338
|
+
|
339
|
+
private void calcTotals() {
|
340
|
+
|
341
|
+
for(int j=0; j<Color.size(); j++) values[Form.total.ordinal()][j] = 0;
|
342
|
+
|
343
|
+
for(int i=0; i<Form.size(); i++) {
|
344
|
+
|
345
|
+
values[i][Color.total.ordinal()] = 0;
|
346
|
+
|
347
|
+
for(int j=0; j<Color.total.ordinal(); j++) {
|
348
|
+
|
349
|
+
values[i][Color.total.ordinal()] += values[i][j];
|
350
|
+
|
351
|
+
if(i != Form.total.ordinal()) values[Form.total.ordinal()][j] += values[i][j];
|
352
|
+
|
353
|
+
}
|
354
|
+
|
355
|
+
}
|
356
|
+
|
357
|
+
}
|
358
|
+
|
359
|
+
//Attribute 名を返す
|
360
|
+
|
361
|
+
private String getAttributeName(Form form, Color color) {
|
362
|
+
|
363
|
+
if(form == Form.total && color == Color.total) return "cnt_all";
|
364
|
+
|
365
|
+
StringBuilder name = new StringBuilder("cnt_");
|
366
|
+
|
367
|
+
if(form != Form. total) name.append("f").append(form.ordinal() +1);
|
368
|
+
|
369
|
+
if(color != Color.total) name.append("c").append(color.ordinal()+1);
|
370
|
+
|
371
|
+
return name.toString();
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
private String getDB(Model model){
|
380
|
+
|
381
|
+
List<Table1Entity> table1DataList = table1Repository.findAll();
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
//集計
|
386
|
+
|
387
|
+
SummaryTable table = new SummaryTable();
|
388
|
+
|
389
|
+
for (Table1Entity entity : table1DataList) {
|
390
|
+
|
391
|
+
try {
|
392
|
+
|
393
|
+
Form form = Form.valueOf(entity.getForm());
|
394
|
+
|
395
|
+
Color color = Color.valueOf(entity.getColor());
|
396
|
+
|
397
|
+
table.increment(form, color);
|
398
|
+
|
399
|
+
} catch(IllegalArgumentException e) {
|
400
|
+
|
401
|
+
System.err.println("form または color の値が異常です. form="+entity.getForm()+", color="+entity.getColor());
|
402
|
+
|
403
|
+
}
|
404
|
+
|
405
|
+
}
|
406
|
+
|
407
|
+
|
408
|
+
|
409
|
+
//設定
|
410
|
+
|
411
|
+
for(Map.Entry<String, Integer> entry : table.getAttributeMap().entrySet()) {
|
412
|
+
|
413
|
+
model.addAttribute(entry.getKey(), entry.getValue());
|
414
|
+
|
415
|
+
}
|
416
|
+
|
417
|
+
|
418
|
+
|
419
|
+
return ""+table1DataList.size();
|
420
|
+
|
421
|
+
}
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
// 以下テスト用
|
426
|
+
|
427
|
+
private Table1Repository table1Repository = new Table1Repository();
|
428
|
+
|
429
|
+
void add(String form, String color) {
|
430
|
+
|
431
|
+
table1Repository.add(form, color);
|
432
|
+
|
433
|
+
}
|
434
|
+
|
435
|
+
void printAll() {
|
436
|
+
|
437
|
+
table1Repository.printAll();
|
438
|
+
|
439
|
+
}
|
440
|
+
|
441
|
+
static class Model {
|
442
|
+
|
443
|
+
void addAttribute(String name, int value) {
|
444
|
+
|
445
|
+
System.out.println("name="+name+", value="+value);
|
446
|
+
|
447
|
+
}
|
448
|
+
|
449
|
+
}
|
450
|
+
|
451
|
+
static class Table1Entity {
|
452
|
+
|
453
|
+
private String form, color;
|
454
|
+
|
455
|
+
Table1Entity(String form, String color) { this.form=form; this.color=color; }
|
456
|
+
|
457
|
+
String getForm(){ return form; }
|
458
|
+
|
459
|
+
String getColor() { return color; }
|
460
|
+
|
461
|
+
public String toString() { return form+":"+color; }
|
462
|
+
|
463
|
+
}
|
464
|
+
|
465
|
+
static class Table1Repository {
|
466
|
+
|
467
|
+
private List<Table1Entity> list = new ArrayList<Table1Entity>();
|
468
|
+
|
469
|
+
void add(String form, String color) {
|
470
|
+
|
471
|
+
list.add(new Table1Entity(form, color));
|
472
|
+
|
473
|
+
}
|
474
|
+
|
475
|
+
List<Table1Entity> findAll() {
|
476
|
+
|
477
|
+
return list;
|
478
|
+
|
479
|
+
}
|
480
|
+
|
481
|
+
void printAll() {
|
482
|
+
|
483
|
+
for(Table1Entity entity : list) System.out.println(entity);
|
484
|
+
|
485
|
+
}
|
486
|
+
|
487
|
+
}
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
public static void main(String args[]) {
|
492
|
+
|
493
|
+
Q214024_2 q214024 = new Q214024_2();
|
494
|
+
|
495
|
+
|
496
|
+
|
497
|
+
Random random = new Random();
|
498
|
+
|
499
|
+
for(int i=0; i<10; i++) {
|
500
|
+
|
501
|
+
q214024.add(Form.getValue(random).name(), Color.getValue(random).name());
|
502
|
+
|
503
|
+
}
|
504
|
+
|
505
|
+
//q214024.add("zzz", Color.getValue(random).name());
|
506
|
+
|
507
|
+
q214024.printAll();
|
508
|
+
|
509
|
+
|
510
|
+
|
511
|
+
System.out.println("----");
|
512
|
+
|
513
|
+
q214024.getDB(new Model());
|
514
|
+
|
515
|
+
}
|
516
|
+
|
517
|
+
}
|
518
|
+
|
519
|
+
```
|
1
メソッド名間違い
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
ランダムに10件のデータを用意して集計しています.
|
4
4
|
|
5
|
-
一応 model.
|
5
|
+
一応 model.addAttribute まで行うようにしましたが, 無いデータ(=0件)分は行っていません.
|
6
6
|
|
7
7
|
|
8
8
|
|