Ajout progress bar

This commit is contained in:
Louis Calas
2019-09-15 12:27:15 +02:00
parent 27267f0422
commit edff961ad3
5 changed files with 118 additions and 68 deletions

View File

@@ -0,0 +1,44 @@
package progressbar;
public class ProgressBar {
private StringBuilder progress;
/**
* initialize progress bar properties.
*/
public ProgressBar() {
init();
}
/**
* called whenever the progress bar needs to be updated.
* that is whenever progress was made.
*
* @param done an int representing the work done so far
* @param total an int representing the total work
*/
public void update(int done, int total) {
char[] workchars = {'|', '/', '-', '\\'};
String format = "\r%3d%% %s %c";
int percent = (++done * 100) / total;
int extrachars = (percent / 2) - this.progress.length();
while (extrachars-- > 0) {
progress.append('=');
}
System.out.printf(format, percent, progress,
workchars[done % workchars.length]);
if (done == total) {
System.out.flush();
System.out.println();
init();
}
}
private void init() {
this.progress = new StringBuilder(60);
}
}