forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyStatsApp.java
More file actions
153 lines (116 loc) · 3.94 KB
/
MyStatsApp.java
File metadata and controls
153 lines (116 loc) · 3.94 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.zetcode;
import com.opencsv.CSVReaderBuilder;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.math3.stat.StatUtils;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
/**
* MyStatsApp is a simple console application which computes
* basic statistics of a series of data values. The application takes
* a file of data as its single argument.
*
* @author janbodnar
*/
public class MyStatsApp {
/**
* Runs the application
*
* @param args an array of String arguments to be parsed
*/
public void run(String[] args) {
CommandLine line = parseArguments(args);
if (line.hasOption("filename")) {
System.out.println(line.getOptionValue("filename"));
String fileName = line.getOptionValue("filename");
double[] data = readData(fileName);
calculateAndPrintStats(data);
} else {
printAppHelp();
}
}
/**
* Parses application arguments
*
* @param args application arguments
* @return <code>CommandLine</code> which represents a list of application
* arguments.
*/
private CommandLine parseArguments(String[] args) {
Options options = getOptions();
CommandLine line = null;
CommandLineParser parser = new DefaultParser();
try {
line = parser.parse(options, args);
} catch (ParseException ex) {
System.err.println("Failed to parse command line arguments");
System.err.println(ex.toString());
printAppHelp();
System.exit(1);
}
return line;
}
/**
* Reads application data from a file
*
* @param fileName file of application data
* @return array of double values
*/
private double[] readData(String fileName) {
var data = new ArrayList<Double>();
double[] mydata = null;
try (var reader = Files.newBufferedReader(Paths.get(fileName));
var csvReader = new CSVReaderBuilder(reader).build()) {
String[] nextLine;
while ((nextLine = csvReader.readNext()) != null) {
for (String e : nextLine) {
data.add(Double.parseDouble(e));
}
}
mydata = ArrayUtils.toPrimitive(data.toArray(new Double[0]));
} catch (IOException ex) {
System.err.println("Failed to read file");
System.err.println(ex.toString());
System.exit(1);
}
return mydata;
}
/**
* Generates application command line options
*
* @return application <code>Options</code>
*/
private Options getOptions() {
var options = new Options();
options.addOption("f", "filename", true, "file name to load data from");
return options;
}
/**
* Prints application help
*/
private void printAppHelp() {
Options options = getOptions();
var formatter = new HelpFormatter();
formatter.printHelp("JavaStatsEx", options, true);
}
/**
* Calculates and prints data statistics
*
* @param data input data
*/
private void calculateAndPrintStats(double[] data) {
System.out.format("Geometric mean: %f%n", StatUtils.geometricMean(data));
System.out.format("Arithmetic mean: %f%n", StatUtils.mean(data));
System.out.format("Max: %f%n", StatUtils.max(data));
System.out.format("Min: %f%n", StatUtils.min(data));
System.out.format("Sum: %f%n", StatUtils.sum(data));
System.out.format("Variance: %f%n", StatUtils.variance(data));
}
}