IDEA代码运行进度条打印

92 阅读1分钟
/**
 * 进度条打印
 * @param total
 * @param now
 * @param elapse
 */
public static void printBar(int total, int now, String elapse) {
    // 参数校验
    check(total, now);
    // 计算
    double percent = (double) now / total * 100;
    // 计算格数,默认100格
    int fillNum = (int) percent;
    // 生成进度条
    String bar = generateBar(fillNum);
    // 输出
    System.out.printf("\r 进度: [%s] %.2f%% | 说明: %s", bar, percent, elapse);
    if (fillNum == 100) System.out.print('\n');
}


public static BigDecimal getRenMinBi(StringRedisTemplate stringRedisTemplate, BigDecimal yen) {
    String exchangeRate = stringRedisTemplate.opsForValue().get("ExchangeRate");
    if (exchangeRate != null) {
        return yen.multiply(new BigDecimal(exchangeRate.replaceAll("\"", ""))).divide(new BigDecimal("100"), 2, RoundingMode.DOWN);
    } else {
        throw new RuntimeException("找不到汇率!");
    }
}


private static String generateBar(int total, int fillNum, char c) {
    char[] chars = new char[total];
    Arrays.fill(chars, 0, fillNum, c);
    Arrays.fill(chars, fillNum, total, '░');
    return String.valueOf(chars);
}

private static String generateBar(int fillNum) {
    return generateBar(100, fillNum, '█');
}

private static void check(int total, int now) {
    if (total < now) throw new IllegalArgumentException("总数不能小于当前");
    if (total < 1) throw new IllegalArgumentException("总数不能小于1");
    if (now < 0) throw new IllegalArgumentException("当前不能小于0");
}