Compare commits
11 Commits
e0750ef26f
...
84a2a94064
Author | SHA1 | Date | |
---|---|---|---|
84a2a94064 | |||
ccb9c06f98 | |||
c3be3892bd | |||
468403801b | |||
b2800a2f78 | |||
4581cec622 | |||
dc99ca2ee4 | |||
d73f2e0cd4 | |||
7cab32467d | |||
a77c8e3e5f | |||
c9d33e6f76 |
76
DrinkingBar.java
Normal file
76
DrinkingBar.java
Normal file
@@ -0,0 +1,76 @@
|
||||
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 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);
|
||||
|
||||
|
||||
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 static long getMinutesToNextStep(double currentLitres, long totalMinutes) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
||||
private static String minutesToTimeString(long minutes) { // DEBUG
|
||||
var minutesBD = BigDecimal.valueOf(minutes);
|
||||
BigDecimal[] hoursAndMinutes = minutesBD.divideAndRemainder(MINS_PER_HOUR_BD, MC_INTEGER);
|
||||
return LocalTime.of(hoursAndMinutes[0].intValue(), hoursAndMinutes[1].intValue()).format(TIME_FORMATTER);
|
||||
}
|
||||
}
|
@@ -27,6 +27,7 @@ public class LoadingBar {
|
||||
private static final int MIN_LUNCH_DURATION = 30;
|
||||
private static final LocalTime LATEST_LUNCH_TIME = LocalTime.of(13, 30);
|
||||
private static final int MINS_PER_HOUR = 60;
|
||||
private static final BigDecimal MINS_PER_HOUR_BD = BigDecimal.valueOf(MINS_PER_HOUR);
|
||||
private static final long DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH = 5L * MINS_PER_HOUR;
|
||||
private static final int MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH = 6 * MINS_PER_HOUR;
|
||||
private static final long MAX_NUMBER_WORK_MINS = 8L * MINS_PER_HOUR;
|
||||
@@ -76,9 +77,10 @@ public class LoadingBar {
|
||||
private static void askParametersAndRun() throws IOException {
|
||||
var br = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
|
||||
print("Ankunftszeit: ");
|
||||
String startTimeRaw = br.readLine();
|
||||
var startTime = LocalTime.parse(startTimeRaw, TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
handleMittagspause(br, startTime);
|
||||
var startTime = LocalTime.parse(br.readLine(), TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
if (startTime.until(LocalTime.now(), ChronoUnit.MINUTES) < DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH) {
|
||||
handleMittagspause(br, startTime);
|
||||
}
|
||||
handleZapfenstreich(br, startTime);
|
||||
}
|
||||
|
||||
@@ -119,6 +121,9 @@ public class LoadingBar {
|
||||
if (mittagspauseDurationRaw != null && !mittagspauseDurationRaw.isBlank()) {
|
||||
mittagspauseDuration = Integer.valueOf(mittagspauseDurationRaw);
|
||||
}
|
||||
LocalTime vorlaeufigeEndzeit = startTime.plusMinutes(MAX_NUMBER_WORK_MINS)
|
||||
.plusMinutes(mittagspauseDuration != null ? mittagspauseDuration : MIN_LUNCH_DURATION);
|
||||
println("Endzeit: " + TIME_FORMATTER.format(vorlaeufigeEndzeit));
|
||||
print("Feierabend verschieben um (optional): ");
|
||||
String zapfenstreichOffsetRaw = br.readLine();
|
||||
Integer zapfenstreichOffset = null;
|
||||
@@ -189,7 +194,7 @@ public class LoadingBar {
|
||||
} else if (OFFSET_PATTERN.matcher(nextArg).matches()) {
|
||||
endTimeOffset = Integer.parseInt(nextArg);
|
||||
} else {
|
||||
verifyDurationFormat(nextArg, "Argument nach " + DaySection.ZAPFENSTREICH.getParam(), true); // FSFIXME erweitere Fehlermeldung
|
||||
verifyDurationFormat(nextArg, "Argument nach " + DaySection.ZAPFENSTREICH.getParam());
|
||||
lunchDuration = Integer.parseInt(nextArg);
|
||||
}
|
||||
if (args.length == 3) {
|
||||
@@ -230,26 +235,25 @@ public class LoadingBar {
|
||||
|
||||
|
||||
private static void verifyTimeFormat(String param, String errMsgPrefix) {
|
||||
verifyInputFormat(TIME_PATTERN, param, errMsgPrefix, true, false);
|
||||
verifyInputFormat(TIME_PATTERN, param, errMsgPrefix, "Uhrzeitformat (" + TIME_FORMAT + ")", false);
|
||||
}
|
||||
|
||||
|
||||
private static void verifyDurationFormat(String param, String errMsgPrefix, boolean timeInputPossible) {
|
||||
verifyInputFormat(LUNCH_DURATION_PATTERN, param, errMsgPrefix, false, timeInputPossible);
|
||||
private static void verifyDurationFormat(String param, String errMsgPrefix) {
|
||||
verifyInputFormat(LUNCH_DURATION_PATTERN, param, errMsgPrefix, "Minutenanzahl (ganze Zahl)", true);
|
||||
}
|
||||
|
||||
|
||||
private static void verifyOffsetFormat(String param, String errMsgPrefix) {
|
||||
verifyInputFormat(OFFSET_PATTERN, param, errMsgPrefix, false, false);
|
||||
verifyInputFormat(OFFSET_PATTERN, param, errMsgPrefix, "Minutendifferenz (ganze Zahl mit Vorzeichen)", false);
|
||||
}
|
||||
|
||||
|
||||
private static void verifyInputFormat(Pattern pattern, String param, String errMsgPrefix, boolean timeInput, boolean timeInputPossible) {
|
||||
private static void verifyInputFormat(Pattern pattern, String param, String errMsgPrefix, String firstInputPart, boolean timeInputPossible) {
|
||||
if (pattern.matcher(param).matches()) {
|
||||
return;
|
||||
} // FSFIXME fine tune message -> HH:mm, mm, -+mm
|
||||
var firstInputPart = timeInput ? "Uhrzeitformat (" + TIME_FORMAT + ")" : "Minutenanzahl (" + LUNCH_DURATION_PATTERN + ")";
|
||||
var possibleTimeInputPart = !timeInput && timeInputPossible ? " oder Uhrzeitformat (" + TIME_FORMAT + ")" : "";
|
||||
}
|
||||
var possibleTimeInputPart = timeInputPossible ? " oder Uhrzeitformat (" + TIME_FORMAT + ")" : "";
|
||||
println(errMsgPrefix + " \"" + param + "\" muss " + firstInputPart + possibleTimeInputPart + " entsprechen.");
|
||||
System.exit(1);
|
||||
}
|
||||
@@ -361,7 +365,7 @@ public class LoadingBar {
|
||||
return MIN_LUNCH_DURATION;
|
||||
}
|
||||
long totalDuration = startTime.until(endTime, ChronoUnit.MINUTES);
|
||||
int effectiveLunchDuration = ((int) totalDuration) - MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH;
|
||||
long effectiveLunchDuration = totalDuration - MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH;
|
||||
return getMinLunchDuration(effectiveLunchDuration);
|
||||
}
|
||||
|
||||
@@ -418,8 +422,9 @@ public class LoadingBar {
|
||||
private static String fillLoadingBar(long totalMinutes, long passedMinutes, boolean progressive) {
|
||||
var nonNegativePassedMinutes = passedMinutes < 0 ? 0 : passedMinutes;
|
||||
BigDecimal wholePercentage = BigDecimal.valueOf(100)
|
||||
.multiply( // kind of reverse dreisatz to avoid to have e.g. 99.9999 instead of 100 %
|
||||
BigDecimal.valueOf(nonNegativePassedMinutes).divide(BigDecimal.valueOf(totalMinutes), MathContext.DECIMAL64));
|
||||
// kind of reverse dreisatz to avoid having e.g. 99.9999 instead of 100 %
|
||||
.multiply(BigDecimal.valueOf(nonNegativePassedMinutes))
|
||||
.divide(BigDecimal.valueOf(totalMinutes), MathContext.DECIMAL64);
|
||||
int numberOfEquals = wholePercentage.intValue();
|
||||
var sb = new StringBuilder("[");
|
||||
for (int i = 0; i < LINE_LENGTH; i++) {
|
||||
@@ -441,7 +446,7 @@ public class LoadingBar {
|
||||
|
||||
private static String minutesToTimeString(long minutes) {
|
||||
var minutesBD = BigDecimal.valueOf(minutes);
|
||||
BigDecimal[] hoursAndMinutes = minutesBD.divideAndRemainder(BigDecimal.valueOf(MINS_PER_HOUR), MC_INTEGER);
|
||||
BigDecimal[] hoursAndMinutes = minutesBD.divideAndRemainder(MINS_PER_HOUR_BD, MC_INTEGER);
|
||||
return LocalTime.of(hoursAndMinutes[0].intValue(), hoursAndMinutes[1].intValue()).format(TIME_FORMATTER);
|
||||
}
|
||||
}
|
||||
|
@@ -16,6 +16,7 @@ public class SimpleLoadingBar {
|
||||
private static final Pattern TIME_PATTERN = Pattern.compile("(?>[01]\\d|2[0-4]):[0-5]\\d");
|
||||
private static final DecimalFormat PERCENTAGE_FORMAT = new DecimalFormat("00.00");
|
||||
private static final int MINS_PER_HOUR = 60;
|
||||
private static final BigDecimal MINS_PER_HOUR_BD = BigDecimal.valueOf(MINS_PER_HOUR);
|
||||
private static final int LINE_LENGTH = 100;
|
||||
private static final MathContext MC_INTEGER = new MathContext(1, RoundingMode.HALF_EVEN);
|
||||
|
||||
@@ -142,7 +143,7 @@ public class SimpleLoadingBar {
|
||||
|
||||
private static String minutesToTimeString(long minutes) {
|
||||
var minutesBD = BigDecimal.valueOf(minutes);
|
||||
BigDecimal[] hoursAndMinutes = minutesBD.divideAndRemainder(BigDecimal.valueOf(MINS_PER_HOUR), MC_INTEGER);
|
||||
BigDecimal[] hoursAndMinutes = minutesBD.divideAndRemainder(MINS_PER_HOUR_BD, MC_INTEGER);
|
||||
return LocalTime.of(hoursAndMinutes[0].intValue(), hoursAndMinutes[1].intValue()).format(TIME_FORMATTER);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user