updated standalone classes

This commit is contained in:
2025-08-11 11:12:42 +02:00
parent 1f1e06a8e1
commit f0bda09710
3 changed files with 68 additions and 26 deletions

View File

@@ -19,7 +19,10 @@ public class DrinkingBar {
private static final int MINUTES_BEFORE_PAUSE = 4 * MINS_PER_HOUR + MINS_PER_HALF_HOUR;
private static final int MINUTES_WITH_PAUSE = 6 * MINS_PER_HOUR;
private static final int DEFAULT_TOTAL_TIME = 8 * MINS_PER_HOUR + MINS_PER_HALF_HOUR;
private static final DecimalFormat LITER_FORMAT = new DecimalFormat("0.00");
private static final BigDecimal DEFAULT_TOTAL_TIME_BD = BigDecimal.valueOf(DEFAULT_TOTAL_TIME);
private static final BigDecimal DEFAULT_TOTAL_LITRES = BigDecimal.valueOf(2.0);
private static final BigDecimal QUARTER_LITRE = BigDecimal.valueOf(0.25);
private static final DecimalFormat LITRE_FORMAT = new DecimalFormat("0.00");
private static final DecimalFormat PERCENTAGE_FORMAT = new DecimalFormat("00.00");
private static final BigDecimal MINS_PER_HOUR_BD = BigDecimal.valueOf(MINS_PER_HOUR);
private static final MathContext MC_INTEGER = new MathContext(1, RoundingMode.HALF_EVEN);
@@ -28,6 +31,7 @@ public class DrinkingBar {
private LocalTime endTime;
private long totalMinutes;
private BigDecimal totalMinutesBD;
private BigDecimal totalLitres;
private DrinkingBar(LocalTime startTime) {
@@ -35,6 +39,7 @@ public class DrinkingBar {
this.totalMinutes = DEFAULT_TOTAL_TIME;
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
this.endTime = startTime.plusMinutes(totalMinutes);
this.totalLitres = DEFAULT_TOTAL_LITRES;
}
@@ -57,15 +62,36 @@ public class DrinkingBar {
}
protected long getPassedMinutes() {
return startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
}
private void setEndTime(LocalTime endTime) {
this.endTime = endTime;
this.totalMinutes = startTime.until(endTime, ChronoUnit.MINUTES);
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
extraInitEndTimeTotalMinutes();
}
protected void extraInitEndTimeTotalMinutes() {
// correct necessary litres to drink based on the end time.
// lower the volume in quarter litre steps
BigDecimal calcTotalLitres = DEFAULT_TOTAL_LITRES;
BigDecimal totalLitresFromMinutes = DEFAULT_TOTAL_LITRES
.multiply(totalMinutesBD) // reverse dreisatz
.divide(DEFAULT_TOTAL_TIME_BD, MathContext.DECIMAL64);
do {
calcTotalLitres = calcTotalLitres.subtract(QUARTER_LITRE);
} while (calcTotalLitres.compareTo(totalLitresFromMinutes) >= 0);
// add quarter since we always did a step "too many", due to the do ... while loop
this.totalLitres = calcTotalLitres.add(QUARTER_LITRE);
}
private void showLoadingBar() {
long passedMinutes = startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
long passedMinutes = getPassedMinutes();
// long passedMinutes = 0; // DEBUG
if (passedMinutes > totalMinutes) {
passedMinutes = totalMinutes;
@@ -84,28 +110,34 @@ public class DrinkingBar {
private String fillLoadingBar(long passedMinutes, boolean progressive) {
long effectivePassedMinutes = passedMinutes;
if (passedMinutes > MINUTES_BEFORE_PAUSE && passedMinutes <= MINUTES_WITH_PAUSE) {
long effectivePassedMinutes = passedMinutes < 0 ? 0 : passedMinutes;
if (totalMinutes > MINUTES_WITH_PAUSE && passedMinutes > MINUTES_BEFORE_PAUSE && passedMinutes <= MINUTES_WITH_PAUSE) {
effectivePassedMinutes = MINUTES_BEFORE_PAUSE;
}
double currentLitres = 2.0 / totalMinutes * effectivePassedMinutes + 0.25;
double printedLitres = currentLitres - (currentLitres % 0.25);
// double currentProgressToNextStep = 100 / 0.25 * (currentLitres - printedLitres);
var effectivePassedMinutesBD = BigDecimal.valueOf(effectivePassedMinutes);
BigDecimal currentLitres = totalLitres
.multiply(effectivePassedMinutesBD) // reverse dreisatz
.divide(totalMinutesBD, MathContext.DECIMAL64)
.add(QUARTER_LITRE);
BigDecimal printedLitres = currentLitres.subtract(currentLitres.remainder(QUARTER_LITRE, MathContext.DECIMAL64));
/* BigDecimal currentProgressToNextStep = ONE_HUNDRED_PERCENT
.multiply(currentLitres.subtract(printedLitres)) // reverse dreisatz
.divide(QUARTER_LITRE, MathContext.DECIMAL64); */
BigDecimal minutesToNextStep = getMinutesToNextStep(currentLitres);
String progressivePart = progressive ? "\r" : "";
return progressivePart + "Aktuelles Volumen: " + LITER_FORMAT.format(printedLitres) + "L - "
return progressivePart + "Aktuelles Volumen: " + LITRE_FORMAT.format(printedLitres) + "L - "
// + PERCENTAGE_FORMAT.format(currentProgressToNextStep) + "% - "
+ minutesToTimeString(minutesToNextStep);
}
private BigDecimal getMinutesToNextStep(double currentLitres) {
private BigDecimal getMinutesToNextStep(BigDecimal currentLitres) {
// berechne Liter benötigt bis zum nächsten 0.25er Schritt
double litresToNextStep = 0.25 - (currentLitres % 0.25);
BigDecimal litresToNextStep = QUARTER_LITRE.subtract(currentLitres.remainder(QUARTER_LITRE));
// berechne Minuten benötigt für 1 Liter
double minutesPerLitre = totalMinutes / 2.0;
BigDecimal minutesPerLitre = totalMinutesBD.divide(totalLitres, MathContext.DECIMAL64);
// berechne Minuten benötigt bis zum nächsten 0.25er Schritt
return BigDecimal.valueOf((minutesPerLitre * litresToNextStep) + 1);
return minutesPerLitre.multiply(litresToNextStep).setScale(0, RoundingMode.HALF_EVEN);
}

View File

@@ -77,13 +77,6 @@ public class LoadingBar {
}
private void setEndTime(LocalTime endTime) {
this.endTime = endTime;
this.totalMinutes = startTime.until(endTime, ChronoUnit.MINUTES);
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
}
public static void main(String[] args) throws IOException {
if (args.length == 0) {
askParametersAndRun();
@@ -330,6 +323,19 @@ public class LoadingBar {
}
protected long getPassedMinutes() {
return startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
}
private void setEndTime(LocalTime endTime) {
this.endTime = endTime;
this.totalMinutes = startTime.until(endTime, ChronoUnit.MINUTES);
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
}
private boolean hasMittagspauseArrived() {
return startTime.until(LocalTime.now(), ChronoUnit.MINUTES) < DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH;
}
@@ -393,8 +399,7 @@ public class LoadingBar {
return MIN_LUNCH_DURATION;
}
long totalDuration = MAX_NUMBER_WORK_MINS + endTimeOffset;
long effectiveLunchDuration = totalDuration - MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH;
return getMinLunchDuration(effectiveLunchDuration);
return getMinLunchDuration(totalDuration);
}
@@ -403,12 +408,12 @@ public class LoadingBar {
return MIN_LUNCH_DURATION;
}
long totalDuration = startTime.until(manualEndTime, ChronoUnit.MINUTES);
long effectiveLunchDuration = totalDuration - MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH;
return getMinLunchDuration(effectiveLunchDuration);
return getMinLunchDuration(totalDuration);
}
private long getMinLunchDuration(long effectiveLunchDuration) {
private long getMinLunchDuration(long precalculatedTotalDuration) {
long effectiveLunchDuration = precalculatedTotalDuration - MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH;
if (effectiveLunchDuration < 0) {
effectiveLunchDuration = 0;
}
@@ -431,7 +436,7 @@ public class LoadingBar {
private void showLoadingBar() {
long passedMinutes = startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
long passedMinutes = getPassedMinutes();
// long passedMinutes = 0; // DEBUG
if (passedMinutes > totalMinutes) {
passedMinutes = totalMinutes;

View File

@@ -112,6 +112,11 @@ public class SimpleLoadingBar {
}
protected long getPassedMinutes() {
return startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
}
private void setEndTime(LocalTime endTime) {
this.endTime = endTime;
this.totalMinutes = startTime.until(endTime, ChronoUnit.MINUTES);
@@ -128,7 +133,7 @@ public class SimpleLoadingBar {
private void showLoadingBar() {
long passedMinutes = startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
long passedMinutes = getPassedMinutes();
// long passedMinutes = 0; // DEBUG
if (passedMinutes > totalMinutes) {
passedMinutes = totalMinutes;