DerKleineVomSee
Mitglied
Ausstellen kann ich ihn leider nicht. Ich denke auch, dass das nicht der richtige Weg sein kann. Kann man nicht irgendwie die Cursor Position festlegen?
Follow along with the video below to see how to install our site as a web app on your home screen.
Anmerkung: This feature currently requires accessing the site using the built-in Safari browser.
package de.tutorials;
/**
* @author Tom
*
*/
public class ConsoleProgressIndicator {
int minValue;
int currentValue;
int maxValue;
int maxChars;
char progressChar = '#';
int charsPrintedCount;
public ConsoleProgressIndicator(int maxChars, char progressChar) {
this.maxChars = maxChars;
this.progressChar = progressChar;
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
new ConsoleProgressIndicator(40, '#')
.execute(new IRunnableWithConsoleProgressIndicatorSupport() {
public void run(
ConsoleProgressIndicator consoleProgressIndicator) {
int minValue = 0;
int maxValue = 200; // 100, 1000, 50, 20
consoleProgressIndicator.setup(minValue, maxValue);
for (int i = minValue; i < maxValue; i++) {
consoleProgressIndicator.updateProgress(i);
try {
Thread.sleep(10L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
}
protected void setup(int minValue, int maxValue) {
this.minValue = minValue;
this.maxValue = maxValue;
}
private void execute(
IRunnableWithConsoleProgressIndicatorSupport runnableWithConsoleProgressIndicatorSupport) {
System.out.print("[");
runnableWithConsoleProgressIndicatorSupport.run(this);
System.out.println("]");
}
static interface IRunnableWithConsoleProgressIndicatorSupport {
void run(ConsoleProgressIndicator consoleProgressIndicator);
}
public void updateProgress(int value) {
double actualProgressPercentage = (double) value / maxValue;
int charsToPrint = (int) (actualProgressPercentage * maxChars)
- charsPrintedCount;
if (charsToPrint > 0) {
for (int i = 0; i < charsToPrint; i++) {
System.out.print(progressChar);
}
this.charsPrintedCount += charsToPrint;
}
this.currentValue = value;
}
}
for (int i = 0; i < 10; i++)
{
.
.
System.out.print("#");
.
.
}