質問編集履歴
4
追加記入
title
CHANGED
File without changes
|
body
CHANGED
@@ -21,7 +21,7 @@
|
|
21
21
|
|
22
22
|
public class Shopping {
|
23
23
|
|
24
|
-
|
24
|
+
Calculation calculation = new Calculation();
|
25
25
|
Stand stand = new Stand();
|
26
26
|
|
27
27
|
public static void main(String[] args) {
|
@@ -34,35 +34,23 @@
|
|
34
34
|
}
|
35
35
|
|
36
36
|
private void exec(String[] args)throws IOException {
|
37
|
-
if (isVaild(args)) {
|
38
|
-
|
37
|
+
try {
|
39
|
-
|
38
|
+
stand.prepare();
|
40
|
-
|
39
|
+
toOrder(args);
|
41
|
-
|
40
|
+
} catch(Exception e) {
|
42
|
-
|
41
|
+
e.printStackTrace();
|
43
|
-
|
42
|
+
|
44
|
-
} else {
|
45
|
-
System.out.println("引数が違います");
|
46
|
-
}
|
47
43
|
}
|
48
44
|
|
49
|
-
private boolean isVaild(String[] args){
|
50
|
-
return args.length >= 2 && args.length % 2 == 0 ;
|
51
|
-
}
|
52
|
-
|
53
45
|
private void toOrder(String[] args) {
|
54
46
|
try {
|
55
47
|
for (int i = 0; i < args.length; i +=2 ) {
|
56
48
|
String code = args[i];
|
57
49
|
String num = args[i + 1];
|
58
50
|
Item oneItem = stand.order(code, num);
|
59
|
-
if (oneItem != null) {
|
60
|
-
|
51
|
+
calculation.buy(oneItem, num);
|
61
|
-
} else {
|
62
|
-
System.out.println("在庫がありません");
|
63
|
-
}
|
64
52
|
}
|
65
|
-
|
53
|
+
calculation.pay();
|
66
54
|
} catch(Exception e) {
|
67
55
|
e.printStackTrace();
|
68
56
|
}
|
@@ -74,33 +62,23 @@
|
|
74
62
|
```Itemjava
|
75
63
|
public class Item {
|
76
64
|
|
77
|
-
private String
|
65
|
+
private String codes = null;
|
78
66
|
|
79
67
|
private String name = null;
|
80
68
|
|
81
69
|
private String price = null;
|
82
70
|
|
83
|
-
private String quantity = null;
|
84
|
-
|
85
71
|
private String stock = null;
|
86
72
|
|
87
|
-
private String unit = null;
|
88
|
-
|
89
|
-
@SuppressWarnings("unused")
|
90
|
-
private Item() {
|
91
|
-
}
|
92
|
-
|
93
|
-
public Item(String
|
73
|
+
public Item(String codes, String name, String price, String stock) {
|
94
|
-
this.
|
74
|
+
this.codes = codes;
|
95
75
|
this.name = name;
|
96
76
|
this.price = price;
|
97
|
-
this.quantity = quantity;
|
98
77
|
this.stock = stock;
|
99
|
-
this.unit = unit;
|
100
78
|
}
|
101
79
|
|
102
|
-
public String
|
80
|
+
public String getCodes() {
|
103
|
-
return
|
81
|
+
return codes;
|
104
82
|
}
|
105
83
|
|
106
84
|
public String getName() {
|
@@ -110,34 +88,21 @@
|
|
110
88
|
public String getPrice() {
|
111
89
|
return price;
|
112
90
|
}
|
113
|
-
public String getQuantity() {
|
114
|
-
return quantity;
|
115
|
-
}
|
116
|
-
|
117
|
-
public String getStock() {
|
118
|
-
return stock;
|
119
|
-
}
|
120
|
-
|
121
|
-
public String getUnit() {
|
122
|
-
return unit;
|
123
|
-
}
|
124
91
|
|
125
|
-
public boolean
|
92
|
+
public boolean stock(String num) {
|
126
|
-
int
|
93
|
+
int order = Integer.parseInt(num);
|
127
|
-
int
|
94
|
+
int stock = Integer.parseInt(this.stock);
|
128
|
-
return
|
95
|
+
return order <= stock;
|
129
96
|
}
|
130
97
|
}
|
131
98
|
```
|
132
|
-
```
|
99
|
+
```Calculationjava
|
133
|
-
public class
|
100
|
+
public class Calculation {
|
134
101
|
|
135
102
|
private int sum;
|
136
103
|
|
137
104
|
public void buy(Item item,String num){
|
138
|
-
int price = Integer.parseInt(item.getPrice())*Integer.parseInt(num);
|
105
|
+
int price = Integer.parseInt(item.getPrice())*Integer.parseInt(num);;
|
139
|
-
String unit = item.getUnit();
|
140
|
-
System.out.println(item.getItemCode() + item.getName() + num + unit + price + "円");
|
141
106
|
sum += price;
|
142
107
|
}
|
143
108
|
|
@@ -159,8 +124,6 @@
|
|
159
124
|
|
160
125
|
public Map<String, Item> items = new HashMap<String, Item>();
|
161
126
|
|
162
|
-
|
163
|
-
|
164
127
|
public Stand() throws IOException {
|
165
128
|
try {
|
166
129
|
BufferedReader csvFile = new BufferedReader(new FileReader("shop.csv"));
|
@@ -175,22 +138,18 @@
|
|
175
138
|
private void readFile(BufferedReader csvFile) throws IOException {
|
176
139
|
String line;
|
177
140
|
while ((line = csvFile.readLine()) != null) {
|
178
|
-
String datas[] = line.
|
141
|
+
String datas[] = line.split(",");
|
179
|
-
datas[0] =
|
142
|
+
datas[0] = Integer.parseInt(datas[0]);
|
180
|
-
if (isValid(datas)) {
|
181
|
-
|
143
|
+
Item item = new Item(datas[0], datas[1], datas[2], datas[3]);
|
182
|
-
|
144
|
+
items.put(datas[0], item);
|
183
145
|
}
|
184
146
|
}
|
185
147
|
}
|
186
148
|
|
187
|
-
private boolean isValid(String[] datas) {
|
188
|
-
return datas.length == 6;
|
189
|
-
}
|
190
149
|
|
191
150
|
public Item order(String code, String num) {
|
192
151
|
Item item = items.get(code);
|
193
|
-
if (item.
|
152
|
+
if (item.Stock(num)) {
|
194
153
|
return item;
|
195
154
|
}
|
196
155
|
return null;
|
3
追加記入
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
shoppingというシステムを作っています。
|
4
4
|
|
5
|
-
shopping.javaの
|
5
|
+
shopping.javaの6行目の
|
6
6
|
Stand stand = new Stand();
|
7
7
|
に以下のエラーメッセージが発生しています。
|
8
8
|
エラーを解決したいです。
|
2
追加記入
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
Javaでショッピングのプログラムを作っています。コンストラクターとクラスのインスタンス化をする際にエラーが出てしまいます。
|
body
CHANGED
File without changes
|
1
追加記入
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
3
|
shoppingというシステムを作っています。
|
4
|
+
|
5
|
+
shopping.javaの
|
6
|
+
Stand stand = new Stand();
|
4
|
-
以下のエラーメッセージが発生しま
|
7
|
+
に以下のエラーメッセージが発生しています。
|
5
8
|
エラーを解決したいです。
|
6
9
|
### 発生している問題・エラーメッセージ
|
7
10
|
|
@@ -193,4 +196,7 @@
|
|
193
196
|
return null;
|
194
197
|
}
|
195
198
|
}
|
196
|
-
```
|
199
|
+
```
|
200
|
+
|
201
|
+
### 考えたこと
|
202
|
+
this()やsuper()を記入してみましたが、上手くいきませんでした。
|