-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBarChartRacer.java
More file actions
55 lines (52 loc) · 2.31 KB
/
Copy pathBarChartRacer.java
File metadata and controls
55 lines (52 loc) · 2.31 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
/* *****************************************************************************
* Name: Ada Lovelace
* Coursera User ID: 123456
* Last modified: October 16, 1842
**************************************************************************** */
import java.util.Arrays;
public class BarChartRacer {
public static void main(String[] args) {
In fileName = new In(args[0]);
int noOfbars = Integer.parseInt(args[1]);
String title = fileName.readLine();
String xAxisLabel = fileName.readLine();
String source = fileName.readLine();
BarChart chart = new BarChart(title, xAxisLabel, source);
StdDraw.setCanvasSize(1000, 700);
StdDraw.enableDoubleBuffering();
while (fileName.hasNextLine()) {
String readSpace = fileName.readLine();
String n = fileName.readLine();
int noOfEntries = Integer.parseInt(n);
Bar[] bars = new Bar[noOfEntries];
for (int i = 0; i < noOfEntries; i++) {
String line = fileName.readLine();
int c = 0;
int[] indexOfCommas = new int[4];
for (int j = 0; j < line.length(); j++) {
if (line.charAt(j) == ',') {
indexOfCommas[c++] = j;
}
}
String caption = line.substring(0, indexOfCommas[0]);
String barName = line.substring(indexOfCommas[0] + 1, indexOfCommas[1]);
String barNameInfo = line.substring(indexOfCommas[1] + 1,
indexOfCommas[2]);
String value = line.substring(indexOfCommas[2] + 1, indexOfCommas[3]);
String category = line.substring(indexOfCommas[3] + 1);
bars[i] = new Bar(barName, Integer.parseInt(value), category);
chart.setCaption(caption);
}
Arrays.sort(bars);
for (int i = 0; i < Math.min(noOfbars, noOfEntries); i++) {
int k = noOfEntries - 1 - i;
chart.add(bars[k].getName(), bars[k].getValue(), bars[k].getCategory());
}
chart.draw();
StdDraw.show();
StdDraw.pause(100);
StdDraw.clear();
chart.reset();
}
}
}