1 |
package dk.daoas.fulddaekning; |
2 |
|
3 |
|
4 |
|
5 |
public class ProgressBar { |
6 |
|
7 |
int total = 0; |
8 |
int current = 0; |
9 |
|
10 |
|
11 |
public ProgressBar(int total) { |
12 |
this.total = total; |
13 |
} |
14 |
|
15 |
public synchronized void tick() { |
16 |
current ++; |
17 |
|
18 |
if ( (current%25) == 0) { |
19 |
|
20 |
updateProgress( ((double)current) / total ); |
21 |
} |
22 |
} |
23 |
|
24 |
private void updateProgress(double progressPercentage) { |
25 |
StringBuilder sb = new StringBuilder(); |
26 |
|
27 |
final int width = 50; // progress bar width in chars |
28 |
|
29 |
sb.append("\r["); |
30 |
int i = 0; |
31 |
for (; i <= (int)(progressPercentage*width); i++) { |
32 |
sb.append("."); |
33 |
} |
34 |
for (; i < width; i++) { |
35 |
sb.append(" "); |
36 |
} |
37 |
sb.append("] "); |
38 |
sb.append( String.format("%5.1f",progressPercentage*100) ); |
39 |
sb.append("% "); |
40 |
|
41 |
System.out.print( sb.toString() ); |
42 |
} |
43 |
} |