adjusted standalone classes (approximately) to state of classes in project

This commit is contained in:
2025-07-31 09:55:06 +02:00
parent 591e256fa5
commit f854d2460c
3 changed files with 311 additions and 170 deletions

View File

@@ -18,59 +18,115 @@ public class DrinkingBar {
private static final int MINS_PER_HALF_HOUR = MINS_PER_HOUR / 2;
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 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);
private final LocalTime startTime;
private LocalTime endTime;
private long totalMinutes;
private BigDecimal totalMinutesBD;
public static void main(String[] args) throws IOException {
var br = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
System.out.print("Ankunftszeit: ");
var startTime = LocalTime.parse(br.readLine(), TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
long totalMinutes = 8 * MINS_PER_HOUR + MINS_PER_HALF_HOUR;
long passedMinutes = startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
// long passedMinutes = 0; // DEBUG
double prevPrintedLitres = 0.0;
while (passedMinutes < totalMinutes) {
if (passedMinutes <= MINUTES_BEFORE_PAUSE || passedMinutes > MINUTES_WITH_PAUSE) {
double currentLitres = 2.0 / totalMinutes * passedMinutes + 0.25;
double printedLitres = currentLitres - (currentLitres % 0.25);
double currentProgressToNextStep = 100 / 0.25 * (currentLitres - printedLitres);
long minutesToNextStep = getMinutesToNextStep(currentLitres, totalMinutes);
System.out.print("\rAktuelles Volumen: " + LITER_FORMAT.format(printedLitres) + "L - "
+ PERCENTAGE_FORMAT.format(currentProgressToNextStep) + "% - " + minutesToTimeString(minutesToNextStep));
prevPrintedLitres = printedLitres;
}
try {
var now = LocalTime.now();
var oneMinuteLater = now.plusMinutes(1).truncatedTo(ChronoUnit.MINUTES);
// +1 second to adjust for ignored milliseconds as it is better to switch between 00 and 01 as between 59 and 00
TimeUnit.SECONDS.sleep(now.until(oneMinuteLater, ChronoUnit.SECONDS) + 1);
// TimeUnit.MILLISECONDS.sleep(100L); // DEBUG
} catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
passedMinutes++;
}
System.out.println("");
private DrinkingBar(LocalTime startTime) {
this.startTime = startTime;
this.totalMinutes = DEFAULT_TOTAL_TIME;
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
this.endTime = startTime.plusMinutes(totalMinutes);
}
private static long getMinutesToNextStep(double currentLitres, long totalMinutes) {
public static void main(String[] args) throws IOException {
var br = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
print("Ankunftszeit: ");
var startTime = LocalTime.parse(br.readLine(), TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
var db = new DrinkingBar(startTime);
db.showLoadingBar();
}
private static void println(Object o) {
System.out.println(o);
}
private static void print(Object o) {
System.out.print(o);
}
private void setEndTime(LocalTime endTime) {
this.endTime = endTime;
this.totalMinutes = startTime.until(endTime, ChronoUnit.MINUTES);
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
}
private void showLoadingBar() {
long passedMinutes = startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
// long passedMinutes = 0; // DEBUG
if (passedMinutes > totalMinutes) {
passedMinutes = totalMinutes;
} else if (passedMinutes < 0) {
var now = LocalTime.now().truncatedTo(ChronoUnit.SECONDS);
println("!ACHTUNG! Startzeit \"" + startTime + ":00\" liegt in der Zukunft von jetzt an (" + now + ") gesehen.");
}
println(minutesToTimeString(totalMinutesBD) + " gesamt; Endzeit: " + TIME_FORMATTER.format(endTime));
while (passedMinutes < totalMinutes) {
print(fillLoadingBar(passedMinutes, true));
waitUntilNextMinute();
passedMinutes++;
}
println(fillLoadingBar(passedMinutes, false));
}
private String fillLoadingBar(long passedMinutes, boolean progressive) {
long effectivePassedMinutes = passedMinutes;
if (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);
BigDecimal minutesToNextStep = getMinutesToNextStep(currentLitres);
String progressivePart = progressive ? "\r" : "";
return progressivePart + "Aktuelles Volumen: " + LITER_FORMAT.format(printedLitres) + "L - "
// + PERCENTAGE_FORMAT.format(currentProgressToNextStep) + "% - "
+ minutesToTimeString(minutesToNextStep);
}
private BigDecimal getMinutesToNextStep(double currentLitres) {
// berechne Liter benötigt bis zum nächsten 0.25er Schritt
double litresToNextStep = 0.25 - (currentLitres % 0.25);
// berechne Minuten benötigt für 1 Liter
double minutesPerLitre = totalMinutes / 2.0;
// berechne Minuten benötigt bis zum nächsten 0.25er Schritt
return (long) (minutesPerLitre * litresToNextStep) + 1;
return BigDecimal.valueOf((minutesPerLitre * litresToNextStep) + 1);
}
private static String minutesToTimeString(long minutes) { // DEBUG
var minutesBD = BigDecimal.valueOf(minutes);
BigDecimal[] hoursAndMinutes = minutesBD.divideAndRemainder(MINS_PER_HOUR_BD, MC_INTEGER);
private String minutesToTimeString(BigDecimal minutes) {
BigDecimal[] hoursAndMinutes = minutes.divideAndRemainder(MINS_PER_HOUR_BD, MC_INTEGER);
return LocalTime.of(hoursAndMinutes[0].intValue(), hoursAndMinutes[1].intValue()).format(TIME_FORMATTER);
}
private void waitUntilNextMinute() {
try {
var now = LocalTime.now();
var oneMinuteLater = now.plusMinutes(1).truncatedTo(ChronoUnit.MINUTES);
/* We wait whole seconds to not make it overly complicated.
That results in cut milliseconds: if we would have to wait 1 second and 526 milliseconds, we wait only 1 second.
So, adjust for ignored milliseconds, add +1 second as it is better to switch between 00 and 01 as between 59 and 00 */
TimeUnit.SECONDS.sleep(now.until(oneMinuteLater, ChronoUnit.SECONDS) + 1);
// TimeUnit.MILLISECONDS.sleep(100L); // DEBUG
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException(ie);
}
}
}