Saturday, May 31, 2025

[JAVA] PROCESS BAR

public class ProgressBar {
    public static void main(String[] args) throws InterruptedException {
        int total = 100;
        for (int i = 0; i <= total; i++) {
            String bar = generateProgressBar(i, total);
            System.out.print("\r" + bar + " " + i + "%");
            Thread.sleep(50);
        }
        System.out.println();
    }

    private static String generateProgressBar(int current, int total) {
        int progressBarLength = 30;
        int filledLength = (int) (progressBarLength * ((double) current / total));
        StringBuilder sb = new StringBuilder("[");
        for (int i = 0; i < progressBarLength; i++) {
            if (i < filledLength) {
                sb.append("=");
            } else {
                sb.append(" ");
            }
        }
        sb.append("]");
        return sb.toString();
    }
}

No comments: