implemted digital watch

This commit is contained in:
Jonas Hinterdorfer 2025-03-20 11:59:40 +01:00
parent 917cfcab8a
commit 5fd9581670
2 changed files with 79 additions and 12 deletions

View File

@ -1,12 +1,13 @@
package at.ionas999.solution.controller;
import at.ionas999.solution.model.DayOfWeek;
import at.ionas999.solution.model.WatchTime;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.shape.Line;
import java.time.LocalTime;
import java.util.Arrays;
public class FastWatchController {
@FXML
@ -38,11 +39,19 @@ public class FastWatchController {
@FXML
private void initialize() {
//TODO: Fill ComboBox from enum.
WatchTime wt = WatchTime.getInstance();
wt.setTime(4,1,DayOfWeek.FRIDAY);
cmbDayOfWeek.getItems().addAll(DayOfWeek.values());
//TODO: Bind labels of digital watch (one-way). Use StringBinding for the minutes to format them (leading 0s).
lblHours.textProperty().bind(wt.hoursProperty().asString());
lblMinutes.textProperty().bind(wt.minutesProperty().asString());
lblDayOfWeek.textProperty().bind(wt.dayOfWeekShortProperty());
//TODO: Bind slider (bidirectionally!) and slider label (one-way).
lblTickDelay.textProperty().bind(wt.tickDelayProperty().asString());
sldTickDelay.valueProperty().bindBidirectional(wt.tickDelayProperty());
//TODO: Add listener to redraw analog hands.
@ -66,7 +75,11 @@ public class FastWatchController {
}
public void btnAdjustOnAction(ActionEvent actionEvent) {
int[] time = Arrays.stream(this.txtTime.getText().split(":"))
.mapToInt(Integer::parseInt)
.toArray();
DayOfWeek dayOfWeek = this.cmbDayOfWeek.getValue();
WatchTime.getInstance().setTime(time[0],time[1], dayOfWeek);
}
//TODO: Listen to the action event of the button.
}

View File

@ -10,18 +10,46 @@ import java.util.TimerTask;
public class WatchTime {
//TODO: Singleton (lazy initialization) goes here.
private Timer timer;
public static WatchTime getInstance() {
private SimpleIntegerProperty hours;
private SimpleIntegerProperty minutes;
private SimpleStringProperty dayOfWeekShort;
private SimpleIntegerProperty tickDelay;
if(instance == null) {
instance = new WatchTime();
}
return WatchTime.instance;
}
private static WatchTime instance = null;
private final Timer timer;
private final SimpleIntegerProperty hours;
private final SimpleIntegerProperty minutes;
private final SimpleStringProperty dayOfWeekShort;
private final SimpleIntegerProperty tickDelay;
private DayOfWeek dayOfWeek;
//TODO: Getters go here.
public SimpleIntegerProperty hoursProperty() {
return hours;
}
public SimpleIntegerProperty minutesProperty() {
return minutes;
}
public SimpleStringProperty dayOfWeekShortProperty() {
return dayOfWeekShort;
}
public SimpleIntegerProperty tickDelayProperty() {
return tickDelay;
}
private WatchTime() {
//TODO: Initialize properties etc.
this.tickDelay = new SimpleIntegerProperty(500);
this.minutes = new SimpleIntegerProperty();
this.hours = new SimpleIntegerProperty();
this.dayOfWeekShort = new SimpleStringProperty();
this.timer = new Timer(true);
this.tickTock();
@ -32,7 +60,20 @@ public class WatchTime {
@Override
public void run() {
Platform.runLater(() -> {
//TODO: Logic for each tick goes here... update hours, minutes, day of week accordingly.
WatchTime wt = WatchTime.getInstance();
wt.minutes.set(wt.minutes.get() == 59 ? 0 : wt.minutes.get() + 1);
if(wt.minutesProperty().get() == 0)
{
wt.hours.set(wt.hours.get() == 23 ? 0 : wt.hours.get() + 1);
}
if(wt.minutesProperty().get() == 0 && wt.hoursProperty().get() == 0)
{
wt.setNextDay();
}
//DO NOT remove/change the following lines!
tickTock();
@ -42,6 +83,19 @@ public class WatchTime {
}
public void setTime(int hours, int minutes, DayOfWeek dayOfWeek) {
//TODO: Allow setting the time and day from outside.
this.hours.set(hours);
this.minutes.set(minutes);
this.dayOfWeek = dayOfWeek;
setDayOfWeekProp();
}
private void setDayOfWeekProp() {
this.dayOfWeekShort.set(this.dayOfWeek.toString().substring(0,3));
}
private void setNextDay() {
int nextDayOrdinal = (this.dayOfWeek.ordinal() + 1) % DayOfWeek.values().length;
this.dayOfWeek = DayOfWeek.values()[nextDayOrdinal];
setDayOfWeekProp();
}
}