-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
46 lines (38 loc) · 1.5 KB
/
Copy pathApp.java
File metadata and controls
46 lines (38 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.example.cleancoder.splitphase;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class App {
public static void main(String[] args) {
try {
System.out.println(run(args));
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
record CommandLine(String fileName, boolean onlyCountReady) {}
private static long run(String[] args) throws IOException {
CommandLine commandLine = parseCommandLine(args);
return countOrder(args, commandLine);
}
private static CommandLine parseCommandLine(String[] args) {
if(args.length == 0) throw new RuntimeException("파일명을 입력하세요");
String fileName = args[args.length - 1];
boolean onlyCountReady = Stream.of(args).anyMatch(arg -> "-r".equals(arg));
return new CommandLine(fileName, onlyCountReady);
}
private static long countOrder(String[] args, CommandLine commandLine) throws IOException {
File input = Paths.get(commandLine.fileName).toFile();
ObjectMapper mapper = new ObjectMapper();
Order[] orders = mapper.readValue(input, Order[].class);
if(commandLine.onlyCountReady()) {
return Stream.of(orders)
.filter(o -> "ready".equals(o.status()))
.count();
} else {
return orders.length;
}
}
}