-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerPlant.java
More file actions
348 lines (293 loc) · 10.2 KB
/
PowerPlant.java
File metadata and controls
348 lines (293 loc) · 10.2 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Arrays;
public class PowerPlant {
// this holds the entries read from the file
private ArrayList<Entry> entries = new ArrayList<Entry>();
// keep the data file name so we can convert it to the stats file name
private String dataFileName;
/**
* Class to hold power plant data entries.
*/
class Entry implements Comparable<Entry> {
private String month;
private int day;
private int year;
private double powerOutput;
public Entry(String m, int d, int y, double pO) {
this.month = m;
this.day = d;
this.year = y;
this.powerOutput = pO;
}
// Accessor methods
public String getMonth() {
return month;
}
public int getDay() {
return day;
}
public int getYear() {
return year;
}
public double getPowerOutput() {
return powerOutput;
}
// mutator methods
public void setMonth(String month) {
this.month = month;
}
public void setDay(int day) {
this.day = day;
}
public void setYear(int year) {
this.year = year;
}
public void setpower(double powerOutput) {
this.powerOutput = powerOutput;
}
/**
* This makes entries orderable by power output and is used when generating the
* statistics file.x
*/
@Override
public int compareTo(Entry other) {
if (this.powerOutput == other.powerOutput)
return 0;
if (this.powerOutput < other.powerOutput)
return -1;
return 1;
}
}
/**
* Convert a line from the data file into an Entry instance
*
* @param line one line of text from the data file
* @return an Entry corresponding to the line
*/
private Entry createEntryFromLine(String line) {
// the data file is separated by spaces, so split it and then convert
// the individual parts into the right types.
String[] parts = line.split(" ");
String month = parts[0];
int day = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);
double powerOutput = Double.parseDouble(parts[3]);
Entry entry = new Entry(month, day, year, powerOutput);
return entry;
}
/**
* Read in file and store it into an list of Entry objects
*
* @throws IOException
*/
private void uploadData() throws IOException {
Scanner inputScanner = new Scanner(System.in);
System.out.print("Enter a file name: ");
dataFileName = inputScanner.nextLine();
File file = new File(dataFileName);
Scanner fileScanner = new Scanner(file);
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
Entry entry = createEntryFromLine(line);
entries.add(entry);
}
fileScanner.close();
System.out.println("uploaded " + entries.size());
}
/**
* Format an Entry to writing to screen or file
* @param e an Entry instance
* @return a String with Entry data
*/
private String getPrintFormat(Entry e) {
StringBuilder s = new StringBuilder();
s.append("Date: ");
s.append(e.getMonth());
s.append(" ");
s.append(e.getDay());
s.append(", ");
s.append(e.getYear());
s.append(" Output: ");
s.append(e.getPowerOutput());
return s.toString();
}
/**
* Print all entries as they were uploaded from the file
*/
private void printData() {
for (Entry e: entries) {
String s = getPrintFormat(e);
System.out.println(s);
}
}
/**
* Prompts for a month and prints corresponding entries
*/
private void printMonth() {
System.out.println("Enter a month:");
Scanner input = new Scanner(System.in);
String month = input.nextLine();
List<Entry> monthly = new ArrayList<Entry>();
for (Entry e: entries) {
if (e.getMonth().equals(month)) {
monthly.add(e);
}
}
if (monthly.size() < 1) {
System.out.println("No entries for " + month);
} else {
for (Entry e : monthly) {
String s = getPrintFormat(e);
System.out.println(s);
}
}
}
/**
* Display the menu and ensure that the user has entered a valid selection.
*
* @return an int corresponding to the menu item
*/
private int getMainMenuSelection() {
Scanner input = new Scanner(System.in);
int selection = -1;
boolean validEntry = false;
while (!validEntry) {
System.out.println("1. Upload Data");
System.out.println("2. View Data");
System.out.println("3. Download Statistics");
System.out.println("4. Print Month");
System.out.println("0. Exit Program");
while (!input.hasNextInt()) {
input.next();
System.out.println("Enter a number");
}
selection = input.nextInt();
// is the entry actually one of the menu items?
validEntry = selection >= 0 && selection < 5;
}
return selection;
}
/**
* Returns a new list of entries ordered by power output
* @return
*/
private List<Entry> getEntriesByPowerOutput() {
List<Entry> orderedEntries = new ArrayList<Entry>(this.entries);
Collections.sort(orderedEntries);
return orderedEntries;
}
/**
* Sums output by month
* @return a Map associating a month to a total power output
*/
private Map<String, Double> getTotalOutputByMonth() {
Map<String, Double> totals = new HashMap<String, Double>();
for (Entry e : entries) {
// Find the total for the month
Double total = totals.get(e.getMonth());
if (total == null) {
// if it's the first time, set it to the current entry's output
total = e.getPowerOutput();
} else {
// otherwise add it to what's already there
total = total + e.getPowerOutput();
}
totals.put(e.getMonth(), total);
}
return totals;
}
/**
* Calculate the average power output of entries
*
* @return the arithmetic mean of power output, or null if empty
*/
private Double getAverageOutput() {
// can't compute an average of empty or we will
// try to divide by zero
if (entries.size() < 1)
return null;
double total = 0;
for (Entry e : entries) {
total = total + e.getPowerOutput();
}
double avg = total / entries.size();
return avg;
}
/**
* Writes formatted statistics to a file ending in _stats.txt
* @throws IOException
*/
private void writeStatsFile() throws IOException {
// Don't attempt to do anything when we don't have any entries
if (entries.size() < 1) {
System.out.println("No entries, aborting.");
return;
}
List<Entry> orderedEntries = getEntriesByPowerOutput();
System.out.println("data file name: " + dataFileName);
// we could use substr, but it splitting the file name on the dot is clearer
String[] parts = dataFileName.split("\\.");
String statsFileName = parts[0] + "_stats.txt";
FileWriter output = new FileWriter(statsFileName);
// write entries ordered by power output
for (Entry e : orderedEntries) {
String s = getPrintFormat(e);
output.write(s);
output.write("\n");
}
// write the highest output entry
Entry highestOutput = orderedEntries.get(orderedEntries.size() - 1);
output.write("Highest Output on ");
output.write(String.valueOf(highestOutput.getMonth()));
output.write(" ");
output.write(String.valueOf(highestOutput.getDay()));
output.write(", ");
output.write(String.valueOf(highestOutput.getYear()));
output.write("\n");
Map<String, Double> byMonth = getTotalOutputByMonth();
for (Map.Entry<String, Double> pair : byMonth.entrySet()) {
output.write("Total for ");
output.write(pair.getKey());
output.write(": ");
output.write(pair.getValue().toString());
output.write("\n");
}
// use a big-D Double because it could be null
Double avgOutput = getAverageOutput();
if (avgOutput != null) {
output.write("Average ouput: ");
output.write(avgOutput.toString());
output.write("\n");
}
output.close();
System.out.println("wrote to " + statsFileName);
}
/**
* Main program. Displays the menu, prompts user, and initiates actions.
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// This is the main PowerPlant instance
PowerPlant pp = new PowerPlant();
int selection;
do {
selection = pp.getMainMenuSelection();
if (selection == 1) {
pp.uploadData();
} else if (selection == 2) {
pp.printData();
} else if (selection == 3) {
pp.writeStatsFile();
} else if (selection == 4) {
pp.printMonth();
}
} while (selection != 0);
}
}