133 lines
5.5 KiB
Java
133 lines
5.5 KiB
Java
import java.io.BufferedReader;
|
|
import java.io.InputStreamReader;
|
|
import java.io.IOException;
|
|
import java.math.BigDecimal;
|
|
import java.math.MathContext;
|
|
import java.math.RoundingMode;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.text.DecimalFormat;
|
|
import java.time.LocalTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.temporal.ChronoUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
public class DrinkingBar {
|
|
|
|
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm");
|
|
private static final int MINS_PER_HOUR = 60;
|
|
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;
|
|
|
|
|
|
private DrinkingBar(LocalTime startTime) {
|
|
this.startTime = startTime;
|
|
this.totalMinutes = DEFAULT_TOTAL_TIME;
|
|
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
|
|
this.endTime = startTime.plusMinutes(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 BigDecimal.valueOf((minutesPerLitre * litresToNextStep) + 1);
|
|
}
|
|
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|