質問編集履歴

4

追加記入

2021/06/18 23:37

投稿

kisese
kisese

スコア2

test CHANGED
File without changes
test CHANGED
@@ -44,7 +44,7 @@
44
44
 
45
45
 
46
46
 
47
- Register register = new Register();
47
+ Calculation calculation = new Calculation();
48
48
 
49
49
  Stand stand = new Stand();
50
50
 
@@ -70,330 +70,248 @@
70
70
 
71
71
  private void exec(String[] args)throws IOException {
72
72
 
73
- if (isVaild(args)) {
74
-
75
- try {
73
+ try {
76
-
74
+
77
- stand.prepare();
75
+ stand.prepare();
78
-
76
+
79
- toOrder(args);
77
+ toOrder(args);
80
-
78
+
81
- } catch(Exception e) {
79
+ } catch(Exception e) {
82
-
80
+
83
- e.printStackTrace();
81
+ e.printStackTrace();
82
+
83
+
84
+
85
+ }
86
+
87
+
88
+
89
+ private void toOrder(String[] args) {
90
+
91
+ try {
92
+
93
+ for (int i = 0; i < args.length; i +=2 ) {
94
+
95
+ String code = args[i];
96
+
97
+ String num = args[i + 1];
98
+
99
+ Item oneItem = stand.order(code, num);
100
+
101
+ calculation.buy(oneItem, num);
84
102
 
85
103
  }
86
104
 
105
+ calculation.pay();
106
+
87
- } else {
107
+ } catch(Exception e) {
88
-
108
+
89
- System.out.println("引数が違います");
109
+ e.printStackTrace();
90
110
 
91
111
  }
92
112
 
93
113
  }
94
114
 
95
-
96
-
97
- private boolean isVaild(String[] args){
98
-
99
- return args.length >= 2 && args.length % 2 == 0 ;
100
-
101
- }
115
+ }
116
+
117
+
118
+
119
+
120
+
102
-
121
+ ```
122
+
103
-
123
+ ```Itemjava
124
+
104
-
125
+ public class Item {
126
+
127
+
128
+
129
+ private String codes = null;
130
+
131
+
132
+
133
+ private String name = null;
134
+
135
+
136
+
137
+ private String price = null;
138
+
139
+
140
+
141
+ private String stock = null;
142
+
143
+
144
+
145
+ public Item(String codes, String name, String price, String stock) {
146
+
147
+ this.codes = codes;
148
+
149
+ this.name = name;
150
+
151
+ this.price = price;
152
+
153
+ this.stock = stock;
154
+
155
+ }
156
+
157
+
158
+
159
+ public String getCodes() {
160
+
161
+ return codes;
162
+
163
+ }
164
+
165
+
166
+
167
+ public String getName() {
168
+
169
+ return name;
170
+
171
+ }
172
+
173
+
174
+
175
+ public String getPrice() {
176
+
177
+ return price;
178
+
179
+ }
180
+
181
+
182
+
183
+ public boolean stock(String num) {
184
+
185
+ int order = Integer.parseInt(num);
186
+
187
+ int stock = Integer.parseInt(this.stock);
188
+
189
+ return order <= stock;
190
+
191
+ }
192
+
193
+ }
194
+
195
+ ```
196
+
197
+ ```Calculationjava
198
+
199
+ public class Calculation {
200
+
201
+
202
+
203
+ private int sum;
204
+
205
+
206
+
105
- private void toOrder(String[] args) {
207
+ public void buy(Item item,String num){
208
+
209
+ int price = Integer.parseInt(item.getPrice())*Integer.parseInt(num);;
210
+
211
+ sum += price;
212
+
213
+ }
214
+
215
+
216
+
217
+ public void pay() {
218
+
219
+ System.out.println(sum + "円");
220
+
221
+ }
222
+
223
+ }
224
+
225
+
226
+
227
+ ```
228
+
229
+ ```Standjava
230
+
231
+ import java.io.BufferedReader;
232
+
233
+ import java.io.FileNotFoundException;
234
+
235
+ import java.io.FileReader;
236
+
237
+ import java.io.IOException;
238
+
239
+ import java.util.HashMap;
240
+
241
+ import java.util.Map;
242
+
243
+
244
+
245
+ public class Stand {
246
+
247
+
248
+
249
+ public Map<String, Item> items = new HashMap<String, Item>();
250
+
251
+
252
+
253
+ public Stand() throws IOException {
106
254
 
107
255
  try {
108
256
 
257
+ BufferedReader csvFile = new BufferedReader(new FileReader("shop.csv"));
258
+
259
+ readFile(csvFile);
260
+
261
+ csvFile.close();
262
+
109
- for (int i = 0; i < args.length; i +=2 ) {
263
+ } catch (FileNotFoundException e) {
110
-
111
- String code = args[i];
264
+
112
-
113
- String num = args[i + 1];
114
-
115
- Item oneItem = stand.order(code, num);
116
-
117
- if (oneItem != null) {
118
-
119
- register.buy(oneItem, num);
120
-
121
- } else {
122
-
123
- System.out.println("在庫がありません");
265
+ System.out.println("ファイルがありません");
266
+
124
-
267
+ throw e;
268
+
125
- }
269
+ }
270
+
271
+ }
272
+
273
+
274
+
275
+ private void readFile(BufferedReader csvFile) throws IOException {
276
+
277
+ String line;
278
+
279
+ while ((line = csvFile.readLine()) != null) {
280
+
281
+ String datas[] = line.split(",");
282
+
283
+ datas[0] = Integer.parseInt(datas[0]);
284
+
285
+ Item item = new Item(datas[0], datas[1], datas[2], datas[3]);
286
+
287
+ items.put(datas[0], item);
126
288
 
127
289
  }
128
290
 
129
- register.pay();
130
-
131
- } catch(Exception e) {
132
-
133
- e.printStackTrace();
134
-
135
291
  }
136
292
 
137
293
  }
138
294
 
295
+
296
+
297
+
298
+
299
+ public Item order(String code, String num) {
300
+
301
+ Item item = items.get(code);
302
+
303
+ if (item.Stock(num)) {
304
+
305
+ return item;
306
+
307
+ }
308
+
309
+ return null;
310
+
311
+ }
312
+
139
313
  }
140
314
 
141
-
142
-
143
-
144
-
145
- ```
146
-
147
- ```Itemjava
148
-
149
- public class Item {
150
-
151
-
152
-
153
- private String itemCode = null;
154
-
155
-
156
-
157
- private String name = null;
158
-
159
-
160
-
161
- private String price = null;
162
-
163
-
164
-
165
- private String quantity = null;
166
-
167
-
168
-
169
- private String stock = null;
170
-
171
-
172
-
173
- private String unit = null;
174
-
175
-
176
-
177
- @SuppressWarnings("unused")
178
-
179
- private Item() {
180
-
181
- }
182
-
183
-
184
-
185
- public Item(String itemCode, String name, String price, String quantity, String stock, String unit) {
186
-
187
- this.itemCode = itemCode;
188
-
189
- this.name = name;
190
-
191
- this.price = price;
192
-
193
- this.quantity = quantity;
194
-
195
- this.stock = stock;
196
-
197
- this.unit = unit;
198
-
199
- }
200
-
201
-
202
-
203
- public String getItemCode() {
204
-
205
- return itemCode;
206
-
207
- }
208
-
209
-
210
-
211
- public String getName() {
212
-
213
- return name;
214
-
215
- }
216
-
217
-
218
-
219
- public String getPrice() {
220
-
221
- return price;
222
-
223
- }
224
-
225
- public String getQuantity() {
226
-
227
- return quantity;
228
-
229
- }
230
-
231
-
232
-
233
- public String getStock() {
234
-
235
- return stock;
236
-
237
- }
238
-
239
-
240
-
241
- public String getUnit() {
242
-
243
- return unit;
244
-
245
- }
246
-
247
-
248
-
249
- public boolean checkStock(String num) {
250
-
251
- int orderNum = Integer.parseInt(num)*Integer.parseInt(this.quantity);
252
-
253
- int stockNum = Integer.parseInt(this.stock);
254
-
255
- return orderNum <= stockNum;
256
-
257
- }
258
-
259
- }
260
-
261
- ```
262
-
263
- ```Registerjava
264
-
265
- public class Register {
266
-
267
-
268
-
269
- private int sum;
270
-
271
-
272
-
273
- public void buy(Item item,String num){
274
-
275
- int price = Integer.parseInt(item.getPrice())*Integer.parseInt(num);
276
-
277
- String unit = item.getUnit();
278
-
279
- System.out.println(item.getItemCode() + item.getName() + num + unit + price + "円");
280
-
281
- sum += price;
282
-
283
- }
284
-
285
-
286
-
287
- public void pay() {
288
-
289
- System.out.println(sum + "円");
290
-
291
- }
292
-
293
- }
294
-
295
-
296
-
297
- ```
298
-
299
- ```Standjava
300
-
301
- import java.io.BufferedReader;
302
-
303
- import java.io.FileNotFoundException;
304
-
305
- import java.io.FileReader;
306
-
307
- import java.io.IOException;
308
-
309
- import java.util.HashMap;
310
-
311
- import java.util.Map;
312
-
313
-
314
-
315
- public class Stand {
316
-
317
-
318
-
319
- public Map<String, Item> items = new HashMap<String, Item>();
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
- public Stand() throws IOException {
328
-
329
- try {
330
-
331
- BufferedReader csvFile = new BufferedReader(new FileReader("shop.csv"));
332
-
333
- readFile(csvFile);
334
-
335
- csvFile.close();
336
-
337
- } catch (FileNotFoundException e) {
338
-
339
- System.out.println("ファイルがありません");
340
-
341
- throw e;
342
-
343
- }
344
-
345
- }
346
-
347
-
348
-
349
- private void readFile(BufferedReader csvFile) throws IOException {
350
-
351
- String line;
352
-
353
- while ((line = csvFile.readLine()) != null) {
354
-
355
- String datas[] = line.replaceAll("\"", "").split(",");
356
-
357
- datas[0] = String.format("%04d",Integer.parseInt(datas[0]));
358
-
359
- if (isValid(datas)) {
360
-
361
- Item item = new Item(datas[0], datas[1], datas[2], datas[3], datas[4], datas[5]);
362
-
363
- items.put(datas[0], item);
364
-
365
- }
366
-
367
- }
368
-
369
- }
370
-
371
-
372
-
373
- private boolean isValid(String[] datas) {
374
-
375
- return datas.length == 6;
376
-
377
- }
378
-
379
-
380
-
381
- public Item order(String code, String num) {
382
-
383
- Item item = items.get(code);
384
-
385
- if (item.checkStock(num)) {
386
-
387
- return item;
388
-
389
- }
390
-
391
- return null;
392
-
393
- }
394
-
395
- }
396
-
397
315
  ```
398
316
 
399
317
 

3

追加記入

2021/06/18 23:37

投稿

kisese
kisese

スコア2

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
 
8
8
 
9
- shopping.javaの
9
+ shopping.javaの6行目の
10
10
 
11
11
  Stand stand = new Stand();
12
12
 

2

追加記入

2021/06/18 06:54

投稿

kisese
kisese

スコア2

test CHANGED
@@ -1 +1 @@
1
- 暗黙的スーパー・コストクターによってスローされる例外型 IOException を処理できせん明示的コンストラクターを定義する必要あります
1
+ Javaでショッピグのプログムを作って。コンストラクターとクラスのインスタンス化をする際にエラー出てしいま
test CHANGED
File without changes

1

追加記入

2021/06/18 06:49

投稿

kisese
kisese

スコア2

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,13 @@
4
4
 
5
5
  shoppingというシステムを作っています。
6
6
 
7
+
8
+
9
+ shopping.javaの
10
+
11
+ Stand stand = new Stand();
12
+
7
- 以下のエラーメッセージが発生しました
13
+ 以下のエラーメッセージが発生してい
8
14
 
9
15
  エラーを解決したいです。
10
16
 
@@ -389,3 +395,9 @@
389
395
  }
390
396
 
391
397
  ```
398
+
399
+
400
+
401
+ ### 考えたこと
402
+
403
+ this()やsuper()を記入してみましたが、上手くいきませんでした。