回答編集履歴
4
Portfolio クラスの Iterable 化
answer
CHANGED
@@ -4,13 +4,13 @@
|
|
4
4
|
コレクション不可ということですので配列に戻しました。
|
5
5
|
そのままというのも勿体ない(?)ので、段階的拡張としてみました。
|
6
6
|
専用メソッドも課題的に問題視されるかもしれませんので、元の形にしました。
|
7
|
+
Portfolio を for-each ループで使えるように Iterable 化しました。
|
7
8
|
```java
|
8
9
|
package teratail_java.q373056;
|
9
10
|
|
10
|
-
import java.util.
|
11
|
+
import java.util.*;
|
11
|
-
import java.util.StringJoiner;
|
12
12
|
|
13
|
-
public class Portfolio {
|
13
|
+
public class Portfolio implements Iterable<Position> {
|
14
14
|
private Position[] positions = new Position[0];
|
15
15
|
private int length = 0;
|
16
16
|
public void addPosition(Position p) {
|
@@ -28,7 +28,7 @@
|
|
28
28
|
positions = newArray;
|
29
29
|
}
|
30
30
|
private Position findPosition(Issue issue) {
|
31
|
-
for(
|
31
|
+
for(Position p : this) if(p.getIssue().equals(issue)) return p;
|
32
32
|
return null;
|
33
33
|
}
|
34
34
|
public int getLength() { return length; }
|
@@ -36,6 +36,20 @@
|
|
36
36
|
if(index < 0 || length <= index) throw new ArrayIndexOutOfBoundsException(index);
|
37
37
|
return positions[index];
|
38
38
|
}
|
39
|
+
@Override
|
40
|
+
public Iterator<Position> iterator() {
|
41
|
+
return new Iterator<Position>() {
|
42
|
+
private int index = 0;
|
43
|
+
@Override
|
44
|
+
public Position next() {
|
45
|
+
return positions[index++];
|
46
|
+
}
|
47
|
+
@Override
|
48
|
+
public boolean hasNext() {
|
49
|
+
return index < length;
|
50
|
+
}
|
51
|
+
};
|
52
|
+
}
|
39
53
|
|
40
54
|
public static void main(String[] args) {
|
41
55
|
Position positionStock1 = new Position(new Stock("92010", "A", Stock.Market.TSE), 0.1);
|
@@ -53,8 +67,8 @@
|
|
53
67
|
portfolio.addPosition(positionBond2);
|
54
68
|
portfolio.addPosition(positionBond3);
|
55
69
|
|
56
|
-
for(
|
70
|
+
for(Position p : portfolio) {
|
57
|
-
System.out.println(
|
71
|
+
System.out.println(p);
|
58
72
|
}
|
59
73
|
}
|
60
74
|
}
|
3
修正の説明の追加
answer
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
----
|
4
4
|
コレクション不可ということですので配列に戻しました。
|
5
5
|
そのままというのも勿体ない(?)ので、段階的拡張としてみました。
|
6
|
+
専用メソッドも課題的に問題視されるかもしれませんので、元の形にしました。
|
6
7
|
```java
|
7
8
|
package teratail_java.q373056;
|
8
9
|
|
@@ -36,7 +37,6 @@
|
|
36
37
|
return positions[index];
|
37
38
|
}
|
38
39
|
|
39
|
-
|
40
40
|
public static void main(String[] args) {
|
41
41
|
Position positionStock1 = new Position(new Stock("92010", "A", Stock.Market.TSE), 0.1);
|
42
42
|
Position positionStock2 = new Position(new Stock("68610", "B", Stock.Market.OSE), 0.2);
|
2
main での for で表示するようにコード変更
answer
CHANGED
@@ -30,10 +30,13 @@
|
|
30
30
|
for(int i=0; i<length; i++) if(positions[i].getIssue().equals(issue)) return positions[i];
|
31
31
|
return null;
|
32
32
|
}
|
33
|
+
public int getLength() { return length; }
|
33
|
-
|
34
|
+
public Position getPosition(int index) {
|
34
|
-
|
35
|
+
if(index < 0 || length <= index) throw new ArrayIndexOutOfBoundsException(index);
|
36
|
+
return positions[index];
|
35
37
|
}
|
36
38
|
|
39
|
+
|
37
40
|
public static void main(String[] args) {
|
38
41
|
Position positionStock1 = new Position(new Stock("92010", "A", Stock.Market.TSE), 0.1);
|
39
42
|
Position positionStock2 = new Position(new Stock("68610", "B", Stock.Market.OSE), 0.2);
|
@@ -50,7 +53,9 @@
|
|
50
53
|
portfolio.addPosition(positionBond2);
|
51
54
|
portfolio.addPosition(positionBond3);
|
52
55
|
|
56
|
+
for(int i=0; i<portfolio.getLength(); i++) {
|
53
|
-
|
57
|
+
System.out.println(portfolio.getPosition(i));
|
58
|
+
}
|
54
59
|
}
|
55
60
|
}
|
56
61
|
|
1
配列仕様化
answer
CHANGED
@@ -1,26 +1,37 @@
|
|
1
1
|
外部のクラスに対し実装("配列")を直接見せるのは良いことではありませんので、表示だけであれば専用メソッドを作ってしまえば良いと思います。
|
2
2
|
|
3
|
+
----
|
4
|
+
コレクション不可ということですので配列に戻しました。
|
5
|
+
そのままというのも勿体ない(?)ので、段階的拡張としてみました。
|
3
6
|
```java
|
4
7
|
package teratail_java.q373056;
|
5
8
|
|
6
|
-
import java.util.
|
9
|
+
import java.util.Objects;
|
10
|
+
import java.util.StringJoiner;
|
7
11
|
|
8
12
|
public class Portfolio {
|
9
|
-
private
|
13
|
+
private Position[] positions = new Position[0];
|
14
|
+
private int length = 0;
|
10
15
|
public void addPosition(Position p) {
|
11
16
|
Position exists = findPosition(p.getIssue());
|
12
17
|
if(exists == null) {
|
18
|
+
if(length >= positions.length) expandArray();
|
13
|
-
|
19
|
+
positions[length++] = p;
|
14
20
|
} else {
|
15
21
|
exists.addAmount(p.getAmount());
|
16
22
|
}
|
17
23
|
}
|
24
|
+
private void expandArray() {
|
25
|
+
Position[] newArray = new Position[positions.length + 5];
|
26
|
+
System.arraycopy(positions, 0, newArray, 0, length);
|
27
|
+
positions = newArray;
|
28
|
+
}
|
18
29
|
private Position findPosition(Issue issue) {
|
19
|
-
for(
|
30
|
+
for(int i=0; i<length; i++) if(positions[i].getIssue().equals(issue)) return positions[i];
|
20
31
|
return null;
|
21
32
|
}
|
22
33
|
void printAll() {
|
23
|
-
for(
|
34
|
+
for(int i=0; i<length; i++) System.out.println(positions[i]);
|
24
35
|
}
|
25
36
|
|
26
37
|
public static void main(String[] args) {
|
@@ -47,6 +58,7 @@
|
|
47
58
|
protected String code;
|
48
59
|
protected String name;
|
49
60
|
Issue(String code, String name) {
|
61
|
+
if(code == null || name == null) throw new NullPointerException();
|
50
62
|
this.code = code;
|
51
63
|
this.name = name;
|
52
64
|
}
|
@@ -74,6 +86,7 @@
|
|
74
86
|
private Market market;
|
75
87
|
Stock(String code, String name, Market market) {
|
76
88
|
super(code, name);
|
89
|
+
if(market == null) throw new NullPointerException();
|
77
90
|
this.market = market;
|
78
91
|
}
|
79
92
|
@Override
|